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

clo*_*ner 26 ruby rspec capybara

我很想在水豚中编写测试,但我无法获取页面的当前URL.我这样写的:

url = page.current_url + page.current_path
Run Code Online (Sandbox Code Playgroud)

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

Sev*_*rin 31

试试这个:

url = URI.parse(current_url)
Run Code Online (Sandbox Code Playgroud)

  • `current_url` 给出与上面一行完全相同的响应 ^^ (2认同)

Зел*_*ный 12

来自capybara会话doc:
当前页面的完全限定URL

def current_url
  driver.current_url
end
Run Code Online (Sandbox Code Playgroud)

当前页面的路径,没有任何域信息

def current_path
  URI.parse(current_url).path
end
Run Code Online (Sandbox Code Playgroud)

我认为你所做的不对


loc*_*dev 7

你可以使用have_current_path

expect(page).to have_current_path(new_user_path)
Run Code Online (Sandbox Code Playgroud)

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

  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
Run Code Online (Sandbox Code Playgroud)