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

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 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

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

selenium-webdriver · watir-webdriver

watir-webdriver –> Ruby gem

Installation:

1. Install Ruby

2. Open Command Prompt >gem update –system

3. >gem install watir-webdriver

Usage:

The same script will work for multiple browsers like IE,FF,Chrome.

 

Code:

 

FOR IE :

require 'watir-webdriver'

  browser = Watir::Browser.new :ie
  browser.goto "http://google.com"
  browser.text_field(:name => 'q').set("ruby watir watir-webdriver")
  browser.button(:name => 'btnG').click
  puts browser.url
  browser.close

FOR Firefox,
change the second line  --> browser = Watir::Browser.new :firefox

For Chrome,
change the second line  --> browser = Watir::Browser.new :chrome

Overview about Watir-webdriver :