Friday, July 10, 2015

How to enhance a script using Selenium IDE

Verify Presence of an Element

We can use following two commands to verify the presence of an element:
  • verifyElementPresent - returns TRUE if the specified element was FOUND in the page; FALSE if otherwise
  • verifyElementNotPresent - returns TRUE if the specified element was NOT FOUND anywhere in the page; FALSE if it is present.
The test script below verifies that the User Name text box is present within the Mercury Tours homepage while the First Name text box is not. The First Name text box is actually an element present in the Registration page of Mercury Tours, not in the homepage. strong>Verify Presence of a Certain Text

Verify Presence of a Certain Text

  • verifyTextPresent - returns TRUE if the specified text string was FOUND somewhere in the page; FALSE if otherwise
  • verifyTextNotPresent - returns TRUE if the specified text string was NOT FOUND anywhere in the page; FALSE if it was found
Remember that these commands are case-sensitive.
In the scenario above, "Atlanta to Las Vegas" was treated differently from "atlanta to Las Vegas" because the letter "A"of "Atlanta" was in uppercase on the first one while lowercase on the other. When the verifyTextPresent command was used on each of them, the other one passed and the other failed.

Verify Specific Position of an Element

Selenium IDE indicates the position of an element by measuring (in pixels) how far it is from the left or top edge of the browser window.
  • verifyElementPositionLeft - verifies if the specified number of pixels match the distance of the element from the left edge of the page. This will return FALSE if the value specified does not match the distance from the left edge.
  • verifyElementPositionTop - verifies if the specified number of pixels match the distance of the element from the top edge of the page. This will return FALSE if the value specified does not match the distance from the top edge.

Wait commands

andWait commands

These are commands that will wait for a new page to load before moving onto the next command.
Examples are
  • clickAndWait
  • typeAndWait
  • selectAndWait

waitFor commands

These are commands that wait for a specified condition to become true before proceeding to the next command (irrespective of loading of a new page). These commands are more appropriate to be used on AJAX-based dynamic websites that change values and elements without reloading the whole page. Examples include:
  • waitForTitle
  • waitForTextPresent
  • waitForAlert
Consider the Facebook scenario below. 

We can use a combination of "click" and "waitForTextPresent" to verify the presence of the text "Providing your birthday".
We cannot use clickAndWait because no page was loaded upon clicking on the "Why do I need to provide my birthday?" link.If we do, the test will fail

Storing Variables and the Echo command

Store

To store variables in Selenium IDE, we use the "store" command. The illustration below stores the value "tutorial" to a variable named "myVariable".
To access the variable, simply enclose it in a ${ ... } symbol. For example, to enter the value of "myVariable" onto the "userName" textbox of Mercury Tours, enter ${myVariable} in the Value field.

StoreElementPresent

This command stores either "true" or "false" depending on the presence of the specified element. The script below stores the Boolean value "true" to "var1" and "false" to "var2". To verify, we will use the "echo" command to display the values of var1 and var2. The Base URL for the illustration below was set to Mercury Tours homepage.

StoreText

This command is used to store the inner text of an element onto a variable. The illustration below stores the inner text of the tag in Facebook onto a variable named 'textVar'.

Since it is the only element in the page, it is safe to use 'css=h1' as our target. The image below shows that Selenium IDE was able to save the string "Sign Up" in the 'textVar' variable by printing its value correctly.


Alerts, Pop-up and Multiple Windows


Alerts Alerts are probably the simplest form of pop-up windows. The most common Selenium IDE commands used in handling alerts are the following:

assertAlert
assertNotAlert

retrieves the message of the alert and asserts it to a string value that you specified
assertAlertPresent
assertAlertNotPresent

asserts if an Alert is present or not
storeAlertretrieves the alert message and stores it in a variable that you will specify
storeAlertPresentreturns TRUE if an alert is present; FALSE if otherwise
verifyAlert
verifyNotAlert

retrieves the message of the alert and verifies if it is equal to the string value that you specified
verifyAlertPresent
verifyAlertNotPresent
verifies if an Alert is present or not
  Remember these two things when working with alerts:
  • Selenium IDE will automatically click on the OK button of the alert window and so you will not be able to see the actual alert.
  • Selenium IDE will not be able to handle alerts that are within the page's onload() function. It will only be able to handle alerts that are generated after the page has completely loaded.
In this example, we will use the storeAlert command to show that even though Selenium IDE did not show the actual alert, it was still able to retrieve its message.
Step 1. In Selenium IDE, set the Base URL to http://jsbin.com. 
Step 2. Create the script as shown below.

Step 3. Execute the script and do not expect that you will be able to see the actual alert. 

Confirmations


Confirmations are pop-ups that give you an OK and a CANCEL button, as opposed to alerts which give you only the OK button. The commands you can use in handling confirmations are similar to those in handling alerts.

  • assertConfirmation/assertNotConfirmation
  • assertConfirmationPresent/assertConfirmationNotPresent
  • storeConfirmation
  • storeConfirmationPresent
  • verifyConfirmation/verifyNotConfirmation
  • verifyConfirmationPresent/verifyConfirmationNotPresent
  However, these are the additional commands that you need to use to instruct Selenium which option to choose, whether the OK or the CANCEL button.
  • chooseOkOnNextConfirmation/chooseOkOnNextConfirmationAndWait
  • chooseCancelOnNextConfirmation
You should use these commands before a command that triggers the confirmation box so that Selenium IDE will know beforehand which option to choose. Again, you will not be able to see the actual confirmation box during script execution.
Let us test a webpage that has a button that was coded to show whether the user had pressed the OK or the CANCEL button.
Step 1. In Selenium IDE, set the Base URL to http://jsbin.com
Step 2. Create the script as shown below. This time, we will press the OK button first.
Step 3. Execute the script and notice that you do not see the actual confirmation, but the webpage was able to indicate which button Selenium IDE had pressed.
Step 4. Replace the "chooseOkOnNextConfirmation" command with "chooseCancelOnNextConfirmation" and execute the script again.

Multiple Windows


If you happen to click on a link that launches a separate window, you must first instruct Selenium IDE to select that window first before you could access the elements within it. To do this, you will use the window's title as its locator.
We use the selectWindow command in switching between windows.
We will use a link http://jsbin.com/ocinaj/1 whose title is "First Window". The "here" hyperlink found in that page will open Facebook on a new window, after which we shall instruct Selenium IDE to do the following:
  • Transfer control from the parent window to the newly launched Facebook window using the "selectWindow" command and its title as the locator
  • Verify the title of the new window
  • Select back the original window using the "selectWindow" command and "null" as its target.
  • Verify the title of the currently selected window
Step 1. Set the Base URL to http://jsbin.com.
Step 2. Create the script as shown below.
We need the "pause" command to wait for the newly launched window to load before we could access its title.
Step 3. Execute the script. Notice that the test case passed, meaning that we were able to switch between windows and verify their titles successfully.
Always remember that setting selectWindow's target to "null" will automatically select the parent window (in this case, the window where the element "link=here" is found)

Summary

  • The three most commonly used commands in verifying page elements are:
    • verifyElementPresent/ verifyElementNotPresent
    • verifyTextPresent/ verifyTextNotPresent
    • verifyElementPositionLeft/ verifyElementPositionTop
  • Wait commands are classified into two:
    • andWait commands - used when a page is expected to be loaded
    • waitFor commands - used when no new page is expected to be loaded
  • The "store" command (and all its variants) are used to store variables in Selenium IDE
  • The "echo" command is used to print a string value or a variable
  • Variables are enclosed within a ${...} when being printed or used on elements
  • Selenium IDE automatically presses the OK button when handling alerts
  • When handling confirmation dialogs, you may instruct Selenium IDE which option to use:
    • chooseOkOnNextConfirmation/chooseOkOnNextConfirmationAndWait
    • chooseCancelOnNextConfirmation
  • Window titles are used as locators when switching between browser windows.
  • When using the "selectWindow" command, setting the Target to "null" will automatically direct Selenium IDE to select the parent window.

1 comment:

vishal kumar said...

It was very nice blog to learn about Selenium.Thanks for sharing new things
.selenium training in chennai