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



我刚开始用水豚编写测试,在获取页面的当前URL时遇到了问题。我是这样写的:

url = page.current_url + page.current_path

不知怎么的,它只是返回了基本URL。非常感谢您的帮助。

试试这个:

url = URI.parse(current_url)

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

def current_url
  driver.current_url
end

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

def current_path
  URI.parse(current_url).path
end

我认为你所做的是不对的

您可以使用have_current_path:

expect(page).to have_current_path(new_user_path)

在看到我在做这样的事情之前:

  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

最新更新