Netsparker Web Application Security Scanner

Learn Selenium Series - How To Enhance A Script Using Selenium IDE

In previous class we leaned about . In this chapter, we are going to discuss about the commands that will make our automation script more robust, intelligent and complete.

  • Verification of an Element Presence: Following are the two commands in Selenium IDE that are used for the presence of a GUI element on a web page.
  • verifyElementPresent: This command returns value as TRUE if the specified element was FOUND in the web page. It returns value as FALSE if not FOUND.
  • verifyElementNotPresent: This command returns value as TRUE if the specified element was NOT FOUND anywhere in the web page. It returns value as FALSE if it is present anywhere in the web page.

In the below example on Selenium IDE, we are testing Facebook login URL. On this page we are verifying above two commands for Target id=email and id=xyz. Considering the scenarios and function of above two commands below is the test output. It is to be noted that on Facebook login page id=email is present, but id=xyz is not present, hence the output.

 

Selenium IDE Screen1

Click image to enlarge.

 

  • Verification of a Certain Text Presence: Following are the two commands in Selenium IDE that are used for the presence of a certain text on a web page.
  • verifyTextPresent: This command returns value as TRUE if the specified text string was FOUND anywhere in the webpage. Otherwise, if not FOUND then it returns value as FALSE.
  • verifyTextNotPresent: This command returns value as TRUE if the specified text string was NOT FOUND anywhere in the webpage. Otherwise, if FOUND then it returns value as FALSE.

In the below example on Selenium IDE, we are testing Facebook login URL. On this page we are verifying above two commands for texts as ‘Facebook’ and ‘Twitter’. Considering the scenarios and function of above two commands below is the test output. It is to be noted that on Facebook login page, the text as ‘Facebook’ is present but the text as ‘Twitter’ is not present. Hence the output.

 

Selenium IDE Screen2

Click image to enlarge.

 

  • Verification of Specific Position of an Element: The position of an element in Selenium IDE are indicated by measuring (in pixels) that how far it is from the left or top edge of the browser window. Below the two commands used in Selenium IDE for this purpose.
  • verifyElementPositionLeft: This command verifies that if the specified number of pixels match the distance of the GUI element from the left edge of the web page. It will return a FALSE value if the value specified does not match the distance from the left edge.
  • verifyElementPositionTop: This command verifies that if the specified number of pixels match the distance of the GUI element from the top edge of the web page. It will return a FALSE value if the value specified does not match the distance from the top edge.

In the below example on Selenium IDE, we are testing Facebook login URL. On this page we are verifying above two commands for position of GUI element id=email. Considering the scenarios and function of above two commands below is the test output. It is to be noted that on Facebook login page, the position of GUI element id=email is not matching the value in pixels measured for top and left of the page, hence the output.

 

Selenium IDE Screen3

Click image to enlarge.

 

  • Wait commands: In Selenium IDE, given below are the commands that will wait for a new page to load before moving onto the next command.
  • clickAndWait: This command will wait for the new webpage to load after click command is executed by the Selenium IDE to record further results on the new page.
  • typeAndWait: This command will wait for the new webpage to load after type command is executed by the Selenium IDE to record further results on the new page.
  • selectAndWait: This command will wait for the new webpage to load after select command is executed by the Selenium IDE to record further results on the new page.

Given below is the demonstration of the failed attempt to login on Facebook using Selenium IDE.

 

Selenium IDE Screen4

Click image to enlarge.

 

  • Use of waitFor commands: In Selenium IDE, following are the commands that wait for a specified condition to become true before it proceeds to execute the next command (irrespective of loading of a new page). These commands could be more useful to test AJAX-based dynamic websites where the elements values are changed without reloading the whole page. These commands are:
  • waitForTitle: This command will make Selenium IDE to wait to execute next command until the given title is available.
  • waitForTextPresent: This command will make Selenium IDE to wait to execute next command until the given Text is present.
  • waitForAlert: This command will make Selenium IDE to wait to execute next command until the given alert is popped up.
  • Storing Variables and the Echo command: Selenium IDE has the feature to store the value in a variable and later use or echo that value using the following commands.
  • Store: We can use store command to store variables in Selenium IDE. Below is the illustration of store command, here we are storing the value “SoftwareTestingClass” to a variable named “newVariable“. Later we are typing this stored value into Facebook login page for element ‘id=email’ by simply enclosing it in a ${…} symbol e.g. ${newVariable}.

 

Selenium IDE Screen5

Click image to enlarge.

 

  • StoreElementPresent: In Selenium IDE, this command just stores Boolean values which are either “true” or “false” depending on the presence of the specified element. Below is the illustration of such example.

In the below example, on the Facebook login page we are looking for elements id=email (which is present) and id=gmail (which is not present). We are storing the Boolean output of command ‘StoreElementPresent’ in variables myVal1 and myVal2. When this test is executed on Selenium IDE, both of these variables with get value as TRUE and FALSE respectively as former element is present on the web page however latter element is absent on the web page, hence the result as shown below.

Here ‘echo’ command is used to echo the values of the variables which are present in the test log.

 

Selenium IDE Screen6

Click image to enlarge.

 

  • StoreText: In Selenium IDE, this command is used to store the inner text of an element onto a variable.

In the below example, we are storing inner HTML text for h1 tag (css=h1) into a variable ‘var’ that has ‘Facebook logo’. When we echo this variable (echo ${var}), the Selenium IDE logs will display this value as shown below.

 

Selenium IDE Screen7

Click image to enlarge.

 

  • Alerts, Pop-up, and Multiple Windows: Selenium IDE supports several commands to test alerts, pop-ups and multiple windows. Some of them are explained below.
  • Alerts: Alerts can be considered as the simplest form of pop-up windows. Following are the most common Selenium IDE commands used in handling alerts.
CommandsDescription
assertAlert & assertNotAlertThese commands are used to retrieve the message of the alert and assert it to a string value that we have specified for system and is under test.
assertAlertPresent &

assertAlertNotPresent

These commands are used to assert if an Alert is present or not. They return Boolean values as TRUE or FALSE.
storeAlertThis command is used to retrieve the alert message and stores it in a variable that we have specified.
storeAlertPresentThis command returns a TRUE value if an alert is present. Otherwise it returns a FALSE value.
verifyAlert & verifyNotAlertThese commands are used to retrieve the message of the alert and verify if it is equal to the string value that we have specified.
verifyAlertPresent

&verifyAlertNotPresent

These commands are used to verify that if an Alert is present or not. They return Boolean values as TRUE or FALSE.

 

  • Confirmations: In Selenium IDE, confirmations are the pop-ups that give us a CANCEL and an OK button. The commands that we can use for the handling of confirmations are similar to those in handling alerts as explained below.
CommandsDescription
assertConfirmation

& assertNotConfirmation

These commands are used to retrieve the message of the confirmation and asserts it to a string value that we have specified for system and is under test.
assertConfirmationPresent &

assertConfirmationNotPresent

These commands are used to assert if a confirmation is present or not. They return Boolean values as TRUE or FALSE.
storeConfirmationThis command is used to retrieve the confirmation message and stores it in a variable that we have specified.
storeConfirmationPresentThis command returns a TRUE value if a confirmation is present. Otherwise it returns a FALSE value.
verifyConfirmation& verifyNotConfirmationThese commands are used to retrieve the message of the confirmation and verify if it is equal to the string value that we have specified.
verifyConfirmationPresent

&verifyConfirmationNotPresent

These commands are used to verify that if a confirmation is present or not. They return Boolean values as TRUE or FALSE.

 

  • Multiple Windows: It is the scenario that happens when we click on a link that launches a separate window. Here we must instruct Selenium IDE to select that window first before we can start accessing the elements present in it. Below are the steps to do so.

Step 1: First transfer the control from the parent window to the newly launched window using the “selectWindow” command and its title as the locator.

Step 2: Verify the title of the new window is as expected.

Step 3: Access the elements of the newly launched window and run tests on it. Once completed then next, select back the original window using the “selectWindow” command again and “null” as its target.

Step 4: Verify the title of the currently selected window is as expected and resume our tests.

*******************************************

Want to start learn complete Selenium

Chekout Selnium Tutorial Series

***************************************************

If you are not regular reader of this website then highly recommends you to Sign up for our free email newsletter!! Sign up just providing your email address below:

Enter your email address:

Check email in your inbox for confirmation to get latest updates Software Testing for free.

Happy Testing!!!

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>