Automation · Blogroll · firefox · QA · selenium · Selenium-webdriver · watir-webdriver

Watir / Selenium Web Driver – Browser Downloads — Solution to download pdf files automatically in firefox 20+ version

 

Solution to download pdf files automatically in firefox 20+ version using Selenium webdriver or Watir webdriver

 

Description:

I have tried to save PDF files automatically using the below link — http://watirwebdriver.com/browser-downloads/  and I have followed the same as per the mentioned page and reference website. But still the firefox browser not downloaded PDF files automatically.

Reason:

In Firefox latest versions, They have added new function called as ‘Portable Document Format’ which causes the issue

Solution:

Please try the below code for latest firefox versions.

download_directory = "#{Dir.pwd}/downloads"

download_directory.gsub!("/", "\\") if Selenium::WebDriver::Platform.windows?
profile = Selenium::WebDriver::Firefox::Profile.new
profile['browser.download.folderList'] = 2 # custom location
profile['browser.download.dir'] = download_directory
profile['browser.helperApps.neverAsk.saveToDisk'] = "text/csv,application/pdf"

profile[‘pdfjs.disabled’] = true
profile[‘pdfjs.firstRun’] = false

b = Watir::Browser.new :firefox, :profile => profile
Note:
profile[‘pdfjs.disabled’] = true
profile[‘pdfjs.firstRun’] = false
These 2 lines are added. Thats it 🙂
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

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:

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

 

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

Watir-webdriver cheatsheet

Getting Started

Load the Watir Webdriver library

require 'watir-webdriver'

Open a browser (Ex: Internet Explorer)

driver = Watir::Browser.new :ie

Go to a specified URL

driver.goto 'http://www.orbitz.com/'

Close the browser

driver.close

Access an Element

Type something in the Text box or text area

driver.text_field(:id,'airOrigin').set("MAA")

To Clear the text from text field 

driver.text_field(:id,'airOrigin').clear

Button

To click the button
driver.button(:id,'BUTTON_ID'').click

Drop down list

To select the value from list
driver.select_list(:id,'airStartTime').select("1 am")

To get the selected value from select field
 driver.select_list(:id,'airStartTime').value

Check box

To clik the check box

driver.checkbox(:id,'airNonStopsPreferred').click
OR
driver.checkbox(:id,'airNonStopsPreferred').set
To know the clicked? or not ?
driver.checkbox(:id,'airNonStopsPreferred').set?

Radio button driver.radio(:id,'htlChoice').click

To verify Flights radio button selected or not

driver.radio(:id,'htlChoice').set?

#if it returns TRUE then radio button already selected.

To get the title of the webpage puts driver.title

Return true if the specified text appears on the TAG

 puts driver.li(:class,'welcomeText').text.include("Welcome to Orbitz")

To Click SPAN Elements driver.span(:text,'Find Flights').click
Ruby 1.9 · selenium · selenium-webdriver · Selenium-webdriver

To access the elements within IFrame using Selenium WebDriver (Ruby)

Situation :

User want to click the field which is placed within 2 IFrames using selenium webdriver.

Field — Friendship

Source code for “Friendship” — <nobr>Friendship</nobr>

Frame ID’s –” nav”  and “JobplaceFrame”

Code:

require ‘rubygems’

require ‘selenium-webdriver’

$browser = Selenium::WebDriver.for :ie

$browser.get "http://URL.com”

current_title=$browser.title
if current_title == “TITLE OF THE WEBPAGE”
puts “step 1 passed”
else
puts “step1 failed”
exit
end

$browser.switch_to.frame(‘nav’) #parent Frame
$browser.switch_to.frame(“JobplaceFrame”) #Child frame

options=$browser.find_elements(:tag_name=>”nobr”)

options.each do |nobr_field|
if nobr_field.text == 'Friendship'
nobr_field.click
break
end
end

cheat sheet · selenium · selenium-webdriver · Selenium-webdriver

Selenium WebDriver – Cheat Sheet

Getting Started

Load the Selenium Webdriver library

require 'selenium-webdriver'

Open a browser (Ex: Internet Explorer)

driver = Selenium::WebDriver.for :ie

Go to a specified URL

driver.navigate.to 'http://www.orbitz.com/'

driver.get 'http://www.orbitz.com/'

Close the browser

driver.close

Access an Element

Type somethign in the Text box or text area

driver.find_element(:id,'airOrigin').send_keys("MAA")

To Clear the text from text field
driver.find_element(:id,'airOrigin').send_keys [:control, 'a'], :space

Button

driver.find_element(:id,'BUTTON_ID'').click

Drop down list

select_box=driver.find_element(:id,'airStartTime')

options=select_box.find_elements(:tag_name=>"option")
options.each do |option_field|
if option_field.text == '12a-9a'
option_field.click
break
end
end

Check box

driver.find_element(:id,'airNonStopsPreferred').click

Radio button

driver.find_element(:id,'htlChoice').click

To verify Flights radio button selected or not

driver.find_element(:id,'airChoice').selected?

#if it returns TRUE then radio button already selected.

Return the title of the document

puts driver.title

Return true if the specified text appears on the TAG

puts driver.find_element(:class,'welcomeText').text.include?("Welcome to Orbitz")

To Click SPAN Elements

options=driver.find_elements(:tag_name=>"span")
options.each do |span_field|
if span_field.text == 'Find Flights'
span_field.click
break
end
end

selenium · selenium ide · selenium-webdriver · Selenium-webdriver

Selenium IDE to Selenium WebDriver Ruby code conversion

1. Install Latest Mozilla Firefox browser (Now 6.0.x)

2. Install Latest Selenium IDE(Now 1.2.0) from http://seleniumhq.org/download/

3. Restart Fire fox — Open Selenium IDE  (Tools –> Selenium IDE)

4. Options — Click Options

5. Change the settings like below



6. Record few actions in IDE and save the recorded script.

7.  Refer Screenshot to convert it into Selenium WebDriver ruby code.

Selenium-webdriver

Selenium WebDriver Basics – Ruby

Installation:

1. Install Ruby

2.  In Command Prompt >gem install selenium-webdriver

3. Copy and Paste the below code and save the file as sample_raveendran.rb

require ‘rubygems’
require “selenium-webdriver”

driver = Selenium::WebDriver.for :ie
driver.navigate.to “http://google.com&#8221;

if driver.title == “Google”
puts “Testcase 1 passed”
else
puts “Testcase 1 failed”
end

q = driver.find_element(:name, ‘q’)
q.send_keys “Raveendran Selenium”
q.submit

sleep 5

if driver.title == “RAVEENDRAN SELENIUM – Google Search”
puts “Testcase 2 passed”
else
puts “Testcase 2 failed”
end

q=driver.page_source

if q.include?(“raveendran.wordpress.com”)
puts “Testcase 3 passed”
else
puts “Testcase 3 failed”
end

driver.navigate.to “http://google.com&#8221;
a=driver.find_element(:link,’Advanced search’)

a.click

if driver.title == “Google Advanced Search”
puts “Testcase 4 passed”
else
puts “Testcase 4 failed”
end

a=driver.find_element(:name,’as_q’)
a.send_keys(“Raveendran Selenium”)

a=driver.find_element(:name,’num’)

options=a.find_elements(:tag_name=>”option”)
options.each do |g|
if g.text == “20 results”
g.click
break
end
end

a=driver.find_elements(:xpath=> ‘/html/body/table[2]/tbody/tr/td/table/tbody/tr/td/div

/form/div/table[4]/tbody/tr/td/input’)

a.each do |b|
b.click
end

sleep 4

if driver.title == “RAVEENDRAN SELENIUM – Google Search”
puts “Testcase 5 passed”
else
puts “Testcase 5 failed”
end

4. Run the ruby code

5. The Output looks like

Testcase 1 passed
Testcase 2 passed
Testcase 3 passed
Testcase 4 passed
Testcase 5 passed