简体   繁体   English

Selenium(WebDriver)Junit 4在Windows之间切换问题

[英]Selenium (WebDriver) Junit 4 switching between windows issue

I am testing a web application that creates a new window long after a button is clicked. 我正在测试一个Web应用程序,该应用程序会在单击按钮很长时间之后创建一个新窗口。 The sequence is the following 顺序如下

window 1: (parent window) click button to create window 2 窗口1 :(父窗口)单击按钮创建窗口2

window 2: progress window appears until background process on server returns data 窗口2:显示进度窗口,直到服务器上的后台进程返回数据

window 3: progress window turns into 3rd window (with different handle) 窗口3:进度窗口变为第三窗口(具有不同的句柄)

I want to properly wait for the 3rd window to appear. 我想适当地等待第三个窗口出现。 I know what the 'title' of all 3 windows will be however in order to get the titles from WebDriver I have to use the following code: 我知道所有3个窗口的“标题”是什么,为了从WebDriver获取标题,我必须使用以下代码:

while(timeout has not occured...){ 
    for (String handle : _driver.getWindowHandles()) {
       String myTitle = driver.switchTo().window(handle).getTitle();
       if(3rdWindowTitle.equalsIgnoreCase(myTitle)){
           return true;
       }
    }
}

This will effectively switch the active window back and forth every time it loops because of the 'switchTo'. 由于“ switchTo”,这将在每次循环时有效地来回切换活动窗口。 This causes the firefox windows to cycle back and forth really quickly and is obnoxious. 这导致Firefox窗口真正快速地来回循环并且令人讨厌。 What I need is a way to get the title's of the windows that are available without having to 'switchTo' each window in a loop waiting for the 3rd window. 我需要的是一种获取可用窗口标题的方法,而不必在等待第三个窗口的循环中“切换到”每个窗口。 Any ideas? 有任何想法吗?

I basically want a method (waitForWindowByTitle(titleIWant)) which will block until the window with the title I want appears. 我基本上想要一个方法(waitForWindowByTitle(titleIWant)),该方法将一直阻塞直到出现带有我想要的标题的窗口。

Well, Better you can wait for your window to appear by checking the number of windows. 好吧,更好的是,您可以通过检查窗口数来等待窗口出现。 Like: 喜欢:

for(int i=0; i<noOfTrials;i++){
        noOfWindows = driver.getWindowHandles().size();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        if(noOfWindows>currentNoOfWindows){
        break;
        }
    }

}

then for the first and last time you can browse through the windows (using switchTo) and navigate to the window you want. 然后,第一次和最后一次,您可以浏览窗口(使用switchTo)并导航到所需的窗口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM