Selenium :After launching a website how could we check if the right page is launched

你离开我真会死。 提交于 2019-12-14 02:58:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!