ruby 如何在 Capybara 中获取当前路径的完整 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20522349/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 06:19:27  来源:igfitidea点击:

How to get the full URL with the current path in Capybara

rubyrspeccapybara

提问by cloudrunner

I'm new to writing tests in capybara and I'm having trouble getting the current URL of a page. I wrote it like this:

我是在水豚中编写测试的新手,并且无法获取页面的当前 URL。我是这样写的:

url = page.current_url + page.current_path

Somehow its just returning the base URL. Help is much appreciated.

不知何故,它只是返回基本 URL。非常感谢帮助。

回答by Severin

Try this:

尝试这个:

url = URI.parse(current_url)

回答by Зелёный

from capybara session doc:
Fully qualified URL of the current page

来自水豚会话文档
当前页面的完全限定 URL

def current_url
  driver.current_url
end

Path of the current page, without any domain information

当前页面的路径,不带任何域信息

def current_path
  URI.parse(current_url).path
end

I think that what you are doing is not right

我认为你在做什么是不对的

回答by localhostdotdev

you could use have_current_path:

你可以使用have_current_path

expect(page).to have_current_path(new_user_path)

before seeing that I was doing something like:

在看到我在做类似的事情之前:

  def current_path
    current_uri = URI.parse(page.current_url)
    current_path = current_uri.path
    current_path += "?#{current_uri.query}" if current_uri.query.present?
    current_path
  end