New browser opens for every feature being executed - WATIR - selenium

We are using the Selenium Cucumber framework with Watir.
We have split one scenario into different features and all these features needs to run on one browser to complete the scenario.
Currently 1 feature completes and then the browser is closed and then a new instance of Firefox is opened; not preserving the state of the previous instance.
For our tests to run effectively, we need these features to be completed on the same browser instance.
How do we prevent a new browser instance open after each feature has been executed?
Here is our test structure:
W2.1.1-Set_Project_Information.feature
W2.1.2-Select_Shotlist.feature
W2.1.3-Flag_shotlist_requiring_physical_inspection.feature
W2.1.4-Select_applicable_shotlist_task.feature
W2.1.5-Record_Primary_Applicant.feature
My env.rb:
require 'rubygems'
require 'watir'
require 'selenium-webdriver'
require 'rspec'
browser = Watir::Browser.new :firefox
Before do
#browser = Watir::Browser.new :chrome
#browser = browser
#browser.goto "https://test.branzartisan.com"
#browser.window.maximize
sleep(5)
puts "###Browser Invoke###"
end
After do |scenario|
#browser.cookies.clear
#browser.refresh
end

The answer is the same as here: https://stackoverflow.com/a/17624188/4072371
That being said, I feel obligated to point out that ideally you can make your tests independent of each other.

The solution to this issue was removing the before do block from the env.rb file.
This prevented the invoking of a new Firefox instance before each scenario.

Related

Is there a way to add -Dchrome.switches chrome properties to serenity.properties or serenity.conf files?

I tried to add the below in the serenity.conf file to always load the chrome browser with these options but it fails to load the browser. When I pass in the below options via command line like so "gradle test -Dchrome.switches="--no-sandbox,--ignore-certificate-errors,--homepage=about:blank,--no-first-run" the browser starts successfully.
"-Dchrome.switches="--no-sandbox,--ignore-certificate-errors,--homepage=about:blank,--no-first-run"
Is there a way to always open chrome browser without having to pass this via command line or have the chrome driver as part of the framework?
serenity.conf
#
# WebDriver configuration
#
webdriver {
driver = chrome
autodownload = true
}
#headless.mode = true
serenity.test.root = java
#
# Chrome options can be defined using the chrome.switches property
#
chrome.switches = """--start-maximized;--test-type;--no-sandbox;--ignore-certificate-errors;
--disable-popup-blocking;--disable-default-apps;--disable-extensions-file-access-check;
--disable-web-security;--incognito;--disable-infobars,--disable-gpu,--homepage=about:blank,--no-first-run"""
Thanks!
Try
chrome {
switches = "--start-maximized;--enable-automation;--no-sandbox;--disable-popup-blocking;--disable-default-apps;--disable-infobars;--disable-gpu;--disable-extensions;"
preferences {
download: "{prompt_for_download: false,directory_upgrade: true,default_directory:'${user.dir}/downloaded-files'}"
}
}
Thank you!
I have switched to
#Managed
WebDriver driver;
https://serenity-bdd.github.io/theserenitybook/latest/web-testing-in-serenity.html#_a_simple_selenium_web_test
Below is the information on manned drivers in serenity.
Serenity reduces the amount of code you need to write and maintain when you write web tests. For example, it takes care of creating WebDriver instances, and of opening and closing the browser for you. The following is a very simple Selenium web test using Serenity:

Unable to download a file when chromedriver is in headless mode. How to get it work?

During the test, a file (.html) will be downloaded from the web application & I have to verify that file by opening it on the browser. In the non-headless mode, my test is working fine. But whenever I'm going to headless mode, that file is not getting downloaded to the download path (i.e. pointed in the "user.dir"). My chrome driver version is 2.44.609538 & selenium version is 3.14.
Apparently this could help you
Shawn Button post the answer related with it.
Downloading with chrome headless and selenium
Are you running the test from the command line?
Because, according to an answer to this question and this, when you run it via command line, your user.dir corresponds to your global user directory (C:\users\username).
This worked for our ruby implementation:
Capybara.register_driver :scrapping_driver do |app|
options = Selenium::WebDriver::Chrome::Options.new
options.add_argument('--headless')
options.add_argument('--disable-popup-blocking')
options.add_argument('--disable-gpu')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--window-size=1366,2000')
options.add_preference(:download, directory_upgrade: true,
prompt_for_download: false,
default_directory: "#{Rails.root}/")
options.add_preference(:browser, set_download_behavior: { behavior: 'allow' })
Selenium::WebDriver::Service.driver_path = Webdrivers::Chromedriver.driver_path
driver = Capybara::Selenium::Driver.new(app, browser: :chrome, options: options)
end
Pay attention at download behaviour
I met same situation.
Headless mode is very faster. So your code might be implemented to detect download(DL).
Solution.
(Current your process?) .click for DL -> Detecting download file generation.
(My solution) Detecting download file generation -> .click for DL.
I implemented the above mechanism using callback function.

Disabling JavaScript when using Capybara + Selenium

I have an app that's designed to still be functional when JavaScript is disabled, so I wanted to write some specs that covered those cases.
I'm using Selenium (Firefox) with Capybara and I'm registering an new driver with JavaScript disabled (via Selenium's javascript.enabled property)
# spec/rails_helper.rb
Capybara.configure do |config|
config.ignore_hidden_elements = true
config.default_driver = :selenium
end
Capybara.register_driver :disable_js do |app|
profile = Selenium::WebDriver::Firefox::Profile.new
profile["javascript.enabled"] = false
Capybara::Selenium::Driver.new(app, profile: profile)
end
# spec/features/siging_in_spec.rb
context "JavaScript disabled", driver: :disable_js do
it "user can still sign in" do
# ...
# ...
end
end
The feature specs are failing to actually disable JavaScript. When the browser window pops up during testing and I pause it with binding.pry, I can definitely click around on items I know require JavaScript and see them working.
Side note: If I actually go to my Firefox settings and disable JavaScript, the test passes. So it appears it's inheriting whatever configuration I set in my browser, and not actually using the configuration specified when registering the driver.
Is this the correct approach here, or is there something I missed?
Thanks!
It's not possible to change the javascript.enabled setting when registering the driver because selenium freezes it at true - https://github.com/SeleniumHQ/selenium/blob/master/javascript/firefox-driver/webdriver.json#L35 - This was done because of issues with trying to use selenium and firefox with JS disabled https://github.com/SeleniumHQ/selenium/issues/635 - and is unlikely to be changed. Can you just run those specific tests with the rack_test driver? or does it not provide enough functionality?
Unfortunately, setting profile["javascript.enabled"] = false no longer works.
An alternative is to install a Firefox addon that disables JavaScript. This worked for me with Firefox 45 ESR, selenium-webdriver (2.53.4), and capybara (2.8.1):
profile.add_extension(File.expand_path('../quickjava-2.1.0-fx.xpi', __FILE__))
# Configure the extension to disable JavaScript by default.
profile['extensions.thatoneguydotnet.QuickJava.startupStatus.JavaScript'] = 2
# Disable loading the extension's first-run tab.
profile['extensions.thatoneguydotnet.QuickJava.curVersion'] = '2.1.0'
I evaluated a few different addons, including NoScript and QuickJs, but decided to find a very simple addon that could disable JavaScript by default—QuickJava did the trick. You can download the XPI file here (use Firefox, right click and Save As instead of installing directly): https://addons.mozilla.org/en-US/firefox/addon/quickjava/versions/
You can also see all of the addon's pref settings in the source.

Configure Capybara to use Marionette WebDriver for Firefox

With Marionette replacing FirefoxDriver, I need to configure my tests to run it.
I've downloaded the binary but I can't seem to get my Capybara driver registration configured to actually use Marionette.
Capybara.register_driver :selenium_firefox do |app|
capabilities = Selenium::WebDriver::Remote::Capabilities.firefox
capabilities["firefox_binary"] = 'path/to/marionette/renamed/to/wires'
Capybara::Selenium::Driver.new(app, browser: :firefox, desired_capabilities: capabilities)
end
When I start a test though, I just get the initial page of FF just like I would trying to run it without marionette.
marionette is passed an option to Driver.new - not to desired_capabilities
Capybara.register_driver :selenium_firefox do |app|
Capybara::Selenium::Driver.new(app, browser: :firefox, marionette: true)
end
It also requires you to have downloaded geckodriver, put it in your path and renamed it to wires
A complete description of these configuration steps, including Marionette latest executables download links can be found here.
Note: Capybara does not yet support marionette, some things are being fixed in capybara, some are bugs in selenium-webdriver, and others are just general flakiness of it - things like it just stops selecting options from select elements - no errors thrown, just stops working. I don't think its ready for real world use yet.

Running multiple Rspec files in parallel with Selenium Grid

I have been working on a Rspec/Selenium Webdriver test framework where I need to run my Rspec tests distributed across multiple files in same directory spec/*_test.rb.I was looking for the existing solutions available and stumbled upon deep test gem (https://github.com/qxjit/deep-test) which helps driving the tests in parallel leveraging selenium grid but I was not able to implement it based on the documentation available and looks like there is no active development going on with it.Are there any ported version of deep test available to work with RSpec > 2.0.
I also looked into parallel_tests (https://github.com/grosser/parallel_tests) but not sure how we can use it for running multiple process on the same cpu with each process running a different rSpec test.
Here is a snippet from one of my spec file,
require 'selenium-webdriver'
require File.join(File.dirname(__FILE__),'../support/Setup')
require 'rspec'
require File.join(File.dirname(__FILE__),'../support/spec_helper')
require File.join(File.dirname(__FILE__),'../support/Helper')
describe "Test", :type => :selenium do
it "should search for flights" do
airline.home_page.queryFlight('oneWay', 'ATL', 'ORD', 'today')
end
it "should list all the flight results and select one flight" do
airline.flight_list_page.selectFlight
end
end
Similarly i have other spec files which I am trying to run in parallel.