Rspec驱动配置前如何调用ENV变量
Rspec how to call ENV variable before driver configuration
我正在为一项新功能编写 UI 测试。此功能位于 iframed 中,并出现在所有浏览器配置中,但第三方 cookie 被阻止的隐身模式除外。我创建了一个新的浏览器设置选项,我可以在本地调用 运行 测试套件“BROWSER=iframe_present rspec --tag service:test_tag”或测试本身“BROWSER =iframe_present rspec spec/service_folder/example_test_spec.rb".
我的目标是把它设置成只有这个测试文件;或服务标签 ("test_tag") 运行 自动通过 travis 配置时具有特定的浏览器配置(我可以通过 运行ning 在本地测试 "BROWSER=headless rspec spec/service_folder/example_test_spec.rb").
我尝试以几种不同的方式从测试文件中调用 'iframe_present' 浏览器配置,但每一种都遇到了我在最终 'else' 浏览器条件下遇到的错误。也许我需要使用 ci.travis.sh 文件?似乎与选择浏览器配置有关。
*编辑以包含 spec_helper.rb 文件
example_test_spec.rb
describe "Validating HQ Extensions menu item", type: :feature, service: "sales_channels1" do
context "Squadlocker" do
# different attempts
# let(:browser_config) { SeleniumTest.browser == iframe_present }
# BROWSER=iframe_present
# before(:all) { SeleniumTest.ENV["BROWSER"] == "iframe_present" }
# before { SeleniumTest.stub(ENV["BROWSER"] => "iframe_present") }
# before { SeleniumTest.stub(ENV["BROWSER"] == "iframe_present") }
before do
allow(ENV).to receive(:[])
.with("BROWSER").and_return("iframe_present")
end
let(:hq_home) { HqHomePage.new }
let(:marketplace) { MarketplacePage.new }
it "some condition check" do
# stuff
end
end
end
env.rb
require 'uri'
require 'capybara/poltergeist'
require 'selenium-webdriver'
require 'webdrivers'
require_relative '../../config/api_config_base'
module SeleniumTest
module_function
# url stuff, unrelated
browser = ENV['BROWSER'] ? ENV['BROWSER'].downcase : ''
puts "browser type: #{browser}"
if browser == 'firefox'
# options
RSpec.configure do |config|
config.before :each do
page.driver.browser.manage.window.maximize
end
end
elsif browser == 'headless'
Capybara.default_driver = :selenium_chrome_headless
Capybara.register_driver :selenium_chrome_headless do |app|
options = Selenium::WebDriver::Chrome::Options.new
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-gpu')
options.add_argument('--incognito')
Capybara::Selenium::Driver.new(
app,
browser: :chrome,
capabilities: [options]
)
end
elsif browser == 'iframe_present'
byebug
# currently matching chrome settings for testing minus incognito setting, will switch to headless
Capybara.default_driver = :selenium_chrome
Capybara.register_driver :selenium_chrome do |app|
options = Selenium::WebDriver::Chrome::Options.new
options.add_argument('--no-sandbox')
options.add_argument('--disable-gpu')
Capybara::Selenium::Driver.new(
app,
browser: :chrome,
capabilities: [options]
)
end
else
byebug
Capybara.default_driver = :selenium_chrome
Capybara.register_driver :selenium_chrome do |app|
options = Selenium::WebDriver::Chrome::Options.new
options.add_argument('--no-sandbox')
options.add_argument('--disable-gpu')
Capybara::Selenium::Driver.new(
app,
browser: :chrome,
capabilities: [options]
)
end
RSpec.configure do |config|
config.before :each do
page.driver.browser.manage.window.maximize
end
end
Capybara.javascript_driver = :chrome
end
ci.travis.sh
source ./script/env.travis.sh
echo $TEST_TAG
echo $RSPEC_TAG
echo $BROWSER
echo $TRAVIS_BRANCH
if [[ $TRAVIS_BRANCH == "main" ]]; then
BROWSER=headless ./run_spec_tests.sh "production" "Rspec UI Tests" \
"$TRAVIS_BUILD_NUMBER" "$TEST_AUTOMATION_NOTIFY_CHANNEL" \
"$TEST_AUTOMATION_NOTIFY_CHANNEL" "$AUTHOR_NAME" "" "$RSPEC_TAG" "$BROWSER"
elif [[ $TRAVIS_BRANCH != "testdrive" ]]; then
BROWSER=headless ./run_spec_tests.sh "staging" "Rspec UI Tests" \
"$TRAVIS_BUILD_NUMBER" "$TEST_AUTOMATION_NOTIFY_CHANNEL" \
"$TEST_AUTOMATION_NOTIFY_CHANNEL" "$AUTHOR_NAME" "" "$RSPEC_TAG" "$BROWSER"
fi
spec_helper.rb 文件
require "capybara/rspec"
require "etc"
Dir[File.dirname(__FILE__) + "/support/*.rb"].each { |f| require f }
Dir[File.dirname(__FILE__) + "/helpers/*.rb"].each { |f| require f }
Dir[File.dirname(__FILE__) + "/page_models/*.rb"].each { |f| require f }
RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods
config.include AffiliateRosteringHelper, type: :feature
config.include AffiliationsHelper, type: :feature
config.include AllureAttachmentHelper
config.include ApiRequestHelper
config.include CSVHelper
config.include DateTimeHelper, type: :feature
config.include UserHelper
config.include Capybara::DSL
# Use color in STDOUT
config.color = true
# Use color not only in STDOUT but also in pagers and files
config.tty = true
config.before(:suite) do
FactoryBot.find_definitions
end
config.formatter = :documentation
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
config.shared_context_metadata_behavior = :apply_to_host_groups
config.filter_run_when_matching :focus
config.before(:each, type: :feature) do
Capybara.current_session.driver.browser.manage.window.resize_to(1500, 1600)
end
if ApiConfigBase.env === 'production'
Capybara.default_max_wait_time = 30
else
Capybara.default_max_wait_time = 45
end
Capybara.raise_server_errors = true
config.verbose_retry = true
config.display_try_failure_messages = true
config.around :each do |ex|
ex.run_with_retry retry: 0 # set back to 3 b4 code review
end
config.formatter = AllureRspecFormatter if ENV["TRAVIS"]
if !ENV["TRAVIS"]
# Instructions on getting your secrets.json file here:
# https://sportngin.atlassian.net/wiki/spaces/DEV/pages/2913271865/Credentials+in+Parameter+Store
JSON.parse(File.read("secrets.json")).each do |key, value|
ENV[key.upcase] = value
end
end
config.after(:each, type: :feature) do |example|
# restart browser sessions for every test
attach_screenshot(example) if ENV["TRAVIS"]
driver = Capybara.current_session.driver
if driver.is_a?(Capybara::Selenium::Driver)
driver.quit
elsif driver.is_a?(Capybara::Poltergeist::Driver)
driver.browser.restart
end
end
end
如果我没理解错的话,我相信你在测试中需要以下内容:
before do
allow(ENV).to receive(:[])
.with("BROWSER").and_return("iframe_present")
end
我正在为一项新功能编写 UI 测试。此功能位于 iframed 中,并出现在所有浏览器配置中,但第三方 cookie 被阻止的隐身模式除外。我创建了一个新的浏览器设置选项,我可以在本地调用 运行 测试套件“BROWSER=iframe_present rspec --tag service:test_tag”或测试本身“BROWSER =iframe_present rspec spec/service_folder/example_test_spec.rb".
我的目标是把它设置成只有这个测试文件;或服务标签 ("test_tag") 运行 自动通过 travis 配置时具有特定的浏览器配置(我可以通过 运行ning 在本地测试 "BROWSER=headless rspec spec/service_folder/example_test_spec.rb").
我尝试以几种不同的方式从测试文件中调用 'iframe_present' 浏览器配置,但每一种都遇到了我在最终 'else' 浏览器条件下遇到的错误。也许我需要使用 ci.travis.sh 文件?似乎与选择浏览器配置有关。
*编辑以包含 spec_helper.rb 文件
example_test_spec.rb
describe "Validating HQ Extensions menu item", type: :feature, service: "sales_channels1" do
context "Squadlocker" do
# different attempts
# let(:browser_config) { SeleniumTest.browser == iframe_present }
# BROWSER=iframe_present
# before(:all) { SeleniumTest.ENV["BROWSER"] == "iframe_present" }
# before { SeleniumTest.stub(ENV["BROWSER"] => "iframe_present") }
# before { SeleniumTest.stub(ENV["BROWSER"] == "iframe_present") }
before do
allow(ENV).to receive(:[])
.with("BROWSER").and_return("iframe_present")
end
let(:hq_home) { HqHomePage.new }
let(:marketplace) { MarketplacePage.new }
it "some condition check" do
# stuff
end
end
end
env.rb
require 'uri'
require 'capybara/poltergeist'
require 'selenium-webdriver'
require 'webdrivers'
require_relative '../../config/api_config_base'
module SeleniumTest
module_function
# url stuff, unrelated
browser = ENV['BROWSER'] ? ENV['BROWSER'].downcase : ''
puts "browser type: #{browser}"
if browser == 'firefox'
# options
RSpec.configure do |config|
config.before :each do
page.driver.browser.manage.window.maximize
end
end
elsif browser == 'headless'
Capybara.default_driver = :selenium_chrome_headless
Capybara.register_driver :selenium_chrome_headless do |app|
options = Selenium::WebDriver::Chrome::Options.new
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-gpu')
options.add_argument('--incognito')
Capybara::Selenium::Driver.new(
app,
browser: :chrome,
capabilities: [options]
)
end
elsif browser == 'iframe_present'
byebug
# currently matching chrome settings for testing minus incognito setting, will switch to headless
Capybara.default_driver = :selenium_chrome
Capybara.register_driver :selenium_chrome do |app|
options = Selenium::WebDriver::Chrome::Options.new
options.add_argument('--no-sandbox')
options.add_argument('--disable-gpu')
Capybara::Selenium::Driver.new(
app,
browser: :chrome,
capabilities: [options]
)
end
else
byebug
Capybara.default_driver = :selenium_chrome
Capybara.register_driver :selenium_chrome do |app|
options = Selenium::WebDriver::Chrome::Options.new
options.add_argument('--no-sandbox')
options.add_argument('--disable-gpu')
Capybara::Selenium::Driver.new(
app,
browser: :chrome,
capabilities: [options]
)
end
RSpec.configure do |config|
config.before :each do
page.driver.browser.manage.window.maximize
end
end
Capybara.javascript_driver = :chrome
end
ci.travis.sh
source ./script/env.travis.sh
echo $TEST_TAG
echo $RSPEC_TAG
echo $BROWSER
echo $TRAVIS_BRANCH
if [[ $TRAVIS_BRANCH == "main" ]]; then
BROWSER=headless ./run_spec_tests.sh "production" "Rspec UI Tests" \
"$TRAVIS_BUILD_NUMBER" "$TEST_AUTOMATION_NOTIFY_CHANNEL" \
"$TEST_AUTOMATION_NOTIFY_CHANNEL" "$AUTHOR_NAME" "" "$RSPEC_TAG" "$BROWSER"
elif [[ $TRAVIS_BRANCH != "testdrive" ]]; then
BROWSER=headless ./run_spec_tests.sh "staging" "Rspec UI Tests" \
"$TRAVIS_BUILD_NUMBER" "$TEST_AUTOMATION_NOTIFY_CHANNEL" \
"$TEST_AUTOMATION_NOTIFY_CHANNEL" "$AUTHOR_NAME" "" "$RSPEC_TAG" "$BROWSER"
fi
spec_helper.rb 文件
require "capybara/rspec"
require "etc"
Dir[File.dirname(__FILE__) + "/support/*.rb"].each { |f| require f }
Dir[File.dirname(__FILE__) + "/helpers/*.rb"].each { |f| require f }
Dir[File.dirname(__FILE__) + "/page_models/*.rb"].each { |f| require f }
RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods
config.include AffiliateRosteringHelper, type: :feature
config.include AffiliationsHelper, type: :feature
config.include AllureAttachmentHelper
config.include ApiRequestHelper
config.include CSVHelper
config.include DateTimeHelper, type: :feature
config.include UserHelper
config.include Capybara::DSL
# Use color in STDOUT
config.color = true
# Use color not only in STDOUT but also in pagers and files
config.tty = true
config.before(:suite) do
FactoryBot.find_definitions
end
config.formatter = :documentation
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
config.shared_context_metadata_behavior = :apply_to_host_groups
config.filter_run_when_matching :focus
config.before(:each, type: :feature) do
Capybara.current_session.driver.browser.manage.window.resize_to(1500, 1600)
end
if ApiConfigBase.env === 'production'
Capybara.default_max_wait_time = 30
else
Capybara.default_max_wait_time = 45
end
Capybara.raise_server_errors = true
config.verbose_retry = true
config.display_try_failure_messages = true
config.around :each do |ex|
ex.run_with_retry retry: 0 # set back to 3 b4 code review
end
config.formatter = AllureRspecFormatter if ENV["TRAVIS"]
if !ENV["TRAVIS"]
# Instructions on getting your secrets.json file here:
# https://sportngin.atlassian.net/wiki/spaces/DEV/pages/2913271865/Credentials+in+Parameter+Store
JSON.parse(File.read("secrets.json")).each do |key, value|
ENV[key.upcase] = value
end
end
config.after(:each, type: :feature) do |example|
# restart browser sessions for every test
attach_screenshot(example) if ENV["TRAVIS"]
driver = Capybara.current_session.driver
if driver.is_a?(Capybara::Selenium::Driver)
driver.quit
elsif driver.is_a?(Capybara::Poltergeist::Driver)
driver.browser.restart
end
end
end
如果我没理解错的话,我相信你在测试中需要以下内容:
before do
allow(ENV).to receive(:[])
.with("BROWSER").and_return("iframe_present")
end