Webdriver showModalDialog
我们使用 webdriver 进行功能测试。但是我们的应用程序大量使用 showModalDialog
JS 函数来打开弹出窗口。当我们尝试使用 webdriver 测试此功能时,它从打开弹出窗口的那一刻起就挂起。
我们尝试了多种方法来测试这一点:
- 使用此处。但这似乎是针对 selenium 的修复,而不是针对 webdriver 的修复。我们尝试过,但没有成功。
- 为了寻找一个好的替代方案,HtmlUnit 打开了模式对话框并可以与其交互,但它有其缺点,例如没有视觉帮助来修复某些测试,并且当它检测到我们必须使用的 JS 库中存在 JS 错误时,它会停止执行无法控制。
我们如何测试或解决这个问题?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
根据我使用各种自动化工具的经验,使用
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()
orwindow.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)
没有一个答案能回答问题。如果驱动程序挂起,则您无法调用其上的任何方法。问题不在于找到弹出窗口,而在于如何阻止驱动程序挂起。我发现的唯一方法是不使用
showModalDialog
。这可以通过将以下代码添加到您的测试代码中来完成:每次 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 :which calls
window.open
each time your JavaScript callswindow.showModalDialog
.我正在使用 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
首先我们必须切换到活动元素:
检查我们是否确实切换到了正确的活动元素:
First we have to switch to the active element:
To check whether we have actually switched to the correct active element:
即使窗口没有名称,您也可以使用
并执行您想要执行的操作
否则您可以使用以下命令获取窗口句柄名称
希望这对您有用。
Even if the window doesn't have name u can use
and perform the operation you want to execute
or else you can get the window handle name using the below command
hope this should work for you.
问题 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.
我一直在使用的方法是通过弹出窗口,它对 IE 和 Firefox 非常有用
并在您尝试与之交互的弹出窗口中查找独特的文本。这是方法,请告诉我它是否适合您。请注意线路 driver = driver.switchTo().window(windowHandle);
public void switchWindow(String containsText, WebDriver driver) 抛出异常 {
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 {
据我所知,到目前为止,webdriver 没有内置功能来处理模式窗口。一旦您单击打开模式窗口的按钮,Webdriver 将挂起。发生这种情况是因为父窗口上的 JS 会停止,直到子窗口关闭。
要处理诸如这个之类的模态窗口,请参阅下文以了解用 Java 编写的可能解决方法。主要思想是在新线程中执行打开模式窗口(单击按钮)的操作。
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.
休·福斯特的解决方案有效,我尝试过并成功了
The solution by Hugh Foster works, i tried this and succeeded
注意:以下是您无法使用 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 博客
当用户选择“模型”弹出窗口时,父窗口将被阻塞,等待弹出窗口的返回值。您将无法看到页面的查看源,需要关闭弹出窗口然后仅激活父窗口。
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.