如何使用 Capybara 通过查询字符串获取当前路径
页面网址类似于 /people?search=name
当我使用水豚的 current_path
方法时,它仅返回 /people
。
current_path.should == people_path(:search => 'name')
但它没有说
expected: "/people?search=name"
got: "/people"
我们怎样才能让它通过?有什么办法可以做到这一点吗?
The page url is something like /people?search=name
while I used current_path
method of capybara it returned /people
only.
current_path.should == people_path(:search => 'name')
But it fails saying
expected: "/people?search=name"
got: "/people"
How we can make it pass? Is there is any way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我更新了这个答案以反映水豚的现代惯例。我认为这是理想的,因为这是公认的答案,也是许多人在寻找解决方案时所参考的答案。话虽如此,检查当前路径的正确方法是使用 Capybara 提供的
has_current_path?
匹配器,如下所示:单击此处示例用法:
正如您在文档中看到的,还有其他选项可用。如果当前页面是
/people?search=name
但您只关心它在/people
页面上而不考虑参数,则可以发送only_path< /code> 选项:
此外,如果您想比较整个 URL:
请感谢 Tom Walpole 指出了此方法。
I've updated this answer to reflect modern conventions in capybara. I think this is ideal since this is the accepted answer, and what many people are being referred to when looking for a solution. With that said, the correct way to check the current path is to use the
has_current_path?
matcher provided by Capybara, as documented here: Click HereExample usage:
As you can see in the documentation, other options are available. If the current page is
/people?search=name
but you only care that it's on the/people
page regardless of the param, you can send theonly_path
option:Additionally, if you want to compare the entire URL:
Credit to Tom Walpole for pointing out this method.
我将 _path 方法替换为 _url 来实现完整 url 与参数的比较。
I replaced the _path method with _url to achieve comparing the full urls with parameters.
只是在现代更新这个问题。当前使用 Capybara 2.5+ 时检查 current_paths 的最佳实践是使用 current_path 匹配器,它将使用 Capybara 等待行为来检查路径。如果想要检查 request_uri(路径和查询字符串)
如果只想要路径部分(忽略查询字符串)
如果想要匹配完整的 url
,匹配器将采用与 == 或正则表达式进行比较的字符串进行匹配
Just updating this question for modern times. The current best practice for checking current_paths when using Capybara 2.5+ is to use the current_path matcher, which will use Capybaras waiting behavior to check the path. If wanting to check against the request_uri (path and query string)
If only wanting the path part (ignoring the query string)
If wanting to match the full url
the matcher will take a string which is compared with == or a regex to match against
我知道已经选择了答案,但我只是想提供替代解决方案。所以:
要获取路径和查询字符串,就像 Rails 中的
request.fullpath
一样,您可以这样做:您还可以在测试类中执行辅助方法(例如
ActionDispatch::IntegrationTest
>)像这样(这就是我所做的):希望这会有所帮助。
I know an answer has been selected, but I just wanted to give an alternative solution. So:
To get the path and querystring, like
request.fullpath
in Rails, you can do:You can also do a helper method in your test class (like
ActionDispatch::IntegrationTest
) like this (which is what I did):Hope this helps.
编辑:正如 Tinynumberes 提到的,对于带有端口号的 URL 来说,这会失败。把它放在这里,以防其他人有同样的好主意。
高尔夫效果与
URI.parse(current_url).request_uri
完全相同(35 个字符),但可能更快,因为不涉及显式 URI 解析。我已提出拉取请求,将其添加到 Capybara: https://github.com/jnicklas/水豚/拉/1405
EDIT: as Tinynumberes mentioned, this fails for URLs with port number. Keeping it here in case anyone else has the same brilliant idea.
golfs exactly as well (35 chars) as
URI.parse(current_url).request_uri
, but is potentially faster because there is no explicit URI parsing involved.I have made a pull request to add this to Capybara at: https://github.com/jnicklas/capybara/pull/1405
请注意,作者在此评论中提到。
使用
current_url
可能会导致不稳定的测试。Beware that author mentioned in this comment.
That usage of
current_url
can lead to flaky tests.