Automating authentication with Watir - ruby

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

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

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%

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

how to download file to a specific location on .click of watir?

when I click on a link of a web page in chrome web browser with ruby watir
ie.input(:id, "c_ImageButton_Text").click
.text file start downloading. I want to download it to a specific location.I tried this code,but still getting.text file in default download location.How can I download .text file to specific location?
download_directory = "#{Dir.pwd}/downloads"
download_directory.gsub!("/", "\\") if Selenium::WebDriver::Platform.windows?
profile = Selenium::WebDriver::Chrome::Profile.new
profile['download.prompt_for_download'] = false
profile['download.default_directory'] = download_directory
b = Watir::Browser.new :chrome, :profile => profile
click here for this code
For latest watir version 6.6.1
prefs = {
prompt_for_download: false,
default_directory: Rails.root.join('tmp').to_s
}
options = Selenium::WebDriver::Chrome::Options.new
options.add_preference(:download, prefs)
browser = Watir::Browser.new :chrome, options: options
As the old saying goes, "There is more than one way to skin a cat":
You can certainly try to skin it the way you are attempting to do in your example code. However, there are a number of issues with this course of action. Namely, it is browser specific.
I really like how Elemental Selenium suggests achieving valid tests and assertions on downloading files by using the rest-client gem to curl the file's URI and assert the expectations of the file from from there: http://elemental-selenium.com/tips/8-download-a-file-revisited

Watir Webdriver Load Chrome Extension

I'm trying to load a chrome extension with Watir, and I'm having issues.
I found this related question: Ability to launch chrome with extensions loaded with watir-webdriver. However, I am still having the same issue after following that.
require 'rubygems'
require 'watir-webdriver'
require 'ruby-debug'
require 'nokogiri'
browser = Watir::Browser.new :chrome, :switches => %w[--load-extension=~/.config/google-chrome/Default/Extensions/anepbdekljkmmimmhbniglnnanmmkoja/0.1.12_0]
sleep(10)
browser.close
I also tried copying the extension from /Extensions to /Desktop and loading from there to no avail.
The error I get is Could not load extension from ... Manifest File Missing or Unreadable.
The Manifest file does indeed exist and seems to be a correct file in JSON format.
Trying to load different extensions fails as well.
Download the chrome extension crx file,
Store the args or any other option need to pass in the watir_opts hash
watir_opts[:extensions] = ['path of *.crx file']
browser = Watir::Browser.new :chrome, options: watir_opts
This worked for me.
Note: I didn't encode using 'base64' gem
If you pack the extension and then base64 it, you can load it into the Chrome browser right from your ruby code.
Pack your extension into a *.crx file. You can follow this guide, or just google how to pack a chrome extension.
Base64 it then add it to your desired capabilities list. You could use some code similar to this one:
chrome_extensions = []
chrome_extension_path = '\home\user\packed_chrome_extension.crx'
begin
File.open(chrome_extension_path, "rb") do |file|
chrome_extensions << Base64.encode64(file.read.chomp)
end
rescue Exception => e
raise "ERROR: Couldn't File.read or Base64.encode64 a Chrome extension: #{e.message}"
end
# Append the extensions to your capabilities hash
my_capabilities.merge!({'chrome.extensions' => chrome_extensions})
desired_capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(my_capabilities)
browser = Watir::Browser.new(:remote, :url => 'http://localhost:4444/wd/hub' :desired_capabilities => desired_capabilities)
And don't forget to require 'base64' too.
The example is for a remote web-driver instance, but I think it should work when using web-driver locally too. Just adjust the arguments passed to Watir::Browser.new.

Resources