Verifying Page Elements
Verifying UI elements on a web page is probably the most common feature of your automated tests.
Selenese allows multiple ways of checking for UI elements. It is important that you understand these different methods because these methods define what you are actually testing.
For example, will you test that...
1. an element is present somewhere on the page?
2. specific text is somewhere on the page?
3. specific text is at a specific location on the page?
For example, if you are testing a text heading, the text and its position at the top of the page are probably relevant for your test. If, however, you are testing for the existence of an image on the home page, and the web designers frequently change the specific image file along with its position on the page, then you only want to test that an image (as opposed to the specific image file) exists somewhere on the page.
Selenese allows multiple ways of checking for UI elements. It is important that you understand these different methods because these methods define what you are actually testing.
For example, will you test that...
1. an element is present somewhere on the page?
2. specific text is somewhere on the page?
3. specific text is at a specific location on the page?
For example, if you are testing a text heading, the text and its position at the top of the page are probably relevant for your test. If, however, you are testing for the existence of an image on the home page, and the web designers frequently change the specific image file along with its position on the page, then you only want to test that an image (as opposed to the specific image file) exists somewhere on the page.
=> Assertion or Verification?
Choosing between assert and verify comes down to convenience and management of failures. There’s very little point checking that the first paragraph on the page is the correct one if your test has already failed when checking that the browser is displaying the expected page. If you’re not on the correct page, you’ll probably want to abort your test case so that you can investigate the cause and fix the issue(s) promptly. On the other hand, you may want to check many attributes of a page without aborting the test case on the first failure as this will allow you to review all failures on the page and take the appropriate action. Effectively an assert will fail the test and abort the current test case, whereas a verify will fail the test and continue to run the test case.
The best use of this feature is to logically group your test commands, and start each group with an assert followed by one or more verify test commands. An example follows:
The best use of this feature is to logically group your test commands, and start each group with an assert followed by one or more verify test commands. An example follows:
The above example first opens a page and then asserts that the correct page is loaded by comparing the title with the expected value. Only if this passes will the following command run and verify that the text is present in the expected location. The test case then asserts the first column in the second row of the first table contains the expected value, and only if this passed will the remaining cells in that row be verified.
=> verifyTextPresent
The command verifyTextPresent is used to verify specific text exists somewhere on the page. It takes a single argument–the text pattern to be verified.
For example:
verifyTextPresent / Marketing Analysis
This would cause Selenium to search for, and verify, that the text string “Marketing Analysis” appears somewhere on the page currently being tested. Use verifyTextPresent when you are interested in only the text itself being present on the page. Do not use this when you also need to test where the text occurs on the page.
verifyTextPresent / Marketing Analysis
This would cause Selenium to search for, and verify, that the text string “Marketing Analysis” appears somewhere on the page currently being tested. Use verifyTextPresent when you are interested in only the text itself being present on the page. Do not use this when you also need to test where the text occurs on the page.
=> verifyElementPresent
Use this command when you must test for the presence of a specific UI element, rather then its content. This verification does not check the text, only the HTML tag. One common use is to check for the presence of an image.
verifyElementPresent | //div/p/img
This command verifies that an image, specified by the existence of an <img> HTML tag, is present on the page, and that it follows a <div> tag and a <p> tag. The first (and only) parameter is a locator for telling the Selenese command how to find the element. Locators are explained in the next section. verifyElementPresent can be used to check the existence of any HTML tag within the page. One can check the existence of links, paragraphs, divisions <div>, etc.
verifyElementPresent | //div/p/img
This command verifies that an image, specified by the existence of an <img> HTML tag, is present on the page, and that it follows a <div> tag and a <p> tag. The first (and only) parameter is a locator for telling the Selenese command how to find the element. Locators are explained in the next section. verifyElementPresent can be used to check the existence of any HTML tag within the page. One can check the existence of links, paragraphs, divisions <div>, etc.
Here are a few more examples.
These examples illustrate the variety of ways a UI element may be tested. Again, locators are explained in the next section.
=> verifyText
Use verifyText when both the text and its UI element must be tested. verifyText must use a locator. If one chooses an XPath or DOM locator, one can verify that specific text appears at a specific location on the page relative to other UI components on the page.
verifyText | //table/tr/td/div/p | This is my text and it occurs right after the div inside the table.
verifyText | //table/tr/td/div/p | This is my text and it occurs right after the div inside the table.


No comments:
Post a Comment