我有一个具有以下场景的测试用例:
// Click on Global Order MGMT Superuser role & Sales Order
driver.findElement(By.xpath("//a[contains(text(),'Global Order Management Super User')]")).click();
driver.findElement(By.xpath("//*[@id=\"N104\"]")).click();
Set <String> handles =driver.getWindowHandles();
Iterator<String> it = handles.iterator();
String parent = it.next();
String child = it.next();
driver.switchTo().defaultContent();
Alert alert = driver.switchTo().alert();
alert.accept();
FAILED: testCase1
org.openqa.selenium.NoAlertPresentException:
Build info: version: '3.14.0', revision: 'aacccce0', time: '2018-08-02T20:05:20.749Z'
System info: host: 'ADONGAR-LAP', ip: '10.180.179.18', os.name: 'Windows 10', os.arch: 'x86', os.version: '10.0', java.version: '1.8.0_201'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities {acceptInsecureCerts: true, browserName: firefox, browserVersion: 64.0, capabilities: {desiredCapabilities: {acceptInsecureCerts: true, browserName: firefox}}, javascriptEnabled: true, moz:accessibilityChecks: false, moz:headless: false, moz:processID: 23748, moz:profile: C:\Users\adongar\AppData\Lo..., moz:shutdownTimeout: 60000, moz:useNonSpecCompliantPointerOrigin: false, moz:webdriverClick: true, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, platformVersion: 10.0, rotatable: false, setWindowRect: true, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
Session ID: 7e3fd1a5-32e0-4fb5-b83c-5f46f14ee28a
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createEx
发布于 2019-04-29 11:29:48
希望这能帮到你:
driver.findElement(By.xpath("//a[contains(text(),'Global Order Management Super User')]")).click();
driver.findElement(By.xpath("//*[@id=\"N104\"]")).click();
// It will return the parent window name as a String
String mainWindow=driver.getWindowHandle();
// It returns no. of windows opened by WebDriver and will return Set of Strings
Set<String> set =driver.getWindowHandles();
// Using Iterator to iterate with in windows
Iterator<String> itr= set.iterator();
while(itr.hasNext()){
String childWindow=itr.next();
// Compare whether the main windows is not equal to child window. If not equal, we will close.
if(!mainWindow.equals(childWindow)){
driver.switchTo().window(childWindow);
// If you just want to print or match the title of new opened window.
System.out.println(driver.switchTo().window(childWindow).getTitle());
}
}
// This is to switch to the main window
driver.switchTo().window(mainWindow);
// Wait for 10 seconds to find the Alert on parent window.
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.alertIsPresent());
driver.switchTo().alert().accept();
我只是根据你的测试步骤写了这篇文章。
发布于 2019-04-29 11:32:07
由于您似乎没有在新打开的窗口上执行任何操作,而且正如您说的那样,它会自动关闭,所以我只关注警报,因为这是异常发生的地方。
移除本部分:
Set <String> handles =driver.getWindowHandles();
Iterator<String> it = handles.iterator();
String parent = it.next();
String child = it.next();
driver.switchTo().defaultContent();
并添加一个FluentWait
:
FluentWait<WebDriver> fluentWait = new FluentWait<>(webDriver)
.pollingEvery(Duration.ofMillis(500))
.withTimeout(Duration.ofSeconds(10))
.ignoring(NoAlertPresentException.class);
fluentWait.until(ExpectedConditions.alertIsPresent()).accept();
请记住,可以以这种方式处理的警报是默认浏览器警报。如果您提到的弹出是特定于您的应用程序,则必须像其他任何WebElement一样处理它(FluentWait
仍然可以用来等待它,但代码可能有所不同)。
https://sqa.stackexchange.com/questions/38957
复制相似问题