如何在 Ruby 中为多个环境创建配置文件?
我不想让您感到困惑,所以我想做的是以下内容:
我有三个环境:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
YAML 是一种用于处理配置信息的出色数据序列化语言。它随 Ruby 一起提供,所以你只需要做:
加载它,然后类似于:
所有配置数据都将在
configuration
变量中可用。通常,我通过编写一些 Ruby 代码,定义包含它的配置哈希,然后告诉 YAML 为我生成文件,为 YAML 文件创建一个存根。 请参阅文档了解
load_file
和dump
了解如何做到这一点。对于像您正在做的事情,我会创建一个哈希,例如:
Using YAML::dump(configuration) returns:,
您希望将其写入
.yaml
文件,然后在运行时加载-time 并像这样访问它:您可以将
'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:
to load it in, then something like:
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
anddump
for ways to do that.For something like you're doing I'd create a hash like:
Using YAML::dump(configuration) returns:
which you'd want to write to your
.yaml
file, then load later at run-time and access it like: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.