Selenese – Selenium Commands

 Certified Selenium Professional

Selenium Testing | Selenese – Commands

Selenium commands, often called selenese, are the set of commands that run tests. A sequence of these commands is a test script. Here we explain those commands in detail, and we present the many choices you have in testing web application when using Selenium.

Selenium provides a rich set of commands for fully testing web-app in virtually any way you can imagine. The command set is often called selenese. These commands essentially create a testing language.

In selenese, one can test the existence of UI elements based on their HTML tags, test for specific content, test for broken links, input fields, selection list options, submitting forms, and table data among other things. In addition Selenium commands support testing of window size, mouse position, alerts, Ajax functionality, pop up windows, event handling, and many other web-application features. The Command Reference lists all the available commands.

A command tells Selenium what to do , Selenium commands come in three “flavors”: Actions, Accessors, and Assertions.

  • Actions are commands that generally manipulate the state of the application. They do things like “click this link” and “select that option”. If an Action fails, or has an error, the execution of the current test is stopped. Many Actions can be called with the “AndWait” suffix, e.g. “clickAndWait”. This suffix tells Selenium that the action will cause the browser to make a call to the server, and that Selenium should wait for a new page to load.
  • Accessors examine the state of the application and store the results in variables, e.g. “storeTitle”. They are also used to automatically generate Assertions.
  • Assertions are like Accessors, but they verify that the state of the application conforms to what is expected. Examples include “make sure the page title is X” and “verify that this checkbox is checked”.

All Selenium Assertions can be used in 3 modes: “assert”, “verify”, and ” waitFor”. For example, you can “assertText”, “verifyText” and “waitForText”. When an “assert” fails, the test is aborted. When a “verify” fails, the test will continue execution, logging the failure. This allows a single “assert” to ensure that the application is on the correct page, followed by a bunch of “verify” assertions to test form field values, labels, etc.

“waitFor” commands wait for some condition to become true (which can be useful for testing Ajax applications). They will succeed immediately if the condition is already true. However, they will fail and halt the test if the condition does not become true within the current timeout setting (see the setTimeout action below).

echo – The Selenese Print Command

Selenese has a simple command that allows you to print text to test’s output. This is useful for providing informational progress notes in test which display on the console as test is running. These notes also can be used to provide context within test result reports, which can be useful for finding where a defect exists on a page in the event test finds a problem. Finally, echo statements can be used to print the contents of Selenium variables

Alerts, Popups, and Multiple Windows

Suppose that you are testing a page that looks like this.

<!DOCTYPE HTML>

<html>

<head>

<script type=”text/javascript”>

function output(resultText){

document.getElementById(‘output’).childNodes[0].nodeValue=resultText;

}

function show_confirm(){

var confirmation=confirm(“Chose an option.”);

if (confirmation==true){

output(“Confirmed.”);

}

else{

output(“Rejected!”);

}

}

function show_alert(){

alert(“I’m blocking!”);

output(“Alert is gone.”);

}

function show_prompt(){

var response = prompt(“What’s the best web QA tool?”,”Selenium”);

output(response);

}

function open_window(windowName){

window.open(“newWindow.html”,windowName);

}

</script>

</head>

<body>

<input type=”button” id=”btnConfirm” onclick=”show_confirm()” value=”Show confirm box” />

<input type=”button” id=”btnAlert” onclick=”show_alert()” value=”Show alert” />

<input type=”button” id=”btnPrompt” onclick=”show_prompt()” value=”Show prompt” />

<a href=”newWindow.html” id=”lnkNewWindow” target=”_blank”>New Window Link</a>

<input type=”button” id=”btnNewNamelessWindow” onclick=”open_window()” value=”Open Nameless Window” />

<input type=”button” id=”btnNewNamedWindow” onclick=”open_window(‘Mike’)” value=”Open Named Window” />

<br />

<span id=”output”>

</span>

</body>

</html>

The user must respond to alert/confirm boxes, as well as moving focus to newly opened popup windows. Fortunately, Selenium can cover JavaScript pop-ups.

But before we begin covering alerts/confirms/prompts in individual detail, it is helpful to understand the commonality between them. Alerts, confirmation boxes and prompts all have variations of the following

Command Description
assertFoo(pattern) throws error if pattern doesn’t match the text of the pop-up
assertFooPresent throws error if pop-up is not available
assertFooNotPresent throws error if any pop-up is present
storeFoo(variable) stores the text of the pop-up in a variable
storeFooPresent(variable) stores the text of the pop-up in a variable and returns true or false

When running under Selenium, JavaScript pop-ups will not appear. This is because the function calls are actually being overridden at runtime by Selenium’s own JavaScript. However, just because you cannot see the pop-up doesn’t mean you don’t have to deal with it. To handle a pop-up, you must call its assert for(pattern) function. If you fail to assert the presence of a pop-up next command will be blocked and you will get an error similar to the following [error] Error: There was an unexpected Confirmation! [

Alerts

Let’s start with alerts because they are the simplest pop-up to handle. To begin, open the HTML sample above in a browser and click on the “Show alert” button. You’ll notice that after you close the alert the text “Alert is gone.” is displayed on the page. Now run through the same steps with Selenium IDE recording, and verify the text is added after you close the alert. the test will look something like this –

Command Target Value
open /  
click btnAlert  
assertAlert I’m blocking!  
verifyTextPresent Alert is gone.  

You may be thinking “That’s odd, I never tried to assert that alert.” But this is Selenium-IDE handling and closing the alert for you. If you remove that step and replay the test you will get the following error [error] Error: There was an unexpected Alert! [I’m blocking!]. You must include an assertion of the alert to acknowledge its presence.

If you just want to assert that an alert is present but either don’t know or don’t care what text it contains, you can use assertAlertPresent. This will return true or false, with false halting the test.

Confirmations

Confirmations behave in much the same way as alerts, with assertConfirmation and assertConfirmationPresent offering the same characteristics as their alert counterparts. However, by default Selenium will select OK when a confirmation pops up. Try recording clicking on the “Show confirm box” button in the sample page, but click on the “Cancel” button in the popup, then assert the output text. test may look something like this:

Command Target Value
open /  
click btnConfirm  
chooseCancelOnNextConfirmation    
assertConfirmation Choose an option.  
verifyTextPresent Rejected  

The chooseCancelOnNextConfirmation function tells Selenium that all following confirmation should return false. It can be reset by calling chooseOkOnNextConfirmation.

You may notice that you cannot replay this test, because Selenium complains that there is an unhandled confirmation. This is because the order of events Selenium-IDE records causes the click and chooseCancelOnNextConfirmation to be put in the wrong order (it makes sense if you think about it, Selenium can’t know that you’re cancelling before you open a confirmation) Simply switch these two commands and test will run fine.

Prompts

Prompts behave in much the same way as alerts, with assertPrompt and assertPromptPresent offering the same characteristics as their alert counterparts. By default, Selenium will wait for you to input data when the prompt pops up. Try recording clicking on the “Show prompt” button in the sample page and enter “Selenium” into the prompt. test may look something like this:

Command Target Value
open /  
answerOnNextPrompt Selenium!  
click id=btnPrompt  
assertPrompt What’s the best web QA tool?  
verifyTextPresent Selenium!  

Selenium professional free practice test

Go back to Tutorial                                                                                Go to Home Page

Share this post
[social_warfare]
Selenium JavaScript Usage | Selenium Automation Testing Online Course
Browser and variable access by JavaScript | Selenium Automation Testing

Get industry recognized certification – Contact us

keyboard_arrow_up