如何设置“基本 URL”对于 Webrat,机械化

发布于 2024-10-25 03:36:47 字数 62 浏览 1 评论 0原文

我想指定一个基本 URL,这样我就不必总是指定绝对 URL。如何指定 Mechanize 使用的基本 URL?

I would like to specify a base URL so I don't have to always specify absolute URLs. How can I specify a base URL for Mechanize to use?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

君勿笑 2024-11-01 03:36:47

要使用 Webrat 完成之前提供的答案,您可以在 Cucumber env.rb 中执行以下操作:

require 'webrat'

Webrat.configure do |config|
  config.mode = :mechanize
end

World do
  session = Webrat::Session.new
  session.extend(Webrat::Methods)
  session.extend(Webrat::Matchers)
  session.visit 'http://yoursite/yourbasepath/'
  session
end

为了使其更加健壮,例如在不同的环境中使用,您可以这样做:

ENV['CUCUMBER_HOST'] ||= 'yoursite'
ENV['CUCUMBER_BASE_PATH'] ||= '/yourbasepath/'

# Webrat
require 'webrat'

Webrat.configure do |config|
  config.mode = :mechanize
end

World do
  session = Webrat::Session.new
  session.extend(Webrat::Methods)
  session.extend(Webrat::Matchers)
  session.visit('http://' + ENV['CUCUMBER_HOST'] + ENV['CUCUMBER_BASE_PATH'])
  session
end

请注意,如果您使用 Mechanize、Webrat也将无法遵循您的重定向,因为它无法正确解释当前主机。要解决此问题,您可以在上面添加 session.header('Host', ENV['CUCUMBER_HOST'])

为了确保在任何地方都使用正确的路径进行访问和匹配,请将 ENV['CUCUMBER_BASE_PATH'] + 添加到 paths.rb 中 paths_to 方法的开头(如果您使用它)。它应该是这样的:

  def path_to(page_name)
    ENV['CUCUMBER_BASE_PATH'] + 

    case page_name

如果有人从中收到几封电子邮件,我深表歉意——我最初试图以评论的形式发布,但 Stack Overflow 令人恼火的 UI 战胜了我。

To accomplish the previously proffered answer using Webrat, you can do the following e.g. in your Cucumber env.rb:

require 'webrat'

Webrat.configure do |config|
  config.mode = :mechanize
end

World do
  session = Webrat::Session.new
  session.extend(Webrat::Methods)
  session.extend(Webrat::Matchers)
  session.visit 'http://yoursite/yourbasepath/'
  session
end

To make it more robust, such as for use in different environments, you could do:

ENV['CUCUMBER_HOST'] ||= 'yoursite'
ENV['CUCUMBER_BASE_PATH'] ||= '/yourbasepath/'

# Webrat
require 'webrat'

Webrat.configure do |config|
  config.mode = :mechanize
end

World do
  session = Webrat::Session.new
  session.extend(Webrat::Methods)
  session.extend(Webrat::Matchers)
  session.visit('http://' + ENV['CUCUMBER_HOST'] + ENV['CUCUMBER_BASE_PATH'])
  session
end

Note that if you're using Mechanize, Webrat will also fail to follow your redirects because it won't interpret the current host correctly. To work around this, you can add session.header('Host', ENV['CUCUMBER_HOST']) to the above.

To make sure the right paths are being used everywhere for visiting and matching, add ENV['CUCUMBER_BASE_PATH'] + to the beginning of your paths_to method in paths.rb, if you use it. It should look like this:

  def path_to(page_name)
    ENV['CUCUMBER_BASE_PATH'] + 

    case page_name

Apologies if anyone got a few e-mails from this -- I originally tried to post as a comment and Stack Overflow's irritating UI got the better of me.

抚笙 2024-11-01 03:36:47

对于 Mechanize,您指定的第一个 URL 将被视为基本 URL。例如:

require "rubygems"
require "mechanize"

agent = Mechanize.new
agent.get("http://some-site.org")

# Subsequent requests can now use the relative path:

agent.get("/contact.html")

这样您只需指定基本 URL 一次。

For Mechanize, the first URL you specify will be considered the base URL. For example:

require "rubygems"
require "mechanize"

agent = Mechanize.new
agent.get("http://some-site.org")

# Subsequent requests can now use the relative path:

agent.get("/contact.html")

This way you only specify the base URL once.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文