Introduction to driver.getPageSource()

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

selenium.isTextPresent();

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

1st Method
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

Method 2:

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 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.

 

This entry was posted in Selenium WebDriver and tagged , , , , , , by Dwarika Dhish Mishra. Bookmark the permalink.

About Dwarika Dhish Mishra

My name is Dwarika Dhish Mishra, its just my name and I am trying to bring the worth of my name in to actions and wants to be the solution not the problem. I believe in spreading knowledge and happiness. More over I am fun loving person and like travelling a lot. By nature I am a tester and a solution maker. I believe in the tag line of http://ted.org “Idea worth spreading” . For the same, I have created this blog to bring more and more learning to tester fraternity through day to day learning in professional and personal life. All contents are the part of my learning and so are available for all..So please spread the contents as much as you can at your end so that it could reach to every needful people in testing fraternity. I am pretty happy that more and more people are showing interest to become the part your Abode QA blog and I think this is good sign for us all because more and more content would be before you to read and to cherish. You may write or call me at my Email id: dwarika1987@gmail.com Cell: 9999978609

13 thoughts on “Introduction to driver.getPageSource()

  1. Hi,

    This is a very nice post and very interesting. i have some queries..I am requesting you to answer them.

    1. How to continue the test execution after a failed test. [ like if i have 10 things to test and if 5th one is a fail, selenium comes out of it !!!]
    2. is it possible to reduce the execution speed in web driver?
    3. How to change the windows handler in C#?
    4. is it possible to carry out the test execution in multiple browsers [ like from test 1 to 10, 1st all in IE, then in chrome etc. ]

    Thanks in Advance,

    Sudhir Bhat

    • Thank you commenting with such load of many useful questions.
      I am trying to answer your all question in nutshell.

      1- If you really want to execute your script without stopping execution of script than why not you are trying try and catch like this

      Suppose you are trying to assert some element on webpage and due to some reason elements are not get loaded on time and due to this assertion get failed than

      try
      {
      assertTrue(driver.findElement(By.xpath(“xpath of your element”)).isDisplayed())
      }
      catch(Exception e)
      {
      e.printStackTrace();
      }

      so in this case if your assertion get failed than it will show the exception message and script will continue its execution

      2- As such in WebDriver there is no such methods like that of Selenium RC …but if you want to slow down the speed of execution than why not you are trying
      Thread.sleep();
      or driver.wait(40000); here time is in milisecond
      or if you really want to wait for some element or some time for the script than try
      Implicit wait
      to read more about implicit wait try this link

      http://abodeqa.wordpress.com/2013/03/12/implicit-wait-in-webdriver/

      3- Since I am familiar with Java so I am just showing you the methodology to handle it in java might be you will learn it and reciprocate the same in yourC# script

      There is one simple way
      a) by using name (keep all window handling in Try and Catch)

      String parentwindow= driver.getWindowHandle();

      by using this you would be able to handle the parent window

      now its time to handle the pop-up

      try
      {
      driver.switchTo().window(“Name of the window”);

      }
      catch(NoSuchWindowException e) {
      e.printStackTrace();
      }

      again if you want to switch back to parent than
      in place of the name of window keep parentwindow.

      b) Second method is to fill all pop-up in a list/set like this

      Set allWindows = driver.getWindowHandles();
      if(!allWindows.isEmpty()) {
      for (String windowId : allWindows) {
      do the requsite action
      }

      4) Please go through my post http://abodeqa.wordpress.com/2013/02/21/how-to-execute-selenium-webdriver-test-cases-parallel-in-multiple-browser-using-testng-parameter-annotation/

      this will answer your fourth query.

      for more question please write me at dwarika1987@gmail.com

  2. Hello ,

    for your reply to his first question,
    => if i use try catch block, the test execution is continued but, the test passes , it doesnt fail.

    now how to handle this, it shud continue execution even if it fails and also the test shud get failed( in reports).

    • Actually in above code I have forget to mention the failed message

      like this in try
      {
      driver.switchTo().window(“Name of the window”);

      }
      catch(NoSuchWindowException e) {
      e.printStackTrace();
      // to find the notification of failed test
      System.out.println(“You test failed and check the code”)
      }

      Above is the one option to find the message related to Failed Test Case

      or for better understanding you may use this code, In this we would write one function in which we will pass two parameter
      public class Verification extends SeleneseTestBase{

      private StringBuffer verificationerrors;

      public CustomVerification()
      {
      verificationErrors = new StringBuffer();
      }

      public void verifyTrue(String msg, driver.findElement(By.xpath(“xpath of certain element”)).isDisplayed())
      {
      try
      {
      Assert.assertTrue(driver.findElement(By.xpath(“xpath of certain element”)).isDisplayed());
      }
      catch(Error e)
      {
      verificationErrors.append(e);
      Reporter.log(msg + “
      “);

      }

      }

      Use above function in your coming test..Might be this code will help you in getting better understanding in handling such cases

  3. Pingback: WebDriver’s most popular Commands | Abode QA

  4. Pingback: Handling Multiple Windows in WebDriver | Abode QA

  5. Hi Dhwarika..
    Iam new to selenium web driver.Your providing excellent information i’ve to appreciate for that.I’ve some queries.
    1). How to handle alerts(how to write script for clicking ok/cancel button in the alert , do i need to use if-else block to handle those actions).
    2). My page is having 20links with same class name for div and span for all the links.how to click a particular link.could you provide the logic for that

  6. Hi Renuka,
    You can click on link using
    driver.findElement(By.linkText(“Just give the name of the link”)).click(); irrespective of class, div or span
    ex: driver.findElement(By.linkText(“Gmail”)).click();

    @Dhwarika.
    Thanks..

  7. Hi Dhwarika..

    How can i wait for text on page using implicit wait?
    In my case I am waiting for a text to be appear on the page and i am using
    driver.getPageSource().contains(element);

    here element is text on the page. As i am testing 1000 of applications I dont want to go with
    new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(By.id/linktext/ect(element)));

    any other tricks ?

    • @Suresh

      We have other options too like you can use Thread.sleep(time in milisecond). But Explicit wait is really good synchronization method and this really justify its significance becuase suppose your element take 10 second and you have given something like 5 second as wait then most probably your script is going to fail and in second case if you have given 15 second as wait then you are wasting 5 second because your element is taking just 10 second(Even this is very big time for any prompt site).

      So Explicit wait gives a way to handle such problem because it will wait for the same time as element is taking to load in to DOM.

      So i would suggest use explicit wait that you have written here as line of code but if you dont want to use this then you have other option to use Thread.sleep(time).

      Hope I have answered your query.

  8. Hi,

    Really nice article, very informative for me.

    I want to ask one thing at this point, how can we execute test script from my java project.
    Suppose, if there is a button in my project, then how can I execute test script on click of that button inside my java project?

    Let me know, if anything not clear in my question.

    Thanks in advance.

    Thannks,
    Sumeet.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>