Home > Net >  Rspec how to call ENV variable before driver configuration
Rspec how to call ENV variable before driver configuration

Time:04-16

I am writing UI tests on a new feature. This feature is iframed in, and appears in all browser configuration except for incognito with third party cookies blocked. I have created a new browser settings option that I can call to run the test suite locally "BROWSER=iframe_present rspec --tag service:test_tag" or the test itself "BROWSER=iframe_present rspec spec/service_folder/example_test_spec.rb".

My goal is to set it up so that only this test file; or service tag ("test_tag") run with the specific browser configuration when automatically through a travis configuration (which I can test locally by running "BROWSER=headless rspec spec/service_folder/example_test_spec.rb").

I've tried to call the 'iframe_present' browser configuration from the test file in a few different ways, but each one hits the byebug I have in the final 'else' browser condition. Perhaps I need to use the ci.travis.sh file? Seems to deal with picking the browser config.

*edit to include spec_helper.rb file

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 file

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

CodePudding user response:

If I'm understanding correctly, I believe you need the following in your test:

before do
  allow(ENV).to receive(:[])
    .with("BROWSER").and_return("iframe_present")
end
  • Related