Setting the headless option for my selenium driver - ruby

I'm setting up tests using rspec and capybara within wsl2. I have had to use a remote selenium server because wsl2 can't open browser windows. I would like to set the headless option on my configuration but I am unable to find documentation that would help me.
My code is as follows within rails_helper.rb:
`
Rspec.configure do |config|
config.before(:each, type: :system) do
driven_by :selenium, using: :remote,
options: {
browser: :chrome,
url: 'http://192.168.56.1:4444'}
end
end
`
I want to put something into options like args: 'headless', but that just gives me an unknown keyword error. I've tried looking up documentation in the webdrivers gem, chromedriver, rspec, selenium, and capybara. They either don't have anything or they will have a different format that won't work when I try to include it.
I can't think of any more places to look so I'm asking a question.

Related

Not able to change default download directory for chrome with selenium hub docker and ruby watir

After a few days of searching and experimenting with any of the solutions I could find online, I give up and want to get some help from the community.
Ruby gems (ruby 2.5.1):
watir 6.11.0
selenium-webdriver 3.4.1
Docker:
selenium/node-chrome-debug:3.14
selenium/hub:3.14
My ruby code:
prefs = {
download: {
prompt_for_download: false,
default_directory: download_directory
}
}
browser = Watir::Browser.new(:chrome, url: selenium_hub_url, options: {prefs: prefs})
Our set-up is:
Run a selenium/hub and a selenium/node-chrome-debug. Something that might be different is that we are mounting the /tmp of the base OS as /hosttmp/tmp in the node container
Make the selenium/node-chrome-debug talk to selenium/hub
Make the browser automation talk to the selenium/hub using the code provided above
The problem is that I was never able to set the default download directory. However, all other parts are working correctly. The VNC window shows the browser is working correctly despite the default download directory settings. It is always /home/seluser/Downloads
Things I have tried:
Other people's ideas such as different ways to specify the options and preferences. (e.g. using the Capabilities)
Docker security-related settings such as: --privileged --security-opt apparmor:unconfined --cap-add SYS_ADMIN
On the base OS, chmod 777 for the download_directory. The download_directory, for example, /tmp/tmp.123 on the base OS, which is mounted as /hosttmp/tmp/tmp.123 in the chrome node container, I could see it and make a few read/write operations in this folder inside the container or on the base OS
Tweaks about the interesting ruby symbol/string stuff when creating a Hash object.
Does anyone have more ideas about what could lead to this situation? What else I could try? And is there any log that I could refer to. There is no error or warning when running the code. Thanks in advance.
I'm using Java+Docker+Selenium+Chrome for automation test and also met similar issue with you. Please find my solutions below and try if it works for your case.
Don't set default download directory in the options, just leave "/home/seluser/Downloads" as it is.
When you start up the chrome node on docker, please add the parameter of volume that could transfer the downloaded files to the directory you want.
e.g. docker run -d -p 5900:5900 --link myhub:hub -v :/home/seluser/Downloads selenium/node-chrome-debug:3.14.0
In my case, the JDK environment and my test script is on Linux machine while the selenium webdriver & browser are all on docker, so once the file downloaded by browser it cannot saved directly on Linux machine, you have to mount the local directory with default directory on docker. Then you could find the file saved in the directory you want.
Thanks & Regards!
Jing
Did you define options = Selenium::WebDriver::Chrome::Options.new?
We use
options = Selenium::WebDriver::Chrome::Options.new
prefs = {
prompt_for_download: false,
default_directory: download_directory
}
options.add_preference(download: prefs)
and then you would want something like
browser = Watir::Browser.new(:chrome, url: selenium_hub_url, options: options)
But maybe the main problem is just that you are using
options: {prefs: prefs}
instead of
options: {download: prefs}
Okay, by digging into the source code of the Watir and Selenium-Webdriver, I think I know the 'root cause'.
I have created an issue since I am not sure if this is a bug or a 'feature' The issue
Also, I have a workaround for my case, in watir/capabilities.rb:
Change
#selenium_browser = browser == :remote || options[:url] ? :remote : browser
to
#selenium_browser = browser == :remote ? :remote : browser
This shouldn't be the final solution as it might not be a good idea. Will wait for what the Watir people say about this.

How to write a simple Cucumber script

I'm following the tutorial to run my first Cucumber script:
Feature: guru99 Demopage Login
In order to Login in Demopage we have to enter login details
Scenario:
Register On Guru99 Demopage without email
Given I am on the Guru99 homepage
When enter blank details for register
Then error email shown
I have the project in Idea but when I run it I get errors.
When using chrome:
Failed to open TCP connection to 127.0.0.1:9515 (No connection could be made because the target machine actively refused it.
I have no idea how to resolve it.
When using Firefox, the script successfully opens the browser but fails after that:
require 'watir'
require 'colorize'
Selenium::WebDriver::Firefox::Binary.path='C:\soft\Mozilla Firefox\firefox.exe'
case ENV['BROWSER']
when 'chrome'
browser = Watir::Browser.new :chrome
when 'firefox'
browser = Watir::Browser.new :firefox
end
Given(/^I am on the Guru99 homepage$/)do
#browser = Watir::Browser.new :firefox
#browser.goto "http://demo.guru99.com"
end
When(/^enter blank details for register$/) do
browser.text_filed(:name,"emaiid").set("")
browser.button(:name,"btnLogin").click
end
Then(/^error email shown$/) do
puts "Email is Required!".red
browser.close
end
And returns:
NoMethodError: undefined method `text_filed' for nil:NilClass
on this line:
browser.text_filed(:name,"emaiid").set("")
I found some references that I need to write a class to call a method. I tried it but didn't succeed.
Connection refused, I'm unsure but "Watir+Cucumber Connection refused" looks a fix.
Copy pasta:
AfterConfiguration do |config|
yourCodeStartUp() # Put your SETUP code here including the launch of webdriver
at_exit
yourCodeTearDown() # Put your CLOSING routine here
puts 'stopped'
end
end
The code error is a typo, it should be browser.text_field(...
Regarding the issue you are observing on chrome, it sounds like you need to update chromedriver (and make sure the exe is in PATH). If you are running chrome v56-58, you need ChromeDriver 2.29.
Regarding the NoMethodError: undefined method error, you have a typo when you call the text_field method (i.e. browser.text_filed).
Nope, I was mistaken, I had an older version of chromedriver. Now it's running in chrome as well. Thank you very much. Appreciate your time!
So, the answers are:
1. Update chromedriver.
2. Check your code for typos one more time.
Was really easy but took me a lot of time%

Headless operations don't work inside Sinatra route

I am using the headless and selenium-webdriver gems to launch a headless Firefox browser:
headless = Headless.new(
video: {
frame_rate: 12,
codec: 'libx264'
}
)
headless.start
driver = Selenium::WebDriver.for(:firefox)
With this code I can write the following:
driver.navigate.to("http://google.com")
Yet the following raises an error after I visit '/' in the browser:
get '/' do
driver.navigate.to("http://google.com")
erb :root
end
The error is as follows:
*** Errno::ECONNREFUSED Exception: Failed to open TCP connection to 127.0.0.1:7055 (Connection refused - connect(2) for "127.0.0.1" port 7055)
I'm pretty sure this is because the driver.navigate is not being called in the headless scope, therefore it can't connect to the Firefox instance.
I have also tried using the modular sinatra style, but the same error occurred.
workaround
What I ended up doing is separating the headless server in a separate script. This script has loop and gets input, printing the output of running the command in the headless scope. Then from the sinatra server, i use PTY.spawn to instantiate the server and pass around its stdin and stdout so I can use it in my Sinatra app. This way the headless script is only run once (therefore multiple headless servers aren't started) and I can connect to it from my Sinatra routes. I can't interact with the headless script's variables or methods directly - I need to just use i/o.
I am hoping for an answer which hows how to make the original code work, though (when the sinatra app's routes are called in headless scope)
One possible trick that can help:
this = self
get '/' do
this.driver.navigate.to("http://google.com")
erb :root
end
I ended up getting this working.
At first I thought that what fixed it was doing the headless environment initialization in the scope of a sinatra route, i.e.
get '/' { do_initialization_here }
The real fix may have been in the way I was calling Headless.new (I originally had a bunch of options tacked on and I removed all them).

Bypassing certificate error with Watir-WebDriver in Selenium Grid environment

I have setup a Grid Environment with 4 Windows ( IE8, IE9, IE10, IE11 ) VMs and an Ubuntu 12.04 VM with Chrome and Firefox.
Selenium Grid and Nodes are version 2.41.0
as for ruby , i am using rvm, and using ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-linux]
I am driving the tests from a CentOs VM. Now when i try to run IE8 Tests, i see the remote windows machine bring up IE8, but first get a message in browser:
"This is the initial start page for the WebDriver server."
and later i get another page with cert warning.
Certificate Error: Navigation Blocked
error:
The security certificate presented by this website was not issued by a trusted certificate authority.
Security certificate problems may indicate an attempt to fool you or intercept any data you send to the server
I tried to do the registry hack making all 4 security levels in the IE8 equal , by
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones
and equalize values for 0x2500 position in all zones.
But does not seem to work.
Any ideas here what a working solution is for ruby / watir-webdriver
Here is the base class
#!/usr/bin/env ruby -W0
require "rubygems"
require "test/unit"
require 'rspec'
require 'watir-webdriver'
require 'headless'
require 'mysql2'
require_relative 'lib/basic_ops'
require_relative 'data/data'
require_relative 'main_class'
include GC
caps = Selenium::WebDriver::Remote::Capabilities.ie
caps.version = "8"
caps[:name] = "IE 8 on win7 , port 5560"
BROWSER = Watir::Browser.new(
:remote,
:url => "http://selenium-hub-vm:4444/wd/hub",
:desired_capabilities => caps)
URL = "https://target-vm/"
BROWSER.window.resize_to(950, 750)
BROWSER.window.move_to(0, 0)
main_class
so main_class.rb is where all the browser interactions are.
You can use javascript to get passed the Security Certificate issue. I've never coded in Ruby before, so the syntax may be a bit off.
Python code:
driver.get("javascript:document.getElementById('overridelink').click()")
Ruby attempts:
driver.execute_script("document.getElementById('overridelink').click()")
driver.get "javascript:document.getElementById('overridelink').click()"
driver.get("javascript:document.getElementById('overridelink').click()");
You may want this in an if statement too so as to check for the condition you need.
i.e. if "Certificate Error" in driver.find_element_by_xpath("//title").text
There are similar solutions on the following thread if none of my suggestions work.
Hope this is helpful or close to what you need.
What i did was to add the cert , that did not remove the cert error, so i used:
#browser.link(:id, "overridelink").click
right after
#browser.goto("https://<URL>/")

Tell Cucumber tests to run on https

I am running cucumber tests with Webrat in external mode that is using Selenium. In production we are running behind https and so we are required to run our cucumber tests on https.
Is there any place we can specify that cucumber, webrat, or selenium needs to be using https? Ideally this could be specified through a parameter that is given to Webrat.
I have seen some stuff that looks like it might be possible if I override the default SeleniumClientDriver that comes bundled under the selenium.rb file.
env.rb =>
Webrat.configure do |config|
config.mode = :selenium
config.aplication_address = 'localhost'
config.aplication_port = 11090
config.selenium_server_address = 'localhost'
config.selenium_server_port = 4444
config.selenium_browser_key = '*iexploreproxy'
config.application_framework = :external
end
World do
session = Webrat::SeleniumSession.new
session.extend(Webrat::Methods)
session.extend(Webrat::Selenium::Methods)
session.extend(Webrat::Selenium::Matchers)
session
end
Thanks for any help!
This apparently is not something that the webrat team has decided to support.
So a team member changed the source to allow that this can be specified through env.rb properties. The pull request can be found here.

Resources