Webdriver showModalDialog

发布于 2024-10-04 13:51:29 字数 488 浏览 5 评论 0原文

我们使用 webdriver 进行功能测试。但是我们的应用程序大量使用 showModalDialog JS 函数来打开弹出窗口。当我们尝试使用 webdriver 测试此功能时,它从打开弹出窗口的那一刻起就挂起。

我们尝试了多种方法来测试这一点:

我们如何测试或解决这个问题?

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 have to use but have no control over.

How can we test this or work around this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

椵侞 2024-10-11 13:51:29

根据我使用各种自动化工具的经验,使用 window.showModalDialog()window.showModelessDialog() 从 IE 打开的“网页对话框”窗口的交互不可用< /强>。

由于该窗口不是“真正的”窗口(查看任务栏,它甚至没有显示),因此大多数工具无法“检查”它和/或与其交互。

但是,如果您确实找到了可以的工具,请告知 - 有很多人在寻找这样的工具。

总而言之,如果您可以避免使用这两种专有方法中的任何一种,您将拥有更多的运气。

(是的,对于挑剔的人来说,Firefox 和 Chrome 已经采用了这些类型对话框,但它们的工作方式并不完全相同)

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)

喜你已久 2024-10-11 13:51:29

没有一个答案能回答问题。如果驱动程序挂起,则您无法调用其上的任何方法。问题不在于找到弹出窗口,而在于如何阻止驱动程序挂起。我发现的唯一方法是不使用 showModalDialog。这可以通过将以下代码添加到您的测试代码中来完成:

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

每次 JavaScript 调用 window.showModalDialog 时,该代码都会调用 window.open

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.

笑咖 2024-10-11 13:51:29

我正在使用 webdriver.SwitchTo().Window() 方法,但我担心我的弹出窗口没有“名称”,

当我使用 webdriver.WindowHandles 时,它只返回一个句柄,我在弹出窗口打开后使用此语句。

由于我没有名称/句柄,我无法从父窗口切换到子窗口。

执行相同功能的任何其他解决方案

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

魂归处 2024-10-11 13:51:29

首先我们必须切换到活动元素:

driver.switchTo().activeElement();

检查我们是否确实切换到了正确的活动元素:

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

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(); 
回忆躺在深渊里 2024-10-11 13:51:29

即使窗口没有名称,您也可以使用

driver.switchTo.defaultcontent();

并执行您想要执行的操作
否则您可以使用以下命令获取窗口句柄名称

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

希望这对您有用。

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.

聊慰 2024-10-11 13:51:29

问题 284 适用于 WebDriver。看来只有在Issue 27发布后才会实施已实施,因此修复应该在 WebDriver 的 Beta 1 或 2 中进行。

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.

南冥有猫 2024-10-11 13:51:29
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]);
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]);
萌吟 2024-10-11 13:51:29

我一直在使用的方法是通过弹出窗口,它对 IE 和 Firefox 非常有用
并在您尝试与之交互的弹出窗口中查找独特的文本。这是方法,请告诉我它是否适合您。请注意线路 driver = driver.switchTo().window(windowHandle);

public void switchWindow(String containsText, WebDriver driver) 抛出异常 {

    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");

}

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");

}
岛歌少女 2024-10-11 13:51:29

据我所知,到目前为止,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);
}

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);
}
围归者 2024-10-11 13:51:29

休·福斯特的解决方案有效,我尝试过并成功了

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

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

((JavascriptExecutor) driver).executeScript("window.showModalDialog = window.open;");
黎歌 2024-10-11 13:51:29
  • 您可以找到模式对话框的 url,然后在另一个选项卡上打开它,它将正常工作。
  • 想处理打开的模式对话框,您可以尝试发送“tab”键来移动对象,并发送“发送键... Enter”来设置文本或单击。

  • 注意:以下是您无法使用 selenium webdriver 处理该模式的一些信息。

模态弹出窗口 - 这是 IE 特有的,Microsoft 将其定义为

当 Windows Internet Explorer 通过使用 showModalDialog 方法或使用 showModelessDialog 方法从模态或非模态 HTML 对话框打开窗口时,Internet Explorer 使用组件对象模型 ( COM)来创建窗口的新实例。通常,该窗口是使用现有 Internet Explorer 进程的第一个实例打开的。当 Internet Explorer 在新进程中打开窗口时,所有内存 cookie 都不再可用,包括会话 ID。此过程与 Internet Explorer 使用 open 方法打开新窗口的过程不同。
http://msdn.microsoft.com/en-us /library/ms536759(VS.85).aspx

关于模态对话框的 MSDN 博客

当用户选择“模型”弹出窗口时,父窗口将被阻塞,等待弹出窗口的返回值。您将无法看到页面的查看源,需要关闭弹出窗口然后仅激活父窗口。

  • 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文