Unable to pass a profile to Capybara when creating a new driver - selenium-webdriver

I'm using a ruby/cucumber/capybara framework with versions:
capybara (2.10.1)
selenium-webdriver (3.0.0)
ruby 2.3.1p112 (2016-04-26 revision 54768) [x64-mingw32]
and I'm trying to register a new driver with some settings passed in the profile to capybara. My code looks like this:
Capybara.register_driver :debug do |app|
profile = Selenium::WebDriver::Firefox::Profile.new
Capybara::Selenium::Driver.new(app, :browser => :firefox, :profile => profile)
end
and I've tried as well with:
Capybara::Selenium::Driver.new(app, :profile => profile)
Then I just use the following to select that driver:
Capybara.default_driver = :debug
but in both cases, when I try to run any test, I get the following error:
ArgumentError: unknown option: {:profile=>#<Selenium::WebDriver::Firefox::Profile:0x000000068ca798 #model=nil, #native_events=true, #secure_ssl=false, #untrusted_issuer=true, #load_no_focus_lib=false, #additional_prefs={}, #extensions={}>}
Any idea what the problem could be? and how to ammend it?

Firefox Profiles are not yet supported in Ruby bindings for geckodriver, which is needed for Firefox 48+. Watch this issue for (hopefully very soon) resolution: https://github.com/SeleniumHQ/selenium/issues/2933

Related

In Selenium webdriver, for remote Firefox how to use OSS bridge instead of w3c bridge for handshaking

I am using selenoid for remote browser testing in ruby.
In that I am using 'selenium-webdriver', 'capybara', 'rspec' for automation. And I am using attach_file method for uploading file to browser
I want to upload file on Firefox and Chrome browser but it raises error on both;
In chrome
Selenium::WebDriver::Error::UnknownCommandError: unknown command: unknown command: session/***8d32e045e3***/se/file
In firefox
unexpected token at 'HTTP method not allowed'
So After searching I found the solution for chrome which is to set w3c option false in caps['goog:chromeOptions'] > caps['goog:chromeOptions'] = {w3c: false}
So now chrome is using OSS bridge for handshaking but I don't know how to do it in Firefox. Similar solution is not available for Firefox.
My browser capabilities are following:
if ENV['BROWSER'] == 'firefox'
caps = Selenium::WebDriver::Remote::Capabilities.new
caps['browserName'] = 'firefox'
# caps['moz:firefoxOptions'] = {w3c: false} ## It is not working
else
caps = Selenium::WebDriver::Remote::Capabilities.new
caps["browserName"] = "chrome"
caps["version"] = "81.0"
caps['goog:chromeOptions'] = {w3c: false}
end
caps["enableVNC"] = true
caps["screenResolution"] = "1280x800"
caps['sessionTimeout'] = '15m'
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app, browser: :remote,
:desired_capabilities => caps,
:url => ENV["REMOTE_URL"] || "http://*.*.*.*:4444/wd/hub"
)
end
Capybara.configure do |config|
config.default_driver = :selenium
end
I have found the problem. There is bug in selenium server which run on java so I have to change my selenium-webdriver gem version 3.142.7 and monkey-patch.
You can find more information here about the bug and resolution.
For now I have to change my gem and monkey patch the selenium-webdriver-3.142.7\lib\selenium\webdriver\remote\w3c\commands.rb file. check for below line which is on line no 150.
upload_file: [:post, 'session/:session_id/se/file']
and update it to
upload_file: [:post, 'session/:session_id/file']
i had a similar issue with rails 7. the issue is connected with the w3c standard. the core problem is that the webdriver for chrome uses a non-w3c standard url for handling file uploads. when uploading a file, the webdriver uses the /se/file url path to upload. this path is only supported by the selenium server. subsequently, the docker image provided by selenium works fine. yet, if we use chromedriver, the upload fails. more info.
we can solve this, by forcing the webdriver to use the standard-compliant url by overriding the :upload_file key in Selenium::WebDriver::Remote::Bridge::COMMANDS. since, the initialization of this the COMMANDS constant does not happen when the module is loaded, we can override the attach_file method to make sure the constant is set correctly. here the hacky code:
module Capybara::Node::Actions
alias_method :original_attach_file, :attach_file
def attach_file(*args, **kwargs)
implement_hacky_fix_for_file_uploads_with_chromedriver
original_attach_file(*args, **kwargs)
end
def implement_hacky_fix_for_file_uploads_with_chromedriver
return if #hacky_fix_implemented
original_verbose, $VERBOSE = $VERBOSE, nil # ignore warnings
cmds = Selenium::WebDriver::Remote::Bridge::COMMANDS.dup
cmds[:upload_file] = [:post, "session/:session_id/file"]
Selenium::WebDriver::Remote::Bridge.const_set(:COMMANDS, cmds)
$VERBOSE = original_verbose
#hacky_fix_implemented = true
end
end
In Firefox images we support /session/<id>/file API by adding Selenoid binary which emulates this API instead of Geckodriver (which does not implement it).

How to disable plain_text.wrap_long_lines option in geckodriver?

Using selenium-webdriver and capybara in rspec, in a features spec I'm trying to get the HTTP response of a plain text request, namely /robots.txt
But instead of getting the plain text response, I get the text response wrapped in HTML:
expected: "User-agent: *\nDisallow:\n\nSitemap: https://prj.org/sitemap.xml\n"
got: "<html><head><link rel=\"alternate stylesheet\" type=\"text/css\" href=\"resource://content-accessible/plaintext.css\" title=\"Wrap Long Lines\"></head><body><pre>User-agent: *\nDisallow:\n\nSitemap: https://prj.org/sitemap.xml\n</pre></body></html>"
When fetching /robots.txt with curl I get the expected plain text response. So I've been through Firefox options, and I found out I needed to disable plain_text.wrap_long_lines option.
And I cannot succeed to pass the option to geckodriver.
I first tried to pass it to the Options object, like this:
Capybara.register_driver :firefox_headless do |app|
options = ::Selenium::WebDriver::Firefox::Options.new
options.headless!
options.add_preference 'plain_text.wrap_long_lines', false
Capybara::Selenium::Driver.new app, browser: :firefox, options: options
end
Then I tried to pass it to a Profile object.
Capybara.register_driver :firefox_headless do |app|
options = ::Selenium::WebDriver::Firefox::Options.new
options.headless!
profile = Selenium::WebDriver::Firefox::Profile.new
profile['plain_text.wrap_long_lines'] = false
Capybara::Selenium::Driver.new app, browser: :firefox, options: options, profile: profile
end
In both cases, the result is the same. Any idea as per why? Thanks!
Using:
selenium-webdriver 3.14.1
capybara 3.7.2
geckodriver 0.22.0
The issue you're seeing here is that when Firefox opens a text file it automatically wraps it in some boilerplate html in order for the browser to be able to display it. You don't show your test code you're using - but whatever you're doing should boil down to something like
# If using minitest
visit('/robots.txt')
find('body > pre').assert_text("User-agent: *\nDisallow:\n\nSitemap: https://prj.org/sitemap.xml\n", exact: true)
# If using RSpec
visit('/robots.txt')
expect(find('body > pr')).to have_text("User-agent: *\nDisallow:\n\nSitemap: https://prj.org/sitemap.xml\n", exact: true)

"TypeError: can't dup NilClass" in Capybara + selenium driver

I tried following code
crawler = Capybara::Session.new(:selenium)
crawler.visit "https://twitter.com" # Error this line!
with
gem:
capybara (= 2.2.0)
selenium-webdriver (3.3.0)
WebBrowser:
FireFoex53.0
OS:
Mac OSX
then got following Error
TypeError: can't dup NilClass
[stack trace]
gems/selenium-webdriver-3.3.0/lib/selenium/webdriver/remote/w3c_capabilities.rb:101:in `dup'",
gems/selenium-webdriver-3.3.0/lib/selenium/webdriver/remote/w3c_capabilities.rb:101:in `json_create'",
gems/selenium-webdriver-3.3.0/lib/selenium/webdriver/remote/w3c_bridge.rb:116:in `create_session'",
gems/selenium-webdriver-3.3.0/lib/selenium/webdriver/remote/w3c_bridge.rb:76:in `initialize'",
gems/selenium-webdriver-3.3.0/lib/selenium/webdriver/firefox/w3c_bridge.rb:45:in `initialize'",
gems/selenium-webdriver-3.3.0/lib/selenium/webdriver/common/driver.rb:52:in `new'",
gems/selenium-webdriver-3.3.0/lib/selenium/webdriver/common/driver.rb:52:in `for'",
gems/selenium-webdriver-3.3.0/lib/selenium/webdriver.rb:87:in `for'",
gems/capybara-2.2.0/lib/capybara/selenium/driver.rb:13:in `browser'",
gems/capybara-2.2.0/lib/capybara/selenium/driver.rb:45:in `visit'",
gems/capybara-2.2.0/lib/capybara/session.rb:197:in `visit'"
url "https://twitter.com" is working when I manualy visit on this FireFox browser.
but capybara's visit method is does't work..
I ran into this error (TypeError: can't dup NilClass) when trying to use the selenium-webdriver gem as instructed:
require "selenium-webdriver"
driver = Selenium::WebDriver.for :firefox
As suggested by Thomas, updating the selenium-webdriver gem number from 3.3.x to 3.4.x resolves the issue.

What is the syntax for appium's caps? I'm using selenium and rubygems

I'm trying to run a script I wrote for selenium webdriver using RubyGems to test a mobile app.
I've done testing with websites, but I'm trying to test an app/apk. Unfortunately I'm not including my desiredCapabilities correctly.
In my code
require 'rubygems'
require 'selenium-webdriver'
require 'uri'
require 'appium_lib'
caps = Selenium::WebDriver::Remote::Capabilities.android
caps['deviceName'] = 'Awesome Fire'
caps['deviceOrientation'] = 'portrait'
caps['platformVersion'] = '4.4'
caps['platformName'] = 'Android'
driver = Selenium::WebDriver.for(:remote, :url => "http://127.0.0.1:4723/wd/hub", :desiredCapabilities => caps)
When I run the script I receive the following msg
unknown option: {:desiredCapabilities=>#<Selenium::WebDriver::Remote::Capabilities:0x66a380 #capabilities={:browser_name=>"android", :version=>"", :platform=>:android, :javascript_enabled=>true, :css_selectors_enabled=>false, :takes_screenshot=>true, :native_events=>false, :rotatable=>true, :firefox_profile=>nil, :proxy=>nil, "deviceName"=>"Awesome Fire", "deviceOrientation"=>"portrait", "platformName"=>"Android"}>} (ArgumentError)
I'm sure how I define my caps is the problem, but I'm unable to find the correct syntax online.
I finally figured it out. Here is my sample
require 'rubygems'
require 'selenium-webdriver'
require 'uri'
require 'appium_lib'
require_relative 'SDK_Navigation'
mySampleApp = SampleApp.new
caps = Selenium::WebDriver::Remote::Capabilities.android
caps['deviceName'] = 'fegero'
caps['platformName'] = 'Android'
caps['app'] = 'c:\users\myfolder\documents\SampleApp_1046.apk'
driver = Selenium::WebDriver.for(
:remote,
:url => "http://127.0.0.1:4723/wd/hub",
:desired_capabilities => caps)
sleep(20)
mySampleApp.PickImagebtn(driver)

Change Browser Capabilities through Robot Framework

I do not have privileges to change IE settings locally. I wrote a Java Code to change the capabilities of IEDriver using :
DesiredCapabilities caps = DesiredCapabilities.internetExplorer(); caps.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
caps.setCapability(
InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,
true);
I want to do the same thing while using the selenium webdriver in Robot Framework. I want to do something like this. But I do not know the right way to do it.
*** Keywords ***
Test Browser
${options}= Evaluate sys.modules['selenium.webdriver'].DesiredCapabilities.INTERNETEXPLORER sys,selenium.webdriver
Call Method ${options} add_argument INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS:True
Create WebDriver Internet Explorer ie_options=${options}
Open Browser To Login Page
Open Browser ${LOGIN URL} ${BROWSER}
Maximize Browser Window
Set Selenium Speed ${DELAY}
Login Page Should Be Open
Thanks a lot!
In the Selenium documentation for DesiredCapabilities, the configurable properties are listed. The required property is ignoreProtectedModeSettings which must be set to True
${dc} Evaluate sys.modules['selenium.webdriver'].DesiredCapabilities.INTERNETEXPLORER sys, selenium.webdriver
Set To Dictionary ${dc} ignoreProtectedModeSettings ${True}
Open Browser www.google.com ie desired_capabilitie=${dc}
${s2l}= Get Library Instance Selenium2Library
Log Dictionary ${s2l._current_browser().capabilities} # actual capabilities
For anyone that came here looking for a solution to this problem within Robot Framewor:
Set Chrome Desired Capabilities
[Documentation] Create the desired capabilities object with which to instantiate the Chrome browser.
${dc} Evaluate sys.modules['selenium.webdriver'].DesiredCapabilities.CHROME sys, selenium.webdriver
${experimental_options} Create Dictionary useAutomationExtension ${False}
Set To Dictionary ${dc} chromeOptions ${experimental_options}
Set Global Variable ${DESIRED_CAPABILITIES} ${dc}
As Pavol Travnik mentioned, David's answer no longer works. At some point the ignoreProtectedModeSettings key got placed inside the se:ieOptions dictionary within the capabilities dictionary. Here is code that will work with newer versions of the IEDriverServer:
${ie_dc} = Evaluate
... sys.modules['selenium.webdriver'].DesiredCapabilities.INTERNETEXPLORER
... sys, selenium.webdriver
${ieOptions} = Create Dictionary ignoreProtectedModeSettings=${True}
Set To Dictionary ${ie_dc} se:ieOptions ${ieOptions}
Open Browser ${url} ie desired_capabilities=${ie_dc}
You can see this if you debug Selenium's Python library, specifically webdriver/remote/webdriver.py and look at the response in start_session.

Resources