【问题标题】:How to get the full URL with the current path in Capybara如何在 Capybara 中获取具有当前路径的完整 URL
【发布时间】:2013-12-11 14:56:29
【问题描述】:

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

url = page.current_url + page.current_path

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

【问题讨论】:

    标签: ruby rspec capybara


    【解决方案1】:

    试试这个:

    url = URI.parse(current_url)
    

    【讨论】:

    • url = URI.parse(current_url).to_s
    • current_url 给出与上面一行完全相同的响应^^
    【解决方案2】:

    来自capybara session doc
    当前页面的完全限定网址

    def current_url
      driver.current_url
    end
    

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

    def current_path
      URI.parse(current_url).path
    end
    

    我觉得你做的不对

    【讨论】:

    • 是的,我做错了。但我会在以后的工作中使用你提供的东西。谢谢。
    【解决方案3】:

    你可以使用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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-26
      • 2011-01-28
      • 2015-09-23
      • 1970-01-01
      • 2011-07-31
      相关资源
      最近更新 更多