Bypassing certificate error with Watir-WebDriver in Selenium Grid environment - ruby

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>/")

Related

how to show element in browed page in selenium testing?

I tried this to do selenium testing
require 'watir'
b = Watir::Browser.start 'http://www.gmail.com'
t = b.text_field id: 'entry_1000000'
t.exists?
t.set 'your name'
t.value
but not fetching any text in browed page(www.gmail.com).
require 'watir'
b = Watir::Browser.start 'http://www.gmail.com'
t = b.text_field id: 'entry_1000000'
t.exists?
t.set 'your name'
t.value
put exact id of that text field . id or xpath or name etc
First lets check if you have whats needed for that script to run, and start from there if not.
Ruby installed
Watir gem installed
Browser installed (chrome or firefox)
Geckodriver downloaded and in path (if firefox installed)
Chromedriver downloaded and in path (if chrome installed)
After that, save the code below in file, lets name it gmail.rb.
require 'watir'
b = Watir::Browser.new :chrome #or :firefox
b.goto "www.gmail.com"
b.text_field(:id => 'identifierId').set "your_email#gmail.com"
b.span(:text => "Next").click
b.text_field(:name => "password").set "your_password"
b.span(:text => "Next").click
Open command prompt or terminal and run the following command from folder where gmail.rb is located
ruby gmail.rb
Browser should open, navigate to gmail, input email, password and submit it, but from there gmail security kicks in so this is not a good use case for automation, at least not this way.
You can try the code above and see how it works, and if not, post errors here. But if you actually need to automate reading gmail, there is a really nice gem that helps you do that https://github.com/gmailgem/gmail

Selenium Webdriver with Chrome dont load Widevine Content Decryption Module on MacOS

I have a problem with chrome not loading the "Widevine Content Decryption Module" under "chrome://plugins/" when i run my test suites in the MacOS operating system, but it's working fine on linux and windows machines. So it's kind of a way chromedriver opens the browser in MacOS as it seems.
Also under "chrome://components/" there is no item, compared to the ones I have when I run chrome manually
I have the following line in my test suite (writed in the Ruby language):
#driver = Selenium::WebDriver.for :chrome
The problem seems to be that under MacOS, chromedriver flag "disable-component-update" blocks "Widevine Content Decryption Module".
I've solved it adding this lines to the webdriver selection section of my test suites:
caps = Selenium::WebDriver::Remote::Capabilities.chrome("chromeOptions" => {"excludeSwitches" => [ "disable-component-update" ]})
#driver = Selenium::WebDriver.for :chrome, desired_capabilities: caps
And I can now playback DRM content on MacOS with Selenium.

Unable to automate (AutoIT) in Ruby because of Watir error

I've tried a few times now to run a Watir browser and then use the AutoIt ruby library (au3) to access a right click context menu but it wasn't working, turns out the au3 library is disappearing for some reason (I'm a little new to Ruby but when I require it again after the browser opens it comes back false for some reason:
irb(main):001:0> require "au3"
=> true
irb(main):002:0> require "watir-webdriver"
=> true
irb(main):003:0> browser = Watir::Browser.new :chrome
Starting ChromeDriver (v2.3) on port 9515
[4868:5640:1025/104947:ERROR:textfield.h(176)] NOT IMPLEMENTED
=> #<Watir::Browser:0x449008c8 url="about:blank" title="about:blank">
<to "https://github.com/lmmx/watir-paper-scanner/blob/master/bookworm.rb"
[WARNING:..\..\..\..\flash\platform\pepper\pep_module.cpp(63)] SANDBOXED
=> "https://github.com/lmmx/watir-paper-scanner/blob/master/bookworm.rb"
irb(main):005:0> require "au3"
=> false
irb(main):006:0>
I'm guessing that whatever's SANDBOXED is crucial to running au3? The browser still works fine... Will try and update the watir-related things, but I only installed it a week or 2 so it shouldn't be out of date already - anyone help me fix it?
The reason it's returning false is because you have already used
require "au3"
on the top line of your code, so when you require it again it's already there.

Why does accessing a SSL site with Mechanize on Windows fail, but on Mac work?

This is the code I'm using to connect to the SSL site.
require 'mechanize'
a = Mechanize.new
page = a.get 'https://site.com'
I"m using using Ruby 1.9.3 and Mechanize 2.1pre1 + dependencies. On Mac the above code works and returns the page. On windows 7 running the same versions it gives me the following error:
OpenSSL::SSL::SSLError: SSL_connect returned=1 errno=0 state=SSLv3
read server certificate B: certificate verify failed
Reverting to Mechanize 2.0.1 seems to solve this problem, but I then get plagued with the too many connections reset by peer problem. Thus that is not a solution.
I've tried doing a.verify_mode = false, but that does not do anything. I have read that you can turn off SSL verification by using:
open(uri,:ssl_verify_mode => OpenSSL::SSL::VERIFY_NONE)
How can I turn it off in Mechanize ? Why am I only getting this error on Windows ?
The version of OpenSSL (the library used to establish secure connections with Net::HTTPS) is not able to properly find the certificate chain in your computer.
To our bad, OpenSSL was never able to use the Windows installed cert storage to validate remote servers so is failing because of that.
From your example, you can do:
a.agent.http.verify_mode = OpenSSL::SSL::VERIFY_NONE
To avoid the verification, however that is far from ideal (due clear security issues)
I recommend you download some cert bundles (like the ones from curl):
http://curl.haxx.se/ca
And modify your code to something like this:
require "rbconfig"
require "mechanize"
a = Mechanize.new
# conditionally set certificate under Windows
# http://blog.emptyway.com/2009/11/03/proper-way-to-detect-windows-platform-in-ruby/
if RbConfig::CONFIG["host_os"] =~ /mingw|mswin/
# http://curl.haxx.se/ca
ca_path = File.expand_path "~/Tools/bin/curl-ca-bundle.crt"
a.agent.http.ca_file = ca_path
end
page = a.get "https://github.com/"
That seems to work, Ruby 1.9.3-p0 (i386-mingw32), Windows 7 x64 and mechanize 2.1.pre.1
Hope that helps.
Luis' answer looks fine but more generally:
OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
You can simply do the following:
agent = Mechanize.new
agent.verify_mode = OpenSSL::SSL::VERIFY_NONE
This worked on the latest version 2.8

Automating authentication with Watir

I've searched and searched for this, but I can't seem to get this automation to work. Having used all of the basic authentication code on the OpenQA site, I still cannot get the authentication box to work.
I'm using IE8, with a website that has HTTPS enabled.
By using Watir I'm able to open IE to the correct page, but nothing I try allows me to enter any content into the login form.
Here is the code I've whittled it down to:
require 'watir'
url = 'https://thewebsite.com'
#username = 'myusername'
#password = 'mypassword'
browser = Watir::Browser.new
browser.goto url
sleep 5
Watir.autoit.WinWait('Blank Page')
Watir.autoit.Send(#username)
Watir.autoit.Send('{TAB}')
Watir.autoit.Send(#password)
Watir.autoit.Send('{ENTER}')
Does anyone have any suggestions, or links? A lot of the information I've found on the OpenQA site seems quite out of date.
Thanks
Did you try RAutomation instead of autoit?
Have you tried using the url with user and pass in it? like url = 'https://username:password#thewebsite.com', you can try visiting the url manually in a browser, if it works manually it should work in your script too.
I came here having the same problem, although it looks like the answer is different due to the latest version of Watir and watir-webdriver. I'll show what worked for me using:
watir (4.0.2 x86-mingw32)
watir-classic (3.6.0)
watir-webdriver (0.6.2)
Watir doesn't have autoit built in any more and it looks like the other suggestion I found (require 'watir/ie') doesn't work any more either. In the spirit of solving this with the original tech requested:
Make sure after installation AutoIT has been registered with windows.
Go to the AutotIT dll (installed with the rautomation gem mentioned above, think Watir installed this)
cd C:\Ruby193\lib\ruby\gems\1.9.1\gems\rautomation-0.8.0\ext\AutoItX
regsvr32 AutoItX3.dll
Then the code below should work
require 'watir'
require 'win32ole'
$b = Watir::Browser.new :ie
begin
$b.goto( 'http://10.254.157.34:8383/mywebsite/stuff.html');
rescue Exception => e
puts "Trapped Error, expecting modal dialog exception"
puts e.backtrace
puts "Continuing"
end
login_title = "Windows Security" #For Windows 7, dialog title for anything else
username = "myuser"
password = "mypassword"
sleep 1 #Just in case
au3 = WIN32OLE.new("AutoItX3.Control")
win_exists = au3.WinWait(login_title, "", 5)
if (win_exists > 0)
au3.WinActivate(login_title)
au3.Send('!u')
au3.Send(username)
au3.Send('{TAB}')
au3.Send(password)
au3.Send('{ENTER}')
end

Resources