在 Selenium 中:启动网站后,我们如何检查是否启动了正确的页面。
例子:
如果我想启动www.google.com
,运行代码后如何检查是否已启动同一页面。
I used,
Assert.assertEquals("Correct web page",driver.findElement(By.Xpath("<xpath of one of the element in the page>")).isDisplayed ());
运行程序后,我收到以下错误:
Exception in thread "main" java.lang.AssertionError: expected:<Correct web page> but was:<true>
有很多方法可以断言页面是否正确加载,最常用的是断言正确加载的 url 和页面标题。
断言已加载正确的 URL:
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");
}
断言页面标题:
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");
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)