【问题标题】:Replacing variable value in ruby while setting the value using "set" command在使用“set”命令设置值时替换 ruby​​ 中的变量值
【发布时间】:2019-02-12 05:03:17
【问题描述】:

我有一个 .properties 文件如下:

user:abcd
pwd:xyz
system:test

接下来,我有一个带有 Watir 的 ruby​​ 脚本,用于实现浏览器自动化。在这个脚本中,我有类似

的语句
browser.text_field(:id => 'identifierId').set "#{user}:variable to be replaced by its value from .properties file".

同样,“pwd”和“system”也需要替换其他值。

我根据以下帖子尝试了解决方案: Replace properties in one file from those in another in Ruby

但是,“set”命令将任何已作为参数的内容设置为它,而不是用它的值替换变量。

请帮忙。

【问题讨论】:

  • 不太清楚您要做什么。你能举例说明你想要发生的事情吗?只是您想更改文件中的一些关键值吗?

标签: ruby watir


【解决方案1】:

您必须从文件中读取信息。 大多数 Watir 用户为此使用 yaml 文件。

config/properties.yml:

user: abcd
pwd: xyz
system: test

然后读取 yaml 文件并解析您的数据:

properties = YAML.safe_load(IO.read('config/properties.yml'))

text_field = browser.text_field(id: 'identifierId')
text_field.set properties['user']

您也可以查看Cheezy's Fig Newton gem,它旨在与他的 Page Object gem 一起使用

【讨论】:

  • 这正是我想要的。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-05
  • 2017-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多