在 Watir 中链接元素时如何让点击事件起作用?

发布于 2024-09-26 09:32:05 字数 676 浏览 0 评论 0原文

我正在尝试在 Watir 中为我们的 Web 应用程序设置一些 UI 测试。我在传播点击事件时遇到了麻烦。我们使用 EXTJS 来制作选项卡,这些选项卡是生成的 html 中的跨度。

如果我选择这样的跨度,它会起作用:

span1 = @browswer.span(:text=>"Tab Name")
span1.click

问题是当我有一个具有相同名称的子选项卡并且希望能够区分它们时。我发现显式选择子选项卡的唯一方法是首先选择子选项卡所​​在的列表,然后从中选择子选项卡。

ul = @browser.ul(:class=>/tab-strip-bottom/)
span2 = ul.span(:text=>"Tab Name")
span2.click

span2.click 似乎没有执行任何操作。 span1 和 span2 之间唯一的不同(我可以看到)是容器属性。 span1.@container = @browser,span2.@container = ul。

我尝试在 span2 上设置容器

span2.instance_variable_set("@container", @browser)

,但最终我还是点击了错误的选项卡。对此有什么想法吗? 谢谢!

I am attempting to set up some UI testing in Watir for our web application. I'm running into trouble getting the click events to propagate. We are using EXTJS to make tabs, which are spans in the resulting html.

If I select a span like this it works:

span1 = @browswer.span(:text=>"Tab Name")
span1.click

The trouble is when I have a subtab with the same name and want to be able to differentiate them. The only way I've found to select the subtab explicitly is to first select the list that the subtab is in, and then select the subtab from that.

ul = @browser.ul(:class=>/tab-strip-bottom/)
span2 = ul.span(:text=>"Tab Name")
span2.click

span2.click doesn't appear to do anything. The only different (I can see) between span1 and span2 is the container attribute. span1.@container = @browser, span2.@container = ul.

I tried setting the container on span2 with

span2.instance_variable_set("@container", @browser)

but then I wind up clicking on the wrong tab anyway. Any thoughts on this?
Thanks!

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

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

发布评论

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

评论(4

叹沉浮 2024-10-03 09:32:05

请分享HTML代码,我什至不明白这个问题。

也许您需要触发一些 JavaScript 事件。请参阅如何找出触发了哪些 JavaScript 事件?

Please share the HTML code, I do not even understand the problem.

Maybe you need to fire some JavaScript event. See How to find out which JavaScript events fired?

丢了幸福的猪 2024-10-03 09:32:05

我终于解决了 xpath 遇到的问题。

tab = @browser.element_by_xpath(".//*[contains(@class,'x-tab-strip-top')]//*/em/span/span[. =\"#{value}\"]") 
tab.click

这并不理想,因为 xpath 代码非常难看。如果可以使用 watir 的“candy”选择器来选择元素,那么可维护性会更好。

I finally solved the problem I was having with xpath.

tab = @browser.element_by_xpath(".//*[contains(@class,'x-tab-strip-top')]//*/em/span/span[. =\"#{value}\"]") 
tab.click

It isn't ideal because the xpath code is pretty ugly. If it's possible to select the element by using watir's "candy" selectors, that would be much better for maintainability.

嗳卜坏 2024-10-03 09:32:05

我们有一个多层的网页菜单,听起来像你所拥有的。鼠标将在进入下一个选择(直接对角线)时离开第二级菜单,并且第二级菜单将关闭。这就是你所说的吗?我们通过触发 javascript 解决了这个问题,因为菜单项调用了 javascript(Zeljko 的解决方案)。我们只需要记下菜单项的 ID 即可。不确定这是否比 xpath 解决方案更丑或更漂亮。我说“我们”是因为我需要这方面的帮助。

We had a web page menu with multiple tiers, sounds like what you have. The mouse would drop off the second level of the menu while going to the next selection (directly diagonal) and the second menu level would close. Is that what you are talking about? We solved it by firing javascript because the menu items called javascript (Zeljko's solution). We just needed to note the id's of the menu items. Not sure if that is uglier or prettier than the xpath solution. I say "we" because I needed help with that.

蓝海似她心 2024-10-03 09:32:05

当有多个匹配对象时,您可以指定要使用的索引。这对你有用吗?

@browser.span(:text=>"Tab Name", :index => 2).click

索引值与您正在搜索的容器相关。有关搜索对象时使用的所有可能性的表格,请参阅:Watir 支持的 HTML 元素

You can specify an index to use when there are multiple matching objects. Does this work for you?

@browser.span(:text=>"Tab Name", :index => 2).click

The index values are relative to the container you're searching in. For a table of all possibilities to use when searching for objects see: HTML Elements Supported By Watir

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