且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

硒:启动网站后,我们如何检查是否启动了正确的页面

更新时间:2023-12-04 18:07:28

有许多方法可以断言正确的页面加载,最常用的是断言正确的URL和页面标题.

There are many ways to assert for correct page loaded, most used are the assert for correct url loaded and page title.

为正确的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");
}