在 Watir 中使用类名 [英] Using class names in Watir

查看:36
本文介绍了在 Watir 中使用类名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我们的 QA 人员今天过来让我将 id 放在我们的 html 中的项目上,这样他就可以使用 watir 自动完成工作.

So our QA guy came by today to get me to put id's on items in our html so he could automate stuff using watir.

我对此知之甚少,所以我试着看看我们是否可以使用类名来代替,但这完全是废话.

I don't know much about it, so I tried to see if we could use the class names instead, but that's a total crapshow.

我只是想知道为什么像这样

I was just wondering why something like

link(:item, :id => 'save-btn')

当你在 watir 中设置它时可以工作,但你不能做类似的事情

works when you set it up in watir, but you can't do something like

links(:item, :class => 'save-btn')[0] 

我也尝试使用 browser.links 调用,但我们始终会得到

I also tried using the browser.links calls, but we would consistently get

element not visible errors

我只是想知道为什么这如此困难,对所有内容使用 id 似乎是推荐的方法?有没有办法在 watir 中使用类名,或者只是这样?

I was just wondering why this was so difficult, to where using ids on everything seems to be the recommended way to go with everything? Is there a way to use class names with watir or is that just the way things are done?

推荐答案

CSS 类属性是使用 Watir 访问元素的非常正常的方式.和身份证一样.

CSS class attributes are quite normal way of accessing elements with Watir. As is id.

对于 class 属性,您通常必须指定搜索的上下文,因为可能有多个元素具有相同的 class,而不是 id 属性,id 属性必须在整个页面中唯一.

For class attributes, however you have to specify usually the context where you're searching from because there might be more than one element with the same class as opposed to id attribute, which has to be unique for the whole page.

我不确定您在示例中使用的是什么框架,但如果保存按钮位于某个容器元素中,我将在普通旧版中执行此操作:

I'm not sure what framework you're using in your examples, but this is how i would do it in plain old watir if the save button is in some container element:

browser.div(id: "container").span(class: "save-btn")

上面的代码将按预期在容器元素中找到第一个类为save-btn"的跨度.

The code above will find first span with class "save-btn" in the container element as expected.

此外,不要按照此处另一个答案的建议使用 xpathcss 定位器.为什么?因为它们真的很脆弱,会让你的测试难以阅读/维护.

Also, do not use xpath or css locators ever as suggested by another answer in here. Why? Because they are really fragile and make your tests too hard to read/maintain.

这篇关于在 Watir 中使用类名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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