Ruby · selenium · Selenium-webdriver · watir · yaml

Configuration Management using YAML file for Watir/Selenium web-driver related frameworks

Requirement:

In watir/selenium webdriver related automation framework, how can we run the same script in multiple environments.

Code:

config.yml:

Beta:
url: "beta.google.com"

Gamma:
url: "gamma.google.com"

Prod:
url: "google.com"

Code.rb

require 'rubygems'
require 'yaml'
require 'watir-webdriver'
$env= ARGV[0]
config = YAML.load_file("config.yml")
$browser=Watir::Browser.new :ie
puts "Navigating to #{config[$env]["url"]}"
$browser.goto("#{config[$env]["url"]}")

Output:

in CMD PROMPT>ruby code.rb Beta

Navigating to beta.google.com

in CMD PROMPT>ruby code.rb Prod

Navigating to google.com

html to pdf · pdf · Ruby · watir-webdriver

Convert HTML webpage to PDF using WatirWebDriver + Pdfcrowd

Installation

Sign up and obtain your username and API key — https://pdfcrowd.com/user/sign_in/

$ gem install pdfcrowd
Or download pdfcrowd-2.3.0.gem and run

Get the API key from your account page

Code:

require 'rubygems'
require 'pdfcrowd'
require 'watir-webdriver'

# create an API client instance
username="YOUR USERNAME"
apikey="YOUR API KEY"
client = Pdfcrowd::Client.new(username, apikey)

# convert an HTML string and save the result to a file
browser=Watir::Browser.new :ff
browser.goto("https://raveendran.wordpress.com")
html=browser.html
File.open('html.pdf', 'wb') {|f| client.convertHtml(html, f)}

# retrieve the number of tokens in your account
ntokens = client.numTokens()

Output:

After ran the script, you will get the html.pdf file in the same directory.

cheat sheet · QA · Ruby · Ruby 1.9 · ruby excercise · selenium · selenium-webdriver · Selenium-webdriver · watir · watir-webdriver

Custom HTML report for Ruby(Watir/Selenium) Base Automation framework

Requirement:

Sample HTML report for any ruby based Automation Testing Framework

Installation/Changes :

1. Create code.rb file and paste the below code.

2. Create folder named as Results in the same location.

Code:

def get_time_name
$time=Time.now
$time_name="#{$time.hour.to_s}-#{$time.min.to_s}-#{$time.sec.to_s}-#{$time.day.to_s}-#{$time.mon.to_s}-#{$time.year.to_s}"
$result_date = "#{$time.day.to_s}-#{$time.month.to_s}-#{$time.year.to_s}"
end
def create_report
get_time_name
@result_file_name="Report"+"-"+$time_name
@full_file_name="Results/#{@result_file_name}.html"
$report=File.open(@full_file_name,'w')
end
def insert_head_title(title)
$report.puts "<html><head>
<title> #{title} </title>
</head>"
end
def start_table
$report=File.open(@full_file_name,'a')
$report.puts "<table border=1>
<tr>
<th>Test Case Name</th>
<th>Test Case Description</th>
<th>Browser Name</th>
<th>Result</th>
<th>Remarks</th>
</tr>"
$report.close
end
def insert_reportname_date(name,date)
$report.puts "<body bgcolor='#5CB3FF'>
<p align='left' size=2>
<b><img src='https://encrypted-tbn1.google.com/images?q=tbn:ANd9GcQB0l0xnGOHuRPFMMMi-OVg39nfAU1Ogvxr7Okk7DD8ZpqlMF9r'></img> </b>
</p>
<p size=12>
<center> <b><u>#{name} </u></b></center>
</p>
<p align='right' size=12>
<b>Date: #{date} </b>
</p>"
$report.close
end
def report_row(*details)
$report=File.open(@full_file_name,'a')
name=details[0]
desc=details[1]
browser=details[2]
result=details[3]
reason=details[4]
if result.downcase == "pass"
$report.puts "<tr>
<td>#{name}</td>
<td> #{desc}</td>
<td> #{browser} </td>
<td bgcolor='green'>#{result}</td>
<td>#{reason}</td>
</tr>"
else
$report.puts "<tr>
<td>#{name}</td>
<td> #{desc}</td>
<td> #{browser} </td>
<td bgcolor='red'>#{result}</td>
<td>#{reason}</td>
</tr>"
end
$report.close
end
def close_table
$report=File.open(@full_file_name,'a')
$report.puts "</table></br>"
$report.close
end
def summary_report(overall,passed,failed)
$report_overall=overall
$report_pass=passed
$report_fail=failed
$report=File.open(@full_file_name,'a')
$report.puts "<p> <b>Total Test cases : #{$report_overall} </b></p>
<p> <b>Passed : #{$report_pass} </b></p>
<p> <b>Failed : #{$report_fail} </b></p>
</body>
</html>"
$report.close
end

create_report
insert_head_title("Raveendran -- Sample HTML Report")
insert_reportname_date("My Test Report",$result_date )
start_table
report_row("Check Google Home Page","Check The Title in Google Home Page","IE","PASS","Title Present")
report_row("Check Google Home Page","Check The Title in Google Home Page","FF","FAIL","Title Not Present")
close_table
summary_report(2,1,1)

 

Output HTML File:

zip

How to zip the Folder using Ruby

Step 1 – Install ruby-zip gem >gem install ruby-zip

Step 2 – Create the folder named as “Chrome” and puts 5 files within that folder

Step 3 – create code.rb file and put the below code with in the ruby file

def zip_it(path)
require 'rubygems'
require 'zip/zip'
require 'zip/zipfilesystem'

path.sub!(%r[/$],”)
archive = File.join(path,File.basename(path))+’.zip’
FileUtils.rm archive, :force=>true

Zip::ZipFile.open(archive, ‘w’) do |zipfile|
Dir[“#{path}/**/**”].reject{|f|f==archive}.each do |file|
zipfile.add(file.sub(path+’/’,”),file)
end
end
end
zip_it(“chrome”)

Step 4 – Run the Ruby File

Output :

Chrome/Chrome.zip file created

Blogroll · Ruby · xml

Ruby Gem xml-simple

Code:

1. Create test.xml file Ex. D:\xml\test.xml

2. Copy the XML content from http://msdn.microsoft.com/en-us/library/windows/desktop/ms762271(v=vs.85).aspx and paste in to test.xml file

3. Install xml-simple library CMD — >gem install xml-simple

4. Create new Ruby File code.rb Ex. D:\xml\code.rb

require 'rubygems'
require 'xmlsimple'
config = XmlSimple.xml_in('test.xml', { 'KeyAttr' => 'name' })
i=0
while i < 12
puts config["book"][i]["id"]
puts config["book"][i]["title"]
puts config["book"][i]["author"]
puts "-----------------------"
i+=1
end

Output :

bk101
XML Developer’s Guide
Gambardella, Matthew
———————–
bk102
Midnight Rain
Ralls, Kim
———————–
bk103
Maeve Ascendant
Corets, Eva
———————–
bk104
Oberon’s Legacy
Corets, Eva
———————–
bk105
The Sundered Grail
Corets, Eva
———————–
bk106
Lover Birds
Randall, Cynthia
———————–
bk107
Splish Splash
Thurman, Paula
———————–
bk108
Creepy Crawlies
Knorr, Stefan
———————–
bk109
Paradox Lost
Kress, Peter
———————–
bk110
Microsoft .NET: The Programming Bible
O’Brien, Tim
———————–
bk111
MSXML3: A Comprehensive Guide
O’Brien, Tim
———————–
bk112
Visual Studio 7: A Comprehensive Guide
Galos, Mike
———————–

Prawn · Proxy server -- Registry · ramaze · random names

Drag and Drop using Watir-webdriver

Requirement :

Drag and Drop an element to the particular location

Code:
require 'rubygems'
require ‘watir-webdriver’

browser=Watir::Browser.new :ie
browser.goto("http://devfiles.myopera.com/articles/735/example.html")

my_element=browser.li(:text,'Art Brut')
target=browser.ul(:id,'Rej').li(:text,'None')

my_element.fire_event("onmousedown")

driver=browser.driver
driver.action.click_and_hold(my_element.wd).perform

sleep 2
driver.action.move_to(target.wd).perform

sleep 2
target.fire_event("onmouseup")

Ruby · selenium · Selenium-webdriver · watir

Get the Version detail of opened Selenium/Watir Webdriver Browser

Code for Selenium WebDriver IE Browser :

require 'rubygems'

require 'selenium-webdriver'

br=Selenium::WebDriver.for :ie

ie=br.execute_script("return navigator.userAgent;")

# Ex. "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C)"

puts ie.split("MSIE ")[1].split(";")[0]

OUTPUT  –> 9.0

——————————————————————————————————————————————-

Code for Selenium WebDriver Firefox Browser :

require 'selenium-webdriver'

br=Selenium::WebDriver.for :ff

ff=br.execute_script("return navigator.userAgent;")

# Ex. "Mozilla/5.0 (Windows NT 6.1; rv:12.0) Gecko/20100101 Firefox/12.0"

puts ff.split("/")[-1]

OUTPUT  –> 12.0

——————————————————————————————————————————————-

Code for Watir WebDriver IE Browser :

require 'rubygems'

require 'watir-webdriver'

br=Watir::Browser.new :ie

ie=br.execute_script("return navigator.userAgent;")

puts ie.split("MSIE ")[1].split(";")[0]

OUTPUT  –> 9.0

——————————————————————————————————————————————-

Code for Watir WebDriver Firefox Browser :

require 'rubygems'

require 'watir-webdriver'

br=Watir::Browser.new :ff

ff=br.execute_script("return navigator.userAgent;")

puts  ff.split("/")[-1]

OUTPUT  –> 12.0

——————————————————————————————————————————————-

Automation · cheat sheet · maximize · resize window · Ruby · selenium · selenium-webdriver · Selenium-webdriver · watir-webdriver

Verify the Element Height/Width in different browser dimensions using Selenium/Watir Webdriver

Selenium WebDriver Code :

require 'rubygems'
require 'selenium-webdriver'

browser=Selenium::WebDriver.for :ie

browser.navigate.to(“google.com”)

width1=browser.find_element(:id,’hplogo’).style(“width”)
height1=browser.find_element(:id,’hplogo’).style(“height”)

browser.manage.window.resize_to(200,600)
width2=browser.find_element(:id,’hplogo’).style(“width”)
height2=browser.find_element(:id,’hplogo’).style(“height”)

if width1=width2
puts “Test Case Passed : Width is not changed”
else
puts “Test Case Failed : Width is changed”
end
if height1=height2
puts “Test Case Passed : Height is not changed”
else
puts “Test Case Failed : Height is changed”
end

browser.close

OUTPUT:

Test Case Passed : Width is not changed
Test Case Passed : Height is not changed

Watir WebDriver Code :

require 'rubygems'
require 'watir-webdriver'

browser=Watir::Browser.new :ie

browser.goto(“google.com”)

width1=browser.div(:id,’hplogo’).style(“width”)
height1=browser.div(:id,’hplogo’).style(“height”)

browser.window.resize_to(200, 200)

width2=browser.div(:id,’hplogo’).style(“width”)
height2=browser.div(:id,’hplogo’).style(“height”)

if width1=width2
puts “Test Case Passed : Width is not changed”
else
puts “Test Case Failed : Width is changed”
end
if height1=height2
puts “Test Case Passed : Height is not changed”
else
puts “Test Case Failed : Height is changed”
end

browser.close

 

OUTPUT: 

Test Case Passed : Width is not changed
Test Case Passed : Height is not changed

 

Ruby · watir-webdriver

Watir-webdriver installtion issue with ffi in Windows

watir-webdriver Installtion issue with ffi in Windows:


1. Install ruby

2. Install the gem watir-webdriver from command prompt

a. CMD>gem install watir-webdriver

C:\Users\admin>gem install watir-webdriver
Fetching: watir-webdriver-0.6.1.gem (100%)
Fetching: ffi-1.0.11.gem (100%)
ERROR: Error installing watir-webdriver:
The 'ffi' native gem requires installed build tools.

Please update your PATH to include build tools or download the DevKit
from ‘http://rubyinstaller.org/downloads&#8217; and follow the instructions
at ‘http://github.com/oneclick/rubyinstaller/wiki/Development-Kit&#8217;

C:\Users\admin>

3. To Reolsve the issue, we need to install the ffi previous version gem.

4. CMD>gem install ffi -v1.0.9

5. Once ffi installed successfully, then install watir-webdriver again

a.CMD>gem install watir-webdriver