将命令行上的变量传递到Cucumber测试 [英] Passing variables on the command line to a Cucumber test
问题描述
我试图保持黄瓜项目的用户名和密码不受版本控制。
I'm trying to keep usernames and passwords for a cucumber project out of version control.
有一种方法可以在命令行上像用户名一样手动传递变量和密码到黄瓜脚本?
Is there a way to manually pass variables on the command line like usernames and passwords to a cucumber script?
我的备份计划是将它们放在一个YML文件中,并将该文件添加到gitignore,所以它们不会被放在版本控制。
My backup plan was to put them in a YML file and add that file to the gitignore so they aren't put in version control.
推荐答案
所以,我看到了你的意见与锡人,答案是肯定的。
So, I saw your comments with the Tin Man, and answer is Yes.
cucumber PASSWORD=my_password
PASSWORD设置为环境变量,您可以通过将其值引用为 ENV ['PASSWORD']
来使用它。例如, browser.text_field(:id =>'pwd')。set ENV ['PASSWORD']
PASSWORD is set as an environment variable and you can use its value by referring to it as ENV['PASSWORD']
. For an example, browser.text_field(:id => 'pwd').set ENV['PASSWORD']
另一种方式是间接的。
我在过去做的是传递配置文件名称,该配置文件将做我想要的东西。例如,我有一个配置文件名称为 firefox
,并且cucumber.yml中的firefox配置文件有一个变量名为 BROWSER_TYPE
其值分配给firefox。此变量( BROWSER_TYPE
)由我的打开浏览器的方法使用。如果它的值是firefox,比这个方法打开firefox浏览器。
Another way is indirect.
What I did in past was to pass profile name and that profile will do something that I want. So, for example, I have a profile name as firefox
and a firefox profile in cucumber.yml has a variable named BROWSER_TYPE
with its value assigned to firefox. And this variable (BROWSER_TYPE
) is used by my method that opens the browser. If its value is firefox, than this method opens firefox browser.
所以,我在这里做的是 -
So, what I did here was -
- 传递个人资料。个人资料的名称是
firefox
- firefox个人资料在cucumber.yml中定义。你可以使用配置文件的任何东西,但在这种情况下,我定义一个名为
BROWSER_TYPE
的变量,并将其值指定为firefox。 - 然后我有一个使用
BROWSER_TYPE
变量并使用其值打开浏览器的方法。
- Pass a profile. Name of the profile is
firefox
- firefox profile is defined in cucumber.yml. You can any thing with the profiles, but in this case, I define a variable named
BROWSER_TYPE
and assign its value as firefox. - Then I have a method that uses
BROWSER_TYPE
variable and uses its value to open browser.
这些步骤的代码 -
Code for these steps -
-
cucumber -p firefox
-
我的cucumber.yml文件看起来像
firefox:BROWSER_TYPE = firefox PLATFORM = beta
我的打开浏览器的方法类似于
My method to open browser looks similar to -
@browser = Watir :: Browser.new ENV ['BROWSER_TYPE']
因此,一个具有密码的环境变量,并将该配置文件名称传递给cucumber。
So, ideally you can create a profile that sets an environment variable with password, and pass that profile name to cucumber.
这篇关于将命令行上的变量传递到Cucumber测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!