首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Selenium :如何处理自动关闭并返回父窗口的窗口&打开PopUp窗口

Selenium :如何处理自动关闭并返回父窗口的窗口&打开PopUp窗口
EN

Stack Exchange QA用户
提问于 2019-04-29 10:27:41
回答 2查看 4.2K关注 0票数 0

我有一个具有以下场景的测试用例:

  1. 点击全局订单Mgmt超级用户
  2. 点击销售订单链接
  3. 它实际上打开了新窗口并自动关闭。
  4. 焦点回到父窗口&打开一个窗口弹出
  5. 单击Ok按钮

代码:

代码语言:javascript
运行
复制
// 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();     

错误:

代码语言:javascript
运行
复制
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
EN

回答 2

Stack Exchange QA用户

回答已采纳

发布于 2019-04-29 11:29:48

希望这能帮到你:

代码语言:javascript
运行
复制
    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();  

我只是根据你的测试步骤写了这篇文章。

票数 0
EN

Stack Exchange QA用户

发布于 2019-04-29 11:32:07

由于您似乎没有在新打开的窗口上执行任何操作,而且正如您说的那样,它会自动关闭,所以我只关注警报,因为这是异常发生的地方。

移除本部分:

代码语言:javascript
运行
复制
Set <String> handles =driver.getWindowHandles();
Iterator<String> it = handles.iterator();

String parent = it.next();
String child = it.next();

driver.switchTo().defaultContent();

并添加一个FluentWait

代码语言:javascript
运行
复制
FluentWait<WebDriver> fluentWait = new FluentWait<>(webDriver)
        .pollingEvery(Duration.ofMillis(500))
        .withTimeout(Duration.ofSeconds(10))
        .ignoring(NoAlertPresentException.class);
fluentWait.until(ExpectedConditions.alertIsPresent()).accept();

请记住,可以以这种方式处理的警报是默认浏览器警报。如果您提到的弹出是特定于您的应用程序,则必须像其他任何WebElement一样处理它(FluentWait仍然可以用来等待它,但代码可能有所不同)。

票数 0
EN
页面原文内容由Stack Exchange QA提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://sqa.stackexchange.com/questions/38957

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档