在不同的环境中运行Cucumber测试

时间:2022-03-02 15:06:21

I'm using Cucumber and Capybara for my automated front end tests.

我正在使用Cucumber和Capybara进行自动化前端测试。

I have two environments that I would like to run my tests on. One is a staging environment, and the other is the production environment.

我有两个环境,我想运行我的测试。一个是临时环境,另一个是生产环境。

Currently, I have my tests written to access staging directly.

目前,我已将我的测试编写为直接访问分段。

visit('https://staging.somewhere.com')

I would like to re-use the tests in production (https://production.somewhere.com).

我想重新使用生产中的测试(https://production.somewhere.com)。

Would it be possible to store the URL in a variable in my step definitions

是否可以将URL存储在我的步骤定义中的变量中

visit(domain)

and define domain using an environment variable called form the command line? Like

并使用从命令行调用的环境变量定义域?喜欢

$> bundle exec cucumber features DOMAIN=staging

if I want to point the tests to my staging environment, or

如果我想将测试指向我的暂存环境,或者

$> bundle exec cucumber features DOMAIN=production

if I want it to run in production?

如果我想让它在生产中运行?

How do I go about setting this up? I'm fairly new to Ruby and I've been searching the forums for a straight forward information but could not find any. Let me know if I can provide more information. Thanks for your help!

我该怎么做呢?我是Ruby的新手,我一直在论坛上搜索直接的信息,但找不到任何信息。如果我能提供更多信息,请告诉我。谢谢你的帮助!

2 个解决方案

#1


7  

In the project's config file, create a config.yml file

在项目的配置文件中,创建一个config.yml文件

---
staging:
    :url: https://staging.somewhere.com

production:
    :url: https://production.somewhere.com

Then extra colon in the yml file allows the hash key to be called as a symbol.

然后yml文件中的冒号允许将散列键作为符号调用。

In your support/env.rb file, add the following

在support / env.rb文件中,添加以下内容

require 'yaml'    

ENV['TEST_ENV'] ||= 'staging'
project_root = File.expand_path('../..', __FILE__)
$BASE_URL = YAML.load_file(project_root + "/config/config.yml")[ENV['TEST_ENV']][:url]

This will default to the staging environment unless you override the TEST_ENV. Then, from your step or hook, you can call:

除非您覆盖TEST_ENV,否则这将默认为暂存环境。然后,从你的步骤或钩子,你可以打电话:

visit($BASE_URL)

or you might need :/

或者您可能需要:/

visit "#{$BASE_URL}"

This will allow you to use

这将允许您使用

bundle exec cucumber features TEST_ENV=production

#2


1  

I don't use cucumber much but you should be able to do

我不会使用黄瓜,但你应该能够做到

bundle exec cucumber features DOMAIN=staging

then in your tests use ENV['DOMAIN'] || YOUR_DEFAULT_DOMAIN to utilize this variable. YOUR_DEFAULT_DOMAIN should probably be your test environment.

然后在你的测试中使用ENV ['DOMAIN'] || YOUR_DEFAULT_DOMAIN使用此变量。 YOUR_DEFAULT_DOMAIN应该是您的测试环境。

See Here

#1


7  

In the project's config file, create a config.yml file

在项目的配置文件中,创建一个config.yml文件

---
staging:
    :url: https://staging.somewhere.com

production:
    :url: https://production.somewhere.com

Then extra colon in the yml file allows the hash key to be called as a symbol.

然后yml文件中的冒号允许将散列键作为符号调用。

In your support/env.rb file, add the following

在support / env.rb文件中,添加以下内容

require 'yaml'    

ENV['TEST_ENV'] ||= 'staging'
project_root = File.expand_path('../..', __FILE__)
$BASE_URL = YAML.load_file(project_root + "/config/config.yml")[ENV['TEST_ENV']][:url]

This will default to the staging environment unless you override the TEST_ENV. Then, from your step or hook, you can call:

除非您覆盖TEST_ENV,否则这将默认为暂存环境。然后,从你的步骤或钩子,你可以打电话:

visit($BASE_URL)

or you might need :/

或者您可能需要:/

visit "#{$BASE_URL}"

This will allow you to use

这将允许您使用

bundle exec cucumber features TEST_ENV=production

#2


1  

I don't use cucumber much but you should be able to do

我不会使用黄瓜,但你应该能够做到

bundle exec cucumber features DOMAIN=staging

then in your tests use ENV['DOMAIN'] || YOUR_DEFAULT_DOMAIN to utilize this variable. YOUR_DEFAULT_DOMAIN should probably be your test environment.

然后在你的测试中使用ENV ['DOMAIN'] || YOUR_DEFAULT_DOMAIN使用此变量。 YOUR_DEFAULT_DOMAIN应该是您的测试环境。

See Here