6

我的测试尝试访问网页并验证页面上是否存在某些元素。例如,它访问http://foo.com/homepage.html并检查徽标图像,然后访问http://bar.com/store/blah.html并检查页面上是否显示某些文本。我的目标是访问经过 Kerberos 身份验证的网页。

我发现 Kerberos 代码如下:

主文件

uri = URI.parse(Capybara.app_host)
kerberos = Kerberos.new(uri.host)
@kerberos_token = kerberos.encoded_token

kerberos.rb 文件

class Kerberos
    def initialize(host)
      @host = host
      @credentials = AuthGss::Negotiate.new("HTTP@#{@host}")
      @credentials.cache = ENV['KRB5CCNAME'] if ENV['KRB5CCNAME']
      @token = @credentials.step("")
    end

    def encoded_token
      Base64.encode64(@token).gsub(/\n/,"")
    end
  end

它利用Capybara.app_host价值。我无法弄清楚将Capybara.app_host值设置为什么。我不知道它做了什么。我有Capybara.run_server = false。有人可以帮助我了解如何使用Capybara.app_host以及这与 Kerberos 身份验证有何关系?

4

2 回答 2

9

Capybara文档显示了使用远程主机的示例。app_host是您的 Web 应用程序的基本主机:

Capybara.current_driver = :selenium
Capybara.run_server = false
Capybara.app_host = 'http://www.google.com'

visit('/users') # goes to http://www.google.com/users
于 2013-11-15T03:37:55.300 回答
8

I was confused about app_host, but after a dig around in the Capybara source code it seems like it doesn't actually do very much. In fact, it only seems to be used at one place in the Capybara source code:

if url_relative && Capybara.app_host
  url = Capybara.app_host + url
  url_relative = false
end

Basically, if you pass a relative URL like /posts/1/edit to a method like visit, Capybara will visit the URL "#{Capybara.app_host}/posts/1/edit`. So the following two snippets of code are equivalent:

# 1:
Capybara.app_host = "https://stackoverflow.com"
visit "/questions/19991349"

# 2:
visit "https://stackoverflow.com/questions/19991349"

By default RSpec sets app_host to http://localhost, which is why you can write code like before { visit edit_post_path(1) } when using Capybara and Rails straight out of the box, and you don't have to write localhost every time.

于 2015-03-15T15:29:59.583 回答