如何在 Ruby 中为多个环境创建配置文件?

发布于 2024-10-24 23:41:21 字数 998 浏览 1 评论 0原文

我不想让您感到困惑,所以我想做的是以下内容:

我有三个环境:

www.env1.com
www.env2.com
www.env3.com

我想创建一些东西来根据我想要运行脚本的环境来定义设置阶段,即:

当前设置-up:

def setup
  @verification_errors = []
  @selenium = Selenium::Client::Driver.new(
    :host => "localhost",
    :port => 4444,
    :browser => "*firefox C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe",
    :url => "www.env1.com",
    :timeout_in_second => 60
  )

  @selenium.start_new_browser_session
end

我想要的:

def setup
  @verification_errors = []
  @selenium = Selenium::Client::Driver.new( 
    :host => "localhost",
    :port => 4444,
    :browser => "*firefox C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe",
    **:url => This parameter configurable from a file or other source.**
    :timeout_in_second => 60
  )

  @selenium.start_new_browser_session
end

如果可能的话,我可以切换环境而不必重新编写所有测试用例。

希望你能帮助我,我真的需要这样做。

I don't want to confuse you so what I want to do is the following:

I have three environments:

www.env1.com
www.env2.com
www.env3.com

I want to create something to define the setup phase according the environment over which I want to run the scripts, that is:

Current set-up:

def setup
  @verification_errors = []
  @selenium = Selenium::Client::Driver.new(
    :host => "localhost",
    :port => 4444,
    :browser => "*firefox C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe",
    :url => "www.env1.com",
    :timeout_in_second => 60
  )

  @selenium.start_new_browser_session
end

What I want:

def setup
  @verification_errors = []
  @selenium = Selenium::Client::Driver.new( 
    :host => "localhost",
    :port => 4444,
    :browser => "*firefox C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe",
    **:url => This parameter configurable from a file or other source.**
    :timeout_in_second => 60
  )

  @selenium.start_new_browser_session
end

If this is possible I can switch environments without having to re-write all test cases.

Hope you can help me out, I really need to do this.

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

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

发布评论

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

评论(1

肥爪爪 2024-10-31 23:41:21

YAML 是一种用于处理配置信息的出色数据序列化语言。它随 Ruby 一起提供,所以你只需要做:

require 'yaml'

加载它,然后类似于:

configuration = YAML::load_file('path/to/yamldata.yaml')

所有配置数据都将在 configuration 变量中可用。

通常,我通过编写一些 Ruby 代码,定义包含它的配置哈希,然后告诉 YAML 为我生成文件,为 YAML 文件创建一个存根。 请参阅文档了解 load_filedump 了解如何做到这一点。

对于像您正在做的事情,我会创建一个哈希,例如:

configuration = {
  'env1' => "www.env1.com",
  'env2' => "www.env2.com",
  'env3' => "www.env3.com",
}

Using YAML::dump(configuration) returns:,

--- 
env1: www.env1.com
env2: www.env2.com
env3: www.env3.com

您希望将其写入 .yaml 文件,然后在运行时加载-time 并像这样访问它:

@selenium = Selenium::Client::Driver.new( 
  :host => "localhost",
  :port => 4444,
  :browser => "*firefox C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe",
  :timeout_in_second => 60
  :url => configuration['env1'],
)

您可以将 'env1' 替换为其他键以使用 env2 或 env3。

Rails 使用 YAML 使一个文件处理应用程序的开发、测试和生产信息。在工作中,我用它来做类似的事情,其中​​一个文件包含我们应用程序的开发和生产环境信息,以及我们需要维护的一些哈希的定义,但不想为此修改代码。

YAML is a great data serialization language for handling configuration information. It comes with Ruby so you only have to do:

require 'yaml'

to load it in, then something like:

configuration = YAML::load_file('path/to/yamldata.yaml')

All your configuration data will be available inside the configuration variable.

Generally I create a stub for my YAML files by writing some Ruby code, defining the configuration hash that contains it, then telling YAML to generate the file for me. See the docs for load_file and dump for ways to do that.

For something like you're doing I'd create a hash like:

configuration = {
  'env1' => "www.env1.com",
  'env2' => "www.env2.com",
  'env3' => "www.env3.com",
}

Using YAML::dump(configuration) returns:

--- 
env1: www.env1.com
env2: www.env2.com
env3: www.env3.com

which you'd want to write to your .yaml file, then load later at run-time and access it like:

@selenium = Selenium::Client::Driver.new( 
  :host => "localhost",
  :port => 4444,
  :browser => "*firefox C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe",
  :timeout_in_second => 60
  :url => configuration['env1'],
)

You can replace 'env1' with the other keys to use env2 or env3.

Rails uses YAML to make one file handle the development, test and production information for an application. At work I use it to do similar things, where one file contains our development and production environmental information for apps, plus the definitions of some hashes we need to maintain, but don't want to have to modify the code to do so.

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