3

Currently, here is part of my configuration with testing using Chrome:

    prefs = {
    'download' => {
        'default_directory' => "#{$myRoot}/PDF",
        'prompt_for_download' => false,
    },
    'credentials_enable_service' => false,
    'profile' => {
        'default_content_settings' => {'multiple-automatic-downloads' => 1}, #for chrome version older ~42
        'default_content_setting_values' => {'automatic_downloads' => 1}, #for chrome new 46
        'password_manager_enabled' => false
    }
}

caps = Selenium::WebDriver::Remote::Capabilities.chrome
caps['chromeOptions'] = {:prefs => prefs}

client = Selenium::WebDriver::Remote::Http::Default.new
client.read_timeout = 600 # seconds – default is 60
client.open_timeout = 600 # seconds – default is 60

# @headless = Headless.new
driver = Watir::Browser.new :chrome, :desired_capabilities => caps, :http_client => client

Before do

    @browser = driver

I am attempting to allow for use of Firefox as well using a similar set up as I found at http://www.agiletrailblazers.com/blog/advanced-selenium-multiple-browsers-and-environments , specifically by defining a getDriver method which uses a "BROWSER=" parameter in the command line:

Before do |scenario|
  $url = getSiteURL
  $driver = getDriver
  $wait = Selenium::WebDriver::Wait.new(:timeout => 5) # seconds
end

def getDriver
    driverPath = ENV['DRIVERPATH']
    if ENV['BROWSER'] == "firefox"
      if ENV['DRIVERPATH']
        driverPath = ENV['DRIVERPATH'] + '/geckodriver'
        return Selenium::WebDriver.for :firefox, driver_path: driverPath
      else
        return Selenium::WebDriver.for :firefox
      end
    elsif ENV['BROWSER'] == "chrome"
      if ENV['DRIVERPATH']
        driverPath = ENV['DRIVERPATH'] + '/chromedriver'
        return Selenium::WebDriver.for :chrome, driver_path: driverPath
      else
        # This may not work with newer versions of Selenium and firefox
        return Selenium::WebDriver.for :chrome
      end
    else
      raise Exception.new("Unsupported browser: " + browser)
    end
end

I made some changes where I set prefs, caps and client inside the definition, as well as expanding the definition for firefox, however, when I did that, I found that multiple Browser instances were being opened for each Scenario of the feature.

What should I change to avoid this, and how do I configure firefox to behave the same as how I have set up Chrome in the prefs?

2 Answers 2

1

At the end of each scenario, you should be quitting the driver instance during teardown:

$driver.quit

2
  • 1
    But what if I want it to persist between scenarios in a single feature? For example, if I set one scenario as a login scenario, the next one could be validating elements of a landing page. If I did a quit between each one, would that not then require me to perform the login again, and thus increase test run-time? Commented Aug 18, 2017 at 12:44
  • Yes it increases run time but that is not a reason to not quit. Tests must be independent of each other, that is a basic requirement. That means you have two choices: 1. login for each test 2. Find a way to create a login or session that does not use the UI. Commented Dec 17, 2017 at 9:18
0

Here is a solution I have found through much more poking around with the items I listed in my original question, which will stop multiple windows from opening, and works with the most recent geckodriver for Firefox. This actually works now, and now I just have to properly configure the Firefox preferences, and profile as well. Right now, I cannot seem to get this to quite work and will have the window size fluctuate as opposed to staying maximized, but I would say I am about 90% there

def getDriver
    case ENV['BROWSER']
        when "Chrome"
            prefs = {
                'download' => {
                    'default_directory' => "#{$myRoot}/PDF",
                    'prompt_for_download' => false,
                },
                'credentials_enable_service' => false,
                'profile' => {
                    'default_content_settings' => {'multiple-automatic-downloads' => 1}, #for chrome version older ~42
                    'default_content_setting_values' => {'automatic_downloads' => 1}, #for chrome new 46
                    'password_manager_enabled' => false
                }
            }

            caps = Selenium::WebDriver::Remote::Capabilities.chrome
            caps['chromeOptions'] = {:prefs => prefs}
            client = Selenium::WebDriver::Remote::Http::Default.new
            client.read_timeout = 600 # seconds – default is 60
            client.open_timeout = 600 # seconds – default is 60
            return Watir::Browser.new :chrome, :desired_capabilities => caps, :http_client => client
        when "Firefox"
            prefs = {
                'download' => {
                    'default_directory' => "#{$myRoot}/PDF",
                    'prompt_for_download' => false,
                },
                'credentials_enable_service' => false,
                'profile' => {
                    'default_content_settings' => {'multiple-automatic-downloads' => 1}, #for chrome version older ~42
                    'default_content_setting_values' => {'automatic_downloads' => 1}, #for chrome new 46
                    'password_manager_enabled' => false
                }
            }

            caps = Selenium::WebDriver::Remote::Capabilities.firefox
            caps['firefoxOptions'] = {:prefs => prefs}
            client = Selenium::WebDriver::Remote::Http::Default.new
            client.read_timeout = 600 # seconds – default is 60
            client.open_timeout = 600 # seconds – default is 60
            return Watir::Browser.new :firefox, :desired_capabilities => caps, :http_client => client
    end
end 

# @headless = Headless.new
driver = getDriver
# Watir::Browser.new :chrome, :desired_capabilities => caps, :http_client => client

Before do

    # @headless.start
    @browser = driver
    @browser.window.maximize

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.