我正在為一項新功能撰寫 UI 測驗。此功能已內置,并出現在所有瀏覽器配置中,但隱身模式除外,第三方 cookie 被阻止。我創建了一個新的瀏覽器設定選項,我可以呼叫它來在本地運行測驗套件“BROWSER=iframe_present rspec --tag service:test_tag”或測驗本身“BROWSER=iframe_present rspec spec/service_folder/example_test_spec.rb”。
我的目標是設定它,以便只有這個測驗檔案;或服務標簽(“test_tag”)在通過 travis 配置自動運行時使用特定的瀏覽器配置(我可以通過運行“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
環境.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
uj5u.com熱心網友回復:
如果我理解正確,我相信您在測驗中需要以下內容:
before do
allow(ENV).to receive(:[])
.with("BROWSER").and_return("iframe_present")
end
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/460086.html
