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

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.

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

Watir WebDriver browser object loses current window upon opening new tab (FIREFOX)

I want to test clicking on multiple links on the same page, but then be able to switch active tabs. However once I click on a link with target=_blank or if I open a new tab on purpose, it routes me to a new tab and I can't get back to the old tab.
Note: I am using Firefox for testing. Chrome does appear to work properly and provides a window count of 2
Here is a simple IRB snippet to show what I mean:
2.2.1 :001 > require 'watir-webdriver'
=> true
2.2.1 :002 > b = Watir::Browser.new :firefox, :profile => 'default'
=> #<Watir::Browser:0x..fa8c7116334ddce82 url="about:blank" title="">
2.2.1 :003 > b.goto 'amazon.com'
=> "http://amazon.com"
2.2.1 :004 > b.a(:class => 'nav-logo-link').click(:command, :shift)
=> nil
2.2.1 :005 > b.windows.count
=> 1
I see two tabs opened, which the (:command, :shift) option is meant to do.. but how do I go back to the first one if the browser object only has 1 window?
It just seems that I can't find anyone having the same problem so perhaps its a firefox issue or my environment maybe?
Running on Mac OSX, FF version 44, Ruby 2.2.1, watir-webdriver 0.9.1
Check this out:
require 'watir-webdriver'
b = Watir::Browser.new
b.goto "amazon.com"
link = b.a(:class => 'nav-logo-link')
b.execute_script("return arguments[0].target = '_blank'", link)
link.click
puts b.windows.count
Main idea is not to try to use a link in a special way to get two windows but modify the link to get two windows in a natural way.

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

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.

Watir-webdriver: Chrome unhandled error on site where IE and Firefox working fine

So I think this might be an error with the ChromeDriver but wanted to ask here first.
I'm having trouble using just about every webdriver function with chrome, yet IE and Firefox is working fine.
The site is www.bing.com's sign in with Windows live site.
https://login.live.com/login.srf?wa=wsignin1.0&rpsnv=11&ct=1314808262&rver=6.0.5286.0&wp=MBI&wreply=http:%2F%2Fwww.bing.com%2FPassport.aspx%3Frequrl%3Dhttp%253a%252f%252fwww.bing.com%252f&lc=1033&id=264960
Once at that site when using chrome the only two functions I can get to work properly are
#browser.title
#browser.html
are the only functions that work properly. When I try
#browser.links.size
#browser.button[0].flash
#browser.button[0].click
I get
Selenium::WebDriver::Error::UnhandledError:
Using :id etc to reference the objects doesn't work either.
However IE and Firefox can interact with the site fine.
If you need code or clarification let me know, but basically I can't get chrome to interact with that log in site. (Other sites work just fine on chrome)
You will need to install the ChomeDriver, which involves downloading the binary and putting it on your PATH. See: http://watirwebdriver.com/chrome/
This site works perfectly for me in Chrome.
Eg.
$irb
ruby-1.9.2-p290 :001 > require 'watir-webdriver'
=> true
ruby-1.9.2-p290 :002 > b = Watir::Browser.new :chrome
=> #<Watir::Browser:0x6c6e06ba1a564e50 url="about:blank" title="about:blank">
ruby-1.9.2-p290 :003 > b.goto "https://login.live.com/login.srf?wa=wsignin1.0&rpsnv=11&ct=1314808262&rver=6.0.5286.0&wp=MBI&wreply=http:%2F%2Fwww.bing.com%2FPassport.aspx%3Frequrl%3Dhttp%253a%252f%252fwww.bing.com%252f&lc=1033&id=264960"
=> "https://login.live.com/login.srf?wa=wsignin1.0&rpsnv=11&ct=1314808262&rver=6.0.5286.0&wp=MBI&wreply=http:%2F%2Fwww.bing.com%2FPassport.aspx%3Frequrl%3Dhttp%253a%252f%252fwww.bing.com%252f&lc=1033&id=264960"
ruby-1.9.2-p290 :004 > b.title
=> "Welcome to Windows Live"
ruby-1.9.2-p290 :005 > b.html
=> "<!-- ServerInfo: BAYIDSLGN1M37 2011.07.22.19.28.58...

Resources