Introduction to driver.getPageSource() and perform assertion on text present on web page
Might be most of you would have used it in your day to day scripting or if not then don’t worry I am trying to make it chew-able that would finally get digested well.
People who has started learning Selenium would be familiar with Selenium RC and hoping would be familiar with Selenium IDE. In RC and IDE we have option to validate or verify any text on the page by using methods like
or in IDE we normally select the text and by right clicking we normally add assert or verify method for specific text. But Selenium 2(WebDriver) don’t have any direct method to find the text and also don’t have any assertion associated with it. But still we have many thing to work with.
there is one method in Selenium2 getPageSource(), it fetch all the source of a specific webpage and that can be user to verify the text on the page
Suppose there is one webpage where there is one string “Selenium is Browser automation tool”.
Here we can find it two way
assertEquals("Selenium is Browser automation tool", driver.findElement(By.xpath("Xpath of the element")).getText()).
By using this we would be able to find that text is available or not if it is not then script will fail here otherwise it will get passed
This is most popular one to fulfill our requirement. but the result that come is in Boolean
Boolean b = driver.getPageSource().contains("Selenium is Browser automation tool").
it’s result will appear in true or false and this is further used to implement some condition while writing scripts.
Main use of this Method
1- To find the text on the page
2- to find the pop up by some text on it.
In coming post i will show you how we handle the pop-up by using text on it.