Ruby-on-rails 如何在 Rails 3 中使用 Capybara 单击具有相同文本的第二个链接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6733427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to click on the second link with the same text using Capybara in Rails 3?
提问by Aleksandr Shvalev
In a view file I have:
在视图文件中,我有:
= link_to 'View', post
= link_to 'View', comment
In a spec file (I'm using Capybara):
在规范文件中(我使用的是 Capybara):
click_on 'View'
It clicks on the first link, but I want it to click on the second one. How can I do it?
它点击第一个链接,但我希望它点击第二个链接。我该怎么做?
回答by Gabriel F. Engel
You could try to find all entries and deal with an array:
您可以尝试查找所有条目并处理数组:
page.all('a')[1].click
Would help to have a class or use withinto scope your search ;)
将有助于开设课程或在搜索范围内使用;)
回答by Keith Gaddis
There's probably a few ways but I usually scope something like this.
可能有几种方法,但我通常会考虑这样的范围。
within(".comment") do
click_on("View")
end
There's quite possibly/probably alternatives as well. I usually do my acceptance testing from cucumber, so my steps typically look like
还有很可能/很可能的替代方案。我通常用黄瓜进行验收测试,所以我的步骤通常看起来像
When I follow "View" within the comment element
Where I have a step that translates within the comment elementto a scoped call to the step itself (which I think is built into the latest capybara web_steps)
我有一个步骤可以转换within the comment element为对步骤本身的范围调用(我认为它内置于最新的水豚 web_steps 中)
回答by installero
The worst thing about "the second" link is that it can become the third or the first or even the twenty fifth someday. So, scoping with a withinblock is the best way. Example:
“第二个”链接最糟糕的地方在于,它可能有一天会变成第三个或第一个,甚至第 25 个。所以,用within块来界定范围是最好的方法。例子:
within(".comment") do
click_on("View")
end
But if it is difficult to specify the link with a withinscope (which sometimes it really is), I guess the way to click the second link with a certain text is:
但是,如果很难指定具有within范围的链接(有时确实如此),我想单击具有特定文本的第二个链接的方法是:
find(:xpath, "(//a[text()='View'])[2]").click
In later versions of capybara (2.0.2, for example) both click_on 'View'and click_link 'View'will raise an ambiguous match error:
在更高版本的水豚(2.0.2例如)中,click_on 'View'和click_link 'View'都会引发不明确的匹配错误:
Failure/Error: click_on 'View'
Capybara::Ambiguous:
Ambiguous match, found 2 elements matching link or button "View"
So this won't do even if you want to click the first link (or if any link would be ok, which is my case).
因此,即使您想单击第一个链接(或者任何链接都可以,这是我的情况),这也不会发生。
As far as I understand this is made to force people write more specific tests where particular links are clicked.
据我所知,这是为了迫使人们在单击特定链接的地方编写更具体的测试。
It definitely could be tricky to debug the code if you accidentally placed two or more links with identical text and try to see what is happening. It's good to rely on something that is unlikely to change and specifying a link with a withinblock is a nice way to do this.
如果您不小心放置了两个或多个具有相同文本的链接并尝试查看发生了什么,那么调试代码肯定会很棘手。依靠不太可能改变的东西是很好的,并且指定一个带有within块的链接是一个很好的方法来做到这一点。
回答by RohitPorwal
There are many ways for solving this type of problems.
Do it like this
有很多方法可以解决此类问题。
像这样做
if(page.find("a")[:href] == "comment")
click_on("View")
or
或者
page.find("a:eq(2)").click
Remember javascript indexing starts with 0 while In Capybara, indexing starts with 1. So use a:eq(2) here for second href.
请记住,javascript 索引从 0 开始,而在 Capybara 中,索引从 1 开始。所以在这里使用 a:eq(2) 作为第二个 href。
回答by Michael Nikitochkin
For capybara 2solution:
对于capybara 2解决方案:
within(".comment") do
click_on("View")
end
would not help if you have a few .comment. So simple use: page.first(:link, "View").click
如果你有几个也无济于事.comment。如此简单的使用:page.first(:link, "View").click
回答by Jwan622
This works for me if you have several rows of identical classes and you want to find the second row. Like a previous author mentioned, capybara indexing starts at 1.
如果您有几行相同的类并且您想找到第二行,这对我有用。就像之前的一位作者提到的那样,水豚索引从 1 开始。
within all(".trip-row")[2] do
assert page.has_content?("content")
end
回答by steel
If you use capybara-uiyou could define the widget, or reusable DOM reference, for each widget.
如果您使用capybara-ui,您可以为每个小部件定义小部件或可重用的 DOM 引用。
# define your widget. in this case,
# we're defining it in a role
widget :view_post, ['.post', text: 'View']
widget :view_comment, ['.comment', text: 'View']
# then click that widget in the test
role.click :view_post
role.click :view_comment

