Disabling JavaScript when using Capybara + Selenium - ruby-on-rails-3

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.

Related

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.

New browser opens for every feature being executed - WATIR

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.

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.

Run js-test-driver Eclipse plugin in IE8 document mode when having IE9 installed

I want to use the Eclipse plugin of js test driver to execute my tests in IE. I have IE9 installed but want the tests to run in IE8 document mode because this is what the app we are developing is running in.
So is there any way to start the js-test-driver plugin in IE8 document mode when having IE9 installed? Its possible to switch mode in MS Developer Tools but that is not what I'm looking for.
I've tried to load a script that adds meta tags at start up by adding a script like this in the jsTestDriver.conf file:
...
load:
- js/bootstrap-IE8.js
...
and the code in the bootstrap file:
(function(){
var meta = document.createElement('meta');
meta.setAttribute('http-equiv','X-UA-Compatible');
meta.setAttribute('content','IE=8');
var meta2 = document.createElement('meta');
meta2.setAttribute('Content-Type','X-UA-Compatible');
meta2.setAttribute('content','text/html; charset=utf-8');
var tophead = top.document.getElementsByTagName('head')[0];
var toptitle = tophead.firstChild;
tophead.insertBefore(meta, toptitle);
tophead.insertBefore(meta2, toptitle);
})();
I have tried to find a way to solve this as you describe have not found any way to do this (or JSTestDriver files to modify)
However, this can be done by modifing the registry by setting FEATURE_BROWSER_EMULATION feature that defines the default emulation mode for Internet Explorer to 8888 (0x22B8).
It is descibed at http://msdn.microsoft.com/en-us/library/ee330730%28VS.85%29.aspx#browser_emulation
Note that your browser will use IE8 document mode always.

How to deal with Java warning popup?

While running WebDriver automation scripts I came across a situation where it is trying to open a page which contain one segment with live camera (Made with Java applet). Once script reaches to this page - a Security Warning alert (with allow and not allow) shows up and blocks the execution process. Is this something that anyone faced before - actually I am looking for an option to block this security warning to get displayed on the page.
A popup is coming where i want to click on the "Allow". How to move the focus to the new popup window and click on Allow.
Can anyone please help me for the above problem?
I was having problems accepting the java applet "Allow"
My solution was to create a firefox profile that had the settings to always activate the plugin:
FirefoxProfile fp = new FirefoxProfile();
fp.setAcceptUntrustedCertificates( true );
fp.setPreference( "security.enable_java", true );
fp.setPreference( "plugin.state.java", 2 );
WebDriver d = new FirefoxDriver( fp );
Where plugin.state.java:
plugin.state.java = 0 --> never activate
plugin.state.java = 1 --> ask to activate
plugin.state.java = 2 --> always activate
This might get you closer...
Selenium uses a different firefox profile because Java was inactive for me and I did not have my firebug plugin in the Firefox browser Selenium launched. I would have to open another Firefox to use Firebug.
I found my default Firefox profile by searching %appdata% in the start menu then clicking on Roaming/ Mozilla/ Firefox/ Profile/ and then it gave my default profile name.
You can also open the firefox help menu (? logo) & click troubleshoot info... click Show Profile Folder
I then configured selenium to use my default profile so Java was enabled and Firebug was available in the browser Selenium launched:
Make sure that you use "/" in selenium even though it may use "\" in the windows path location
fp = webdriver.FirefoxProfile('C:/Users/xxx/AppData/Roaming/Mozilla/Firefox/Profiles/41s7nq9o.default')
driver = webdriver.Firefox(fp)
driver.get('www.stackoverflow.com')
where 41s7nq9o.default is the name of your default profile