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 🙂

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

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

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

Watir vs Selenium

 

NOTE:   Its not comparison of Watir-webdriver and Selenium-webdriver

We had a 2 different mechanism before webdriver(2009) and this comparison is not useful if you are looking with Webdriver Option 🙂

 

 

Watir — Opens browser as a normal way how the user opens it. Selenium — Create object for browser and works with that object in mentioned/particular port.

Watir — Changing registry details(Proxy-http,socks) is very easy. Selenium — Changing registry details(Proxy-http,socks) is not easy work.

Selenium — If your net connection is very slow then Selenium MAY returns error because of waitForPageToLoad exceeds(We can handle it by its not easy for beginneers) and  also sleep commads most of the times fails.  Watir — Watir waits until the page was fully loaded. We can use sleep commands

Watir is a Ruby library that wraps the COM interface to Internet Explorer. COM is a long-standing Windows-based technology for making libraries accessible to various languages. This allows access to the Document Object Model (DOM) so it doesn’t matter where on the page an object exists, what matters is how you identify it (id, name, title, etc).

Selenium uses a unique mechanism for driving browsers. Selenium’s automation engine is written in JavaScript and runs inside a browser. The engine, called a browser bot, is embedded in a page that accesses the application under test in a separate frame. Because of cross-site scripting, Selenium’s browser bot has to be served from the same site as the application under test – requiring installation on the server.

Selenium supports a command language, called Selenese. Tests can be embedded in an HTML table, which will be read in by the browser bot and then executed.

Both tools run tests directly in a browser, and both do it in a way that allows the browser to be minimized while the tests are running, which means you don’t have to dedicate a machine to running tests.(if you open more tahn 10 browsers tehn may be your machine hangs depends upon hardware)

Watir was designed to be easy to learn, it allows page elements to be identified by index, name, ID, value or adjacent text.  Now Watir supports IE, Firefox,chrome,Safari with the help of  Firewatir, SafariWatir,chrome Watir. Complaint: Watir is currently limited to IE browsers on Windows, Firewatir supports  firefox browsers. So we need to write separate code(or object) for each and every browsers. But Selenium does in Single attempt.

Selenium was designed for breadth of coverage- multiple browsers and platforms. It was expected to be used by the same developers who built the application. Drawback: Selenium requires a server-side installation.

You can also express Selenium tests in a programming language, taking advantage of language-specific drivers that communicate in Selenese to the browser bot.

Learning Watir –> Without Ruby knowledge its not possible to write the watir script for one web application.

Learning Selenium –> Programming skill is not Major. But the QA should familiar with anyone of the language from Ruby/Java/perl/Puthon/c#/Groovy/PHP to implement the code for any web application.

How to run Selenium RC with Ruby codes

1. Download seleniumRC from –> http://seleniumhq.org

2. Unzip the file

3. commans prompt>java -version  –> Version should grater than 1.6.0_5

4. command prompt go to –> C:\selenium-remote-control-1.0.1\selenium-server-1.0.1>java -jar  selenium-server.jar

Now Selenium runs on 4444 port.

5.  a)Download and install Ruby from http://ruby-lang.org/en/downloads/

b) cmd prompt>gem install selenium-client

or

i) Go to http://rubygems.org/gems/selenium-client

ii) Click Download and save the GEM file in C:\ruby\bin\

iii) Cmd Propmt –> C:\ruby\bin>gem install GEMNAME

 

6. Open new command prompt and goto ruby program path.In Ex, c:\test>ruby code.rb

7.  code.rb

require “selenium/client”
require “test/unit”

class Untitled < Test::Unit::TestCase
def setup
@verification_errors = []
if $selenium
@selenium = $selenium
else
@selenium = Selenium::Client::Driver.new(“localhost”, 4444, “*chrome”, “http://jazzez.wordpress.com/&#8221;, 10000);
@selenium.start
end
@selenium.set_context(“test_untitled”)
end

def teardown
@selenium.stop unless $selenium
assert_equal [], @verification_errors
end

def test_untitled
@selenium.open “/”
@selenium.click “link=jazzez-news.exe released (using Mechanize – ruby gem)”
@selenium.wait_for_page_to_load “30000”
@selenium.click “link=exact:http://bit.ly/j_news_demo&#8221;
end
end

8. Selenium RC will give some outputs in new firefox window.

Any more details need then just ping me at jazzezravi@gmail.com