问题
In Selenium :After launching a website how could we check if the right page is launched or not.
Example:
If I want to launch www.google.com
,after running the code how do I check if the same page has been launched.
I used,
Assert.assertEquals("Correct web page",driver.findElement(By.Xpath("<xpath of one of the element in the page>")).isDisplayed ());
After running the program, I got the below error:
Exception in thread "main" java.lang.AssertionError: expected:<Correct web page> but was:<true>
回答1:
There are many ways to assert for correct page loaded, most used are the assert for correct url loaded and page title.
Assert for Correct URL Loaded:
String expectedUrl = "https://www.google.com";
WebDriver driver = new FirefoxDriver();
driver.get(expectedUrl);
try{
Assert.assertEquals(expectedUrl, driver.getCurrentUrl());
System.out.println("Navigated to correct webpage");
}
catch(Throwable pageNavigationError){
System.out.println("Didn't navigate to correct webpage");
}
Assert for page title:
String expectedTitle = "Google";
String expectedUrl = "https://www.google.com";
WebDriver driver = new FirefoxDriver();
driver.get(expectedUrl);
try{
Assert.assertEquals(expectedTitle, driver.getTitle());
System.out.println("Navigated to correct webpage");
}
catch(Throwable pageNavigationError){
System.out.println("Didn't navigate to correct webpage");
}
来源:https://stackoverflow.com/questions/24777134/selenium-after-launching-a-website-how-could-we-check-if-the-right-page-is-laun