Java 在Selenium中登录后处理弹出窗口

Java 在Selenium中登录后处理弹出窗口,java,selenium,Java,Selenium,我有以下场景的测试用例: 1) 导航到网站。 2) 输入登录凭证。 3) 点击登录 在我的应用程序登录后,我为最终用户弹出提示。我使用窗口处理程序来关闭它,但问题是有时候selenium运行得太快,以至于在它可见之前就点击了它。任何帮助都将不胜感激 Wait<WebDriver> wait = new FluentWait<WebDriver>(driver) .withTimeout(60, TimeUnit.SECONDS)

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

1) 导航到网站。
2) 输入登录凭证。
3) 点击登录

在我的应用程序登录后,我为最终用户弹出提示。我使用窗口处理程序来关闭它,但问题是有时候selenium运行得太快,以至于在它可见之前就点击了它。任何帮助都将不胜感激

Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
                .withTimeout(60, TimeUnit.SECONDS)
                .pollingEvery(2, TimeUnit.SECONDS)
                .ignoring(NoSuchElementException.class);
    String mainWindowHandle = driver.getWindowHandle();
            driver.switchTo().parentFrame();
            wait.until(ExpectedConditions.urlContains("client/default"));
            wait.until(new ExpectedCondition<Boolean>() {
                @Override
                public Boolean apply(WebDriver d) {
                    System.out.println(String.format("Window size is %d", d.getWindowHandles().size()));
                    return (d.getWindowHandles().size() == 1);
                }
            });

            for (String activeHandle : driver.getWindowHandles()) {
                if (!activeHandle.equals(mainWindowHandle)) {
                    driver.switchTo().window(activeHandle);
                }
            }

            driver.findElement(By.xpath(elementLocator("popup_close_button"))).click();
           driver.switchTo().window(mainWindowHandle);  // switch back to parent window
Wait Wait=new FluentWait(驱动程序)
.带超时(60,时间单位。秒)
.轮询间隔(2,时间单位。秒)
.忽略(NoSuchElementException.class);
字符串mainWindowHandle=driver.getWindowHandle();
driver.switchTo().parentFrame();
wait.until(ExpectedConditions.urlContains(“客户机/默认”);
等待.直到(新的ExpectedCondition(){
@凌驾
公共布尔应用(WebDriver d){
System.out.println(String.format(“窗口大小为%d”,d.getWindowHandles().size());
返回值(d.getWindowHandles().size()=1);
}
});
对于(字符串activeHandle:driver.getWindowHandles()){
如果(!activeHandle.equals(mainWindowHandle)){
driver.switchTo()窗口(activeHandle);
}
}
findElement(By.xpath(elementLocator(“弹出\关闭\按钮”)))。单击();
driver.switchTo().window(mainWindowHandle);//切换回父窗口
参考此代码(取自 并修改):


如果您想让它一直工作到弹出窗口出现,请添加一些代码,如下所示:

int flag=0,wait=0;

while(flag==0 && wait<60){
try{

String parentWindowHandler = driver.getWindowHandle(); // Store your parent window
String subWindowHandler = null;

Set<String> handles = driver.getWindowHandles(); // get all window handles
Iterator<String> iterator = handles.iterator();
while (iterator.hasNext()){
subWindowHandler = iterator.next();
}
driver.switchTo().window(subWindowHandler); // switch to popup window
                                        // perform operations on popup
driver.close();///close pop up

driver.switchTo().window(parentWindowHandler);  // switch back to parent window
 //statements executed successfully, now exit by flag set to 1
 flag=1;

}
catch(exception e){//wait for one second
driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
wait++;
}

if(wait==60){
JOptionPane.showMessageDialog(null,"Failed to detect pop up after 60 seconds."); 
}


}
int标志=0,等待=0;

虽然(flag==0&&wait在注释之后,有人指出关闭按钮不会出现在新窗口或选项卡中。相反,它是登录后的下一个页面。更新答案以利用预先存在的ExpectedConditions元素(希望)解决问题

 /**
  * Performs application Login.
  * @param driver WebDriver to act upon.
  */
  public void login(WebDriver driver) {
     //Perform Login function

     //1.  Identify the lookup we're going to use for the button.
     By closeButtonLookup = elementLocator("popup_close_button");

     //2.  Set up the Wait object with a reasonable timout value.
     WebDriverWait wait = new WebDriverWait(driver, 30);

     //3.  Try to resolve the close button (from the current page/window/tab) after Selenium believes it to be a viable click target.
     WebElement closeButton = wait.until(ExpectedConditions.elementToBeClickable(closeButtonLookup));
     System.out.println("Close button valid!  Proceeding to click!");

     //4.  Click the button
     closeButton.click();
     System.out.println("I clicked the close button!");

     //Continue program
 }

没有代码是无法帮助的。嗨,罗曼,我编辑了我的帖子。嗨,乔纳斯,我尝试了所有3种等待方式——隐式、显式和流畅的等待,但没有人为我感到苦恼。10次中有7次效果很好。但我需要10次才行。嗨,杰里米,谢谢你宝贵的帖子。我看到了你的代码,但问题是我总是感到不舒服窗口处理程序大小为“1”所以我面临着困难。如果你能帮我解决我的问题,那就太好了。谢谢你。从你最初的帖子中,我感觉登录后的额外确认元素是在一个新窗口中管理的。如果窗口计数从未增加,那么你可能正在处理一个警报。我在我的日志中添加了另一个方法建议考虑这种可能性。您能发布您在失败中看到的控制台输出吗?嗨,Jeremiah。这是应用程序中的引导提示模型弹出窗口。我认为问题是,当模型弹出窗口出现在dom中时,它会单击关闭按钮,但此时页面未完全加载,这导致了问题。是否有帮助在这里我很感激。这似乎与上面的说法相矛盾,即窗口句柄的大小从来都不大于1?我最初的回答应该保证关闭不会被解决,直到第二个窗口打开并在驱动程序中接收到焦点。在这种情况下,我不明白在页面被关闭之前,关闭会被点击多少次esolved。如果第二个窗口从未打开,windowCheckAndSwitch谓词应该抛出TimeoutException,它甚至不会尝试关闭行为。我担心我不理解问题的部分上下文。直接回答您的上述评论:“在页面完全加载之前单击关闭按钮[…]的问题”,请尝试以下操作。等待。直到(ExpectedConditions.visibilityOfElementLocatedBy(elementLocator(“popup_close_button”))。单击();这将强制按钮在返回单击之前可见。隐式等待与您认为的不同。它与Thread.sleep(1000)或WebDriverWait不同。