Verify Element Present & waitFor Command in Selenium
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 UserName 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.
Verify Presence of a Certain Text in Command in Selenium
- 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, one passed while 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 in Selenium
The following are the types of wait commands in Selenium
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
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