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!')]")));