我写了一个硒测试,单击页面上的所有链接。但是我的弹出窗口关闭代码无法处理弹出窗口,并且测试已终止。
我使用的是Selenium Java V2.53.1,TestNG,后端是browserstack。
这是调用堆栈,在最后一页之后会出现弹出窗口,并且不会将其关闭!
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running TestSuite
link: /
link2: /
link2: /articles
link: /articles
link2: /
这是我的测试方法:
@Test
public void test_click_all_links() throws Exception {
String base_url = "https://infinite-taiga-25466.herokuapp.com";
driver.get(base_url);
//get all links with href that start with /
ArrayList<String> links = (ArrayList) ((JavascriptExecutor) driver).executeScript("return [...document.querySelectorAll(\"a[href^='/']\")].map(e=>e.getAttribute('href'))");
links.forEach(link->{
driver.get(base_url + link);
System.out.println("link: " + link);
//check here
try {
WebDriverWait wait = new WebDriverWait(driver, 5, 100);
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
// Prints text and closes alert
//System.out.println(alert.getText());
alert.dismiss();
} catch (NoAlertPresentException | TimeoutException ex) {
//do nothing
};
Assert.assertNotEquals(title(), "The page you were looking for doesn't exist.");
//get all sublinks with href that start with /
ArrayList<String> sublinks = (ArrayList) ((JavascriptExecutor) driver).executeScript("return [...document.querySelectorAll(\"a[href^='/']\")].map(e=>e.getAttribute('href'))");
sublinks.forEach(link2->{
driver.get(base_url + link2);
System.out.println("link2: " + link2);
//check here
try {
WebDriverWait wait = new WebDriverWait(driver, 5, 100);
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
// Prints text and closes alert
//System.out.println(alert.getText());
alert.dismiss();
} catch (NoAlertPresentException | TimeoutException ex) {
//do nothing
};
Assert.assertNotEquals(title(), "The page you were looking for doesn't exist.");
});
});
}
答案 0 :(得分:1)
没有清楚的了解您如何通过没有用户名/密码的身份验证,在这种情况下将无法打开页面,下面的代码将使用页面加载超时取消身份验证。
如何使用基本身份验证,您可以找到here。
private WebDriver driver;
private WebDriverWait wait;
private JavascriptExecutor js;
private String baseUrl = "https://infinite-taiga-25466.herokuapp.com";
@BeforeMethod
public void setUp() {
driver = new ChromeDriver();
wait = new WebDriverWait(driver, 5, 100);
js = (JavascriptExecutor) driver;
}
public void closeAlert() {
try {
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
alert.dismiss();
} catch (NoAlertPresentException | TimeoutException ignored) { }
}
@SuppressWarnings("unchecked")
public ArrayList<String> getLinks() {
return (ArrayList<String>) js.
executeScript("return [...document.querySelectorAll(\"a[href^='/']:not([href='/'])\")].map(e=>e.getAttribute('href'))");
}
@Test
public void clickAllLinks() {
driver.get(baseUrl);
ArrayList<String> links = getLinks();
links.forEach(link -> {
System.out.println("link: " + link);
driver.get(baseUrl + link);
closeAlert();
Assert.assertNotEquals(driver.getTitle(), "The page you were looking for doesn't exist.");
ArrayList<String> subLinks = getLinks();
subLinks.forEach(link2 -> {
System.out.println("link2: " + link2);
try {
driver.manage().timeouts().pageLoadTimeout(5, TimeUnit.SECONDS);
driver.get(baseUrl + link2);
} catch (Exception ignore) {
System.out.println("Cancel authorization popup");
} finally {
driver.manage().timeouts().pageLoadTimeout(15, TimeUnit.SECONDS);
}
// On page loading timeout, authentication closed automatically.
// No need
//closeAlert();
Assert.assertNotEquals(driver.getTitle(), "The page you were looking for doesn't exist.");
});
});
}