开发者

Webdriver showModalDialog

We are using webdriver for our functional tests. But our application uses the showModalDialog JS function a lot to open a popup. When we try to test this functionality with webdriver it hangs from the moment the popup is opened.

We tried several things to test this:

  • Using the workaround explained here. But this seems to be a fix for selenium and not for webdriver. We tried it but it didn't work.
  • Searching for a good alternative, HtmlUnit opened the modal dialog and could interact with it, but it has it's drawbacks like no visual help to fix certain tests and it stopped execution when it detected a JS error in a JS library we hav开发者_如何转开发e to use but have no control over.

How can we test this or work around this problem?


From my experiences with various automation tools interaction with "webpage dialog" windows opened from IE using window.showModalDialog() or window.showModelessDialog() is not available.

Since the window is not a "true" window (look at the taskbar, it doesn't even show up) most tools can't "inspect" it and/or interact with it.

However if you do find a tool that will, please advise - there are many people looking for such a beast.

That all said, if you can possibly avoid using either of these 2 proprietary methods you'll have much more luck.

(and yes, for the picky ones, Firefox and Chrome have adopted these kind of dialogs but they don't work quite the same)


None of the answers answer the question. If the driver hangs, then you can't call any methods on it. The question is NOT about finding the pop up, it is about how to stop the driver hanging. The only way I have found is to not use showModalDialog. This can be done by adding the folowing to your test code :

((JavascriptExecutor) driver).executeScript("window.showModalDialog = window.open;");

which calls window.open each time your JavaScript calls window.showModalDialog.


I am using webdriver.SwitchTo().Window() method but my concern is my popup window does not have "Name"

When I use webdriver.WindowHandles it return only one handle, I am using this statement after popup window open.

As I don't have name / handle I cannot switch from parent window to child window.

Any other solution to do the same functionality


First we have to switch to the active element:

driver.switchTo().activeElement();

To check whether we have actually switched to the correct active element:

driver.switchTo().activeElement().getText(); 


Even if the window doesn't have name u can use

driver.switchTo.defaultcontent();

and perform the operation you want to execute or else you can get the window handle name using the below command

for (String handle : driver.getWindowHandles()) { 
    driver.switchTo().window(handle); }

hope this should work for you.


Issue 284 is for WebDriver. It seems that it will be implemented only after Issue 27 will be implemented, so the fix should be in Beta 1 or 2 of WebDriver.


Set<String> beforePopup = driver.getWindowHandles();

Set<String> afterPopup = driver.getWindowHandles();

afterPopup.removeAll(beforePopup);

if(afterPopup.size()==1){
    System.out.println(afterPopup.toArray()[0]);
}

driver.switchTo().window((String) afterPopup.toArray()[0]);


What I have been using and it works great for us on with IE and Firefox is to go through popups and look for a a unique text on the popup you are trying to interact with. Here is the method, let me know if it works for you. Please note the line driver = driver.switchTo().window(windowHandle);

public void switchWindow(String containingText, WebDriver driver) throws Exception {

    if ( StringUtils.isEmpty(containingText))
        return;

    int counter = 1;
    int numOfpopups = driver.getWindowHandles().size();
    System.out.println("Waiting for popup to load..... # handles:" + numOfpopups);
    while ( numOfpopups  < 2 && ((counter%10) != 0) ) {
        counter++;
        try{Thread.sleep(1000);}catch (Exception e) {}
    }
    System.out.println("Done waiting for..... " + counter + " seconds");

    if (driver.getWindowHandles().size() < 2)
          throw new BrowserException("Timeout after " + counter + " secs. No popup present. ");

    System.out.println("Going through window handles...");

    for (String windowHandle : driver.getWindowHandles()) { 
                driver = driver.switchTo().window(windowHandle);
        if ( driver.getPageSource().contains(containingText) 
        return;
         else 
        continue;
    }

    throw new Exception("Window containing text '" + containingText + "' not found");

}


To my knowledge, webdriver has no built-in functionality to handle modal windows as of now. Webdriver will hang once you click button which opens modal window. This happens due to JS on parent window halts until child window is closed.

To handle modal windows such as this one, see below for possible workaround written in Java. The main idea is to perform action that opens modal window (click on the button) in new thread.

/**
 * Click button to open modal window and switch to it
 * @param we webElement handle of a button
 */
public void clickToOpenModal(final WebElement we) {
    //Get handles of all opened windows before opening modal window
    Set<String> initWindowHandles = getDriverInstance().getWindowHandles();

    //Create new thread and click button to open window
    Thread thread1 = new Thread() {
            @Override
            public void run() {
            //Click button
            click(we);
        }
    };
    thread1.start();

    //Wait for window to appear
    waitForWindow(initWindowHandles, pauseL);
    thread1.interrupt();
    thread1 = null;

    //Get handles of all opened windows after opening modal window
    Iterator<String> it = getDriverInstance().getWindowHandles().iterator();

    //Select handle of modal window
    String windowHandle = "";
    while(it.hasNext()){
        windowHandle = it.next();
    }

    //Switch focus and work on the modal window
    getDriverInstance().switchTo().window(windowHandle);
}


The solution by Hugh Foster works, i tried this and succeeded

((JavascriptExecutor) driver).executeScript("window.showModalDialog = window.open;");


  • You can find the url of modal dialog then open it on another tab, it will work as normal.
  • In case you want to deal with open modal dialog, you can try to send "tab" key for move around objects and "send keys... enter" for setText or click.

  • Note: Below is some information why you cannot use selenium webdriver for work with that modal.

Modal pop-up - This is very specific to IE, Microsoft defined it as

When Windows Internet Explorer opens a window from a modal or modeless HTML dialog box by using the showModalDialog method or by using the showModelessDialog method, Internet Explorer uses Component Object Model (COM) to create a new instance of the window. Typically, the window is opened by using the first instance of an existing Internet Explorer process. When Internet Explorer opens the window in a new process, all the memory cookies are no longer available, including the session ID. This process is different from the process that Internet Explorer uses to open a new window by using the open method. http://msdn.microsoft.com/en-us/library/ms536759(VS.85).aspx

MSDN blog on Modal dialog

When user select Model popup, parent window is blocked waiting for the return value from the popup window. You will be not able to see the view source of the page, need to close the popup then only the parent window is activated.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜