如何使用 Capybara 通过查询字符串获取当前路径

发布于 2024-10-20 13:13:38 字数 317 浏览 2 评论 0原文

页面网址类似于 /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 技术交流群。

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

发布评论

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

评论(6

凤舞天涯 2024-10-27 13:13:38

我更新了这个答案以反映水豚的现代惯例。我认为这是理想的,因为这是公认的答案,也是许多人在寻找解决方案时所参考的答案。话虽如此,检查当前路径的正确方法是使用 Capybara 提供的 has_current_path? 匹配器,如下所示:单击此处

示例用法:

expect(page).to have_current_path(people_path(search: 'name'))

正如您在文档中看到的,还有其他选项可用。如果当前页面是 /people?search=name 但您只关心它在 /people 页面上而不考虑参数,则可以发送 only_path< /code> 选项:

expect(page).to have_current_path(people_path, only_path: true)

此外,如果您想比较整个 URL:

expect(page).to have_current_path(people_url, url: true)

请感谢 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 Here

Example usage:

expect(page).to have_current_path(people_path(search: 'name'))

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 the only_path option:

expect(page).to have_current_path(people_path, only_path: true)

Additionally, if you want to compare the entire URL:

expect(page).to have_current_path(people_url, url: true)

Credit to Tom Walpole for pointing out this method.

做个少女永远怀春 2024-10-27 13:13:38

我将 _path 方法替换为 _url 来实现完整 url 与参数的比较。

current_url.should == people_url(:search => 'name')

I replaced the _path method with _url to achieve comparing the full urls with parameters.

current_url.should == people_url(:search => 'name')
对不⑦ 2024-10-27 13:13:38

只是在现代更新这个问题。当前使用 Capybara 2.5+ 时检查 current_paths 的最佳实践是使用 current_path 匹配器,它将使用 Capybara 等待行为来检查路径。如果想要检查 request_uri(路径和查询字符串)

expect(page).to have_current_path(people_path(:search => 'name'))  

如果只想要路径部分(忽略查询字符串)

expect(page).to have_current_path(people_path, only_path: true) # Capybara < 2.16
expect(page).to have_current_path(people_path, ignore_query: true) # Capybara >= 2.16

如果想要匹配完整的 url

expect(page).to have_current_path(people_url, url: true) # Capybara < 2.16
expect(page).to have_current_path(people_url) # Capybara >= 2.16

,匹配器将采用与 == 或正则表达式进行比较的字符串进行匹配

expect(page).to have_current_path(/search=name/)

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)

expect(page).to have_current_path(people_path(:search => 'name'))  

If only wanting the path part (ignoring the query string)

expect(page).to have_current_path(people_path, only_path: true) # Capybara < 2.16
expect(page).to have_current_path(people_path, ignore_query: true) # Capybara >= 2.16

If wanting to match the full url

expect(page).to have_current_path(people_url, url: true) # Capybara < 2.16
expect(page).to have_current_path(people_url) # Capybara >= 2.16

the matcher will take a string which is compared with == or a regex to match against

expect(page).to have_current_path(/search=name/)
浅笑依然 2024-10-27 13:13:38

我知道已经选择了答案,但我只是想提供替代解决方案。所以:

要获取路径和查询字符串,就像 Rails 中的 request.fullpath 一样,您可以这样做:

URI.parse(current_url).request_uri.should == people_path(:search => 'name')

您还可以在测试类中执行辅助方法(例如 ActionDispatch::IntegrationTest >)像这样(这就是我所做的):

def current_fullpath
  URI.parse(current_url).request_uri
end

希望这会有所帮助。

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:

URI.parse(current_url).request_uri.should == people_path(:search => 'name')

You can also do a helper method in your test class (like ActionDispatch::IntegrationTest) like this (which is what I did):

def current_fullpath
  URI.parse(current_url).request_uri
end

Hope this helps.

樱娆 2024-10-27 13:13:38

编辑:正如 Tinynumberes 提到的,对于带有端口号的 URL 来说,这会失败。把它放在这里,以防其他人有同样的好主意。

current_url[current_host.size..-1]

高尔夫效果与 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.

current_url[current_host.size..-1]

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

冰雪梦之恋 2024-10-27 13:13:38

请注意,作者在此评论中提到
使用 current_url 可能会导致不稳定的测试。

Beware that author mentioned in this comment.
That usage of current_url can lead to flaky tests.

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