6

I'm logging into the page using this function:

    public static void login() {
       driver.get(url);
       driver.findElement(By.name("username")).clear();
       driver.findElement(By.name("username")).sendKeys("username- login");
       driver.findElement(By.name("password")).clear();
       driver.findElement(By.name("password")).sendKeys("password");
       driver.findElement(By.name("submit")).click();
       driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
   }

The login is successful and now i'd like to verify this by checking for certain elements on the landing page as follows:

    public static void testLogin(WebDriver driver) {
       String pageSource = driver.getPageSource();
       //Assert.assertTrue("Login was not successful!", pageSource.contains("Welcome to the page!"));
       System.out.println(pageSource);
}

Everything works except for the fact that when i display the source code on the console, it is the source code of the login page (previous) and i'm hence the Assert is false.

Can anyone point me in the right direction?

Edit

I have accepted the answer from the_coder because it led me to this solution: I used WebDriverWait to wait until a specific element is present on the page before saving the source code into a String.

 WebDriverWait wait = new WebDriverWait(driver, 10);  
 wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[contains(.,'Welcome to the page!')]")));
  • welcome to SQA! I'm not exactly sure what your question is. Are you asking why does your console print the previous pages code? Maybe you could post the console print? – Chris Kenst Jan 19 '17 at 17:14
  • I'd like to verify that my login is successful by checking for the presence of certain elements on the landing page. I'm checking for this elements by storing the source code of the landing page in a string and looking if the string contains "Welcome to the page!", which is supposed to be shown upon successful login. – Anthony Okoth Jan 20 '17 at 7:45
  • 1
    Yes, I think the wait is the best solution, always wait for key elements to show or hide (e.g. loader indicators). – Niels van Reijmersdal Jan 20 '17 at 8:25
  • import time give it a pause in between. – Anshul Mishra Mar 1 at 6:57
7

You may be getting the previous page source code because the getPageSource() method is called before the page arrives to the page you want.

One thing I would like to suggest is before getting the pagesource of the webpage check whether you have navigated to the correct page like:

public static void testLogin(WebDriver driver) {
     if(driver.getCurrentUrl().equals("<expectedPageURL>"))
     {
       String pageSource = driver.getPageSource();
       Assert.assertTrue("Login was not successful!", pageSource.contains("Welcome to the page!"));
       System.out.println(pageSource);
     }
     else
     {
         System.out.println("Not navigated to the correct page");
     }
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.