Monday, October 1, 2012

JavaScript Dialogs with Web Drivers


JavaScript dialogs are fairly common in web applications.Watir and Watir-Webdriver have different methods for dealing with JS dialogs.

Webdriver has an Alerts API, and Watir-Webdriver has an alerts helper which makes use of that, with a few different methods in it to deal with various styles of javascript popups.Below are the commands to tackle different dialogs using web drivers


JavaScript Alerts
# Check if alert is shown
browser.alert.exists?

# Get text of alert
browser.alert.text

# Close alert
browser.alert.ok
browser.alert.close

JavaScript Confirms
# Accept confirm
browser.alert.ok

# Cancel confirm
browser.alert.close

JavaScript Prompt
# Enter text to prompt
browser.alert.set "Prompt answer"

# Accept prompt
browser.alert.ok

# Cancel prompt
browser.alert.close

If you are not on web drivers then you can use traditional .click and no_wait variants to get rid of them.

Tuesday, September 11, 2012

Grid Continued...


WatirGrid is Distributed

Built with standard DRb packages. Completely written in Ruby using core libraries.Lets you control Watir objects remotely with transmission passed by reference.

The standard Ruby library ships with a package known as DRb, it means Distributed Ruby. DRb is an incredibly easy package to learn and use. It has the benefits of being written completely in Ruby and using core libraries. It also offers advantages such as automatic selection of object transmission (either pass by value or pass by reference), reasonable speed, and the ability to run on any operating system that runs Ruby. 

WatirGrid is Parallel

WatirGrid uses Threads to execute Watir test cases in parallel (sort of). Threads execute on remote Watir objects, offloading any processing overheads

threads = []
  grid.browsers.each_with_index do |browser, index|
    threads << Thread.new do
      ...
    end
  end
threads.each {|thread| thread.join}


Key Terminology

Controllers implements a repository of tuples (tuple space) that can be accessed concurrently. The controller hosts the ring server which advertises these tuples across a grid network. Typically you will host one controller on a central machine.

Providers make remote Watir objects available to the tuple space hosted by the ring server. Typically you will host one or many providers on your grid network, for example, each PC may become a single provider of a Watir tuple in the form of an Internet Explorer, Firefox or Safari browser object.

Example:

require 'watirgrid'
require 'pp'
# Start a Controller

controller = Controller.new
controller.start
# Start a Provider
provider = Provider.new(:browser_type => 'safari')
provider.start
grid = Watir::Grid.new
grid.start(:take_all => true)
pp grid.browsers.first
# Take the first browser on the grid and execute some Watir
browser = grid.browsers.first[:object].new_browser
browser.goto "http://google.com"
browser.close


Thursday, September 6, 2012

WatirGrid

WatirGrid allows for distributed testing across a grid network using Watir.WatirGrid allows a local client to control remote Watir objects in parallel, hosted by providers on a grid network, via a central controller.

The controller implements a repository of tuples (tuple space) that can be accessed concurrently. The controller also hosts a ring server which advertises these tuples across a grid network making it loosely coupled.
Typically you will host one controller on a central machine. You will normally connect to this controller via a contoller_uri. You can also find this controller by its ring server, using a UDP broadcast for the ring server port.
The providers make remote Watir objects available to the tuple space hosted by the controller. Typically you will host one or many providers on your grid network, for example, each PC may become a single provider of a Watir tuple in the form of an Internet Explorer, Firefox, Safari or WebDriver browser object.

WatirGrid IS

  • A lightweight, pure Ruby implementation of distributed computing using DRb and Rinda.
  • A simple way to control remote Watir objects in parallel.
  • Cross platform friendly and works on Windows, OSX or Linux.
  • Open source, you’re already looking in the right place if you want the source code.
  • WebDriver friendly, thanks to the wire protocol, WatirGrid happily runs with WebDriver implementations such as watir-webdriver and selenium-webdriver.

    More Coming soon...

Wednesday, September 5, 2012

Dealing With Browser Certificates


IE:

The watir documentation has a workaround for this:

my_browser.link(:id, "overridelink").click

Another solution for this is to use autoit to tab into the 'continue to website', saves having to add to website all the time

    autoit=WIN32OLE.new('AutoItX3.Control')
    i=1
    while i < 11 
        autoit.Send("{Tab}")
        i+=1
    end

    autoit.Send("{Enter}")

Firefox:

The Firefox driver properly handles untrusted certificates by default.

If you have a trusted certificate, but there is some other certificate error such as a hostname mismatch (eg. using a production certificate in test), you should do the following:

profile = Selenium::WebDriver::Firefox::Profile.new
profile.assume_untrusted_certificate_issuer = false
b = Watir::Browser.new :firefox, :profile => profile
The reason this is needed is explained here.

Chrome:

It is easy to ignore invalid Browser certificates in Google Chrome by passing a command line switch:

Watir::Browser.new :chrome, :switches => ['--ignore-certificate-errors']

Thursday, March 29, 2012

Headless gem


The headless gem is a ruby wrapper for Xvfb, the virtual frame buffer.Virtual frame buffers are used to run graphical software on a “headless”, i.e. display-less, server.Headless gem makes it possible to create and release frame buffers straight from Ruby code. 

This gem makes it easy to run graphical applications (such as real web browsers) on a headless Linux machine. 

Command to install this gem:

gem install headless

An Example:

require 'watir-webdriver'
require 'headless'
headless = Headless.new
headless.start
b = Watir::Browser.start 'www.google.com'
puts b.title
b.close
headless.destroy

Finally, running WebDriver headless tests requires you to run a Selenium Server, which is additional overhead for your tests.


But why headless?


Whilst running automated browser tests in a headless form can speed things up, the main reason,  people wanting headless watir-web driver support is so that tests can be run on headless Linux machines, for example, a Jenkins Server.

Stop loading browser page

Sometimes the page is loading too slow, and we get a timeout error from watir-webdriver. Like in the below case:

require 'watir-webdriver'
browser = Watir::Browser.new :chrome
begin
  browser.goto("any url for a slow site")
 # Raise Timeout exception in 60 sec
rescue
  browser.send_keys(:escape) # => Raise another Timeout exception
end

Browser just refuses to respond to any othere commands before it finished loading the page. AS in this case the escape command is never executed until page loading is done.

This can be solved in below way:

require 'watir-webdriver'

client = Selenium::WebDriver::Remote::Http::Default.new
client.timeout = 60 
@browser = Watir::Browser.new :chrome, :http_client => client

begin
  @browser.goto mySite
rescue => e
  puts "Browser timed out: #{e}"
end

next_command

I hope this helps...

Monday, January 9, 2012

Adding Html tags to Watir libraries

Html tags to be verified in html page are maintained in non_control_elements.rb file in watir libraries under location Ruby location\lib\ruby\gems\1.9.1\gems\watir-2.0.4\lib.

If you have the framework for tags verification and some tags are not identified signifies that it is not there in non_control_elements. Here is the easy way to do it, just add below code to the end of this file

class LEGEND < NonControlElement
    TAG = 'LEGEND'
  end
  class TH < NonControlElement
    TAG = 'TH'
  end
  class BR < NonControlElement
    TAG = 'BR'
  end

With watir version 2.0.4 this has slightly changed and you need to do below:

%w[Pre P Div Span Map Area Li Ul H1 H2 H3 H4 H5 H6
     Dl Dt LEGEND TH TD Dd Strong Em Del Font Meta Ol].each do |elem|
    module_eval %Q{
      class #{elem} < NonControlElement; end
    }

This is it and you should be good to verify them in your framework. Hope it helps.