Webdriver showModalDialog

We use webdriver for our functional tests. But our application reuses the showModalDialogJS function to open a popup. When we try to test this functionality with webdriver, it freezes since the pop-up window pops up.

We tried a few things to check this out:

  • The use of a workaround is explained here . But this seems like a fix for selenium, not webdriver. We tried, but it didn’t work.
  • Finding a good alternative, HtmlUnit opened a modal dialog and could interact with it, but it has flaws, such as the lack of visual help to fix certain tests, and it stopped execution when it found a JS error in the JS library, which we should but have no control over them.

How can we verify this or solve this problem?

+3
source share
11 answers

From my experiences with various automation tools for interacting with the "web page dialog", windows opened from IE using window.showModalDialog()or window.showModelessDialog()are not available .

"" ( , ), "" / .

, , , , - .

, 2 , .

( , , Firefox Chrome , )

+2

. , - . , , , . , , - showModalDialog. , :

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

window.open , JavaScript window.showModalDialog.

+2

webdriver.SwitchTo().Window(), ""

webdriver.WindowHandles, , .

/, .

+1

,

driver.switchTo.defaultcontent();

, ,

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

, .

+1

:

driver.switchTo().activeElement();

, :

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

284 WebDriver. , 27 , Beta 1 2 WebDriver.

0
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]);
0

, , , IE Firefox - , . , , . = driver.switchTo(). Window (windowHandle);

public void switchWindow (String containsText, WebDriver) 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");

}
0

, webdriver . Webdriver , , . - JS , .

, , . , Java. - , ( ) .

/**
 * 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);
}
0

,

((JavascriptExecutor) driver).executeScript("window.showModalDialog = window.open;");
0
  • URL , , .
  • , "tab" "send keys... enter" setText .

  • . , - .

- IE, Microsoft

Windows Internet Explorer HTML showModalDialog showModelessDialog, Internet Explorer (COM) . , Internet Explorer. Internet Explorer , cookie , . , Internet Explorer open. http://msdn.microsoft.com/en-us/library/ms536759(VS.85).aspx

MSDN

When the user selects the Model pop-up window, the parent window is blocked waiting for the return value from the pop-up window. You will not be able to see the page view source, you must close the pop-up window, after which only the parent window is activated.

0
source

Source: https://habr.com/ru/post/1777474/


All Articles