使用 Watir 附加到 javascript 窗口时遇到问题 [英] Trouble attaching to a javascript window with Watir

查看:26
本文介绍了使用 Watir 附加到 javascript 窗口时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Watir 的新手,正在为我的工作开发测试工具.
我遇到了一个似乎无法解决的问题,即使检查了几个站点.

I am new to Watir, and am working on developing a testing tool for my work.
I have run into a problem that I cannot seem to solve, even after checking several sites.

javascript 窗口创建如下:(创建的窗口在窗口中保存一个 pdf,所以唯一的按钮"是最小化、最大化、关闭)

The javascript window creation is below: (the window created holds a pdf in a window, so the only "buttons" are the minimize, maximize, close)

<a id="LogIn_HyperLink2" class="ms-WPTitle" onclick="javascript:var win = new Window({className: 'spread', title: 'Security Statement', top:0, left:1, width:750, height:365, url:'--redacted--/security.pdf', showEffectOptions: {duration:1.0}}); win.setConstraint(true, {left:10, right:20}); win.showCenter(); return false;" href="--redacted--/security.pdf" style="color:#6699cc; font-weight:bold;">Security Statement</a><br>

我都试过使用

puts browser.modal_dialog(:title, "Security Statement").exists?
puts browser.javascript_dialog.exists?

两者都返回了'false'

both have returned 'false'

我应该采取什么方法附加到这个新窗口,或者更直接:如何关闭这个新窗口?

What approach should I be taking to attach to this new window, or more directly: How can I close this new window?

您可以在此链接(仅限 IE)

You can see the page at this link (IE only)

推荐答案

如果窗口包含 PDF 文件,则它是浏览器窗口,而不是模态 javascript 弹出窗口(警报、确认、提示)

If the window holds a PDF file it's a browser window, not a modal javascript popup (alert, confirm, prompt)

它被定义为在没有所有正常菜单等活动的情况下启动,但它仍然是一个浏览器窗口.您可以按照有关新浏览器窗口的Watir Wiki部分,使用 url 或标题,因为您知道这两者(鉴于您向我们展示的 HTML).

It's defined to start without all the normal menus etc active, but it's still a browser window. You can attach to it as described in the Watir Wiki section about new browser windows, using the url or the title since you know both of those (given the HTML you showed us).

如果您使用 Watir-Webdriver,请使用它的窗口切换命令.现在 watirspec 是您对所支持方法的最佳参考,它们是如何工作的.

If you are using Watir-Webdriver use it's window switching commands. Right now the watirspec for that is your best reference to the methods supported and how they work.

编辑谢谢你的链接.虽然对于新的浏览器窗口,上述情况是正确的,但这不是您所面临的.你所拥有的一切都在浏览器 HTML 中,在 DOM 中使用 javascript 动态创建.它都是标准的 HTML 元素,一旦你知道发生了什么,就可以轻松地进行交互(恕我直言,这实际上比弹出窗口或单独的窗口更容易处理)

EDIT Thanks for the link. While the above would be true for a new browser window, that's not what you are faced with. What you have there is all inside the browser HTML, created in the DOM on the fly with javascript. It's all standard HTML elements, easily interacted with once you know what's going on (this is actually IMHO easier to deal with than a popup or separate window)

使用 IE 开发者工具,点击使窗口"出现的链接后,点击开发工具工具栏中的图标以刷新开发工具中的 DOM,您将能够看到.

Use the IE developer tools, after you click the link that makes that 'window' appear, click the icon in the toolbar of the dev tools to refresh the DOM in the dev tools and you will be able to see that.

最外面的容器似乎是dialog"类的一个 div,它在当时的 DOM 中是唯一的.

The outermost container appears to be a div of class 'dialog', which is unique in the DOM at that point.

窗口控件是那一个下的三个 div,分别是spread_close"、spread_minimize"、spread_maximize".有三个表格保存了窗口"的顶部、侧面和底部的图形元素,但那里的实际内容为零,它只是一个可视窗口框.

The window controls are three divs under that one, with classes 'spread_close', 'spread_minimize', 'spread_maximize'. There are three tables that hold the graphic elements for the top, sides, and bottom of the 'window' but there is ZERO actual content there, it's just a visual windowframe.

还有一个叠加该窗口的 iframe,我认为这是内容(我无法加载它,可能是因为我没有获得授权或其他原因)

There is also an iframe that superimposes that window, which is I think were the content would be (I can't get it to load, maybe because I'm not authorized for it or something)

如果你只想关闭窗口,试试这个:

If you just want to close the window, try this:

browser.div(:class => 'spread_close').click

由于这是由于一堆客户端 JS 代码而存在的,因此您可能需要在首次开始与其交互之前单击链接后使用类似when_present"的方法.例如,如果您只想点击链接打开它,然后关闭它,您可以这样做

Since this is coming into existing due to a bunch of client side JS code you may need to use something like the 'when_present' method after clicking the link before you first start to interact with it. eg if all you want to do is click the link to open it, and then close it, you'd do something like this

browser.link(:text => 'Security Statement').click
browser.div(:class => 'spread_close').when_present.click

这篇关于使用 Watir 附加到 javascript 窗口时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆