Created
October 22, 2010 21:11
-
-
Save 100ideas/641383 to your computer and use it in GitHub Desktop.
use watir-webdriver to script firefix to visit a page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# How does webdriver work? | |
# http://wiki.openqa.org/display/WTR/Selection+Boxes | |
# http://jarib.github.com/watir-webdriver/doc/# | |
# http://github.com/jarib/watir-webdriver/wiki/FAQ | |
require 'rubygems' | |
require 'watir-webdriver' | |
require 'watir-webdriver/extensions/wait' | |
require 'pony' | |
require 'open-uri' | |
@response = '' | |
filename = '' | |
browser = Watir::Browser.new :firefox | |
browser.goto "http://cofactorbio.com" | |
# use this if we ever write more elaborate tests | |
=begin | |
puts "\t mailing report" | |
Pony.mail(:to => '[email protected]', :via => :smtp, :via_options => { | |
:address => 'smtp.gmail.com', | |
:port => '587', | |
:user_name => '[email protected]', | |
:password => 'password', | |
:authentication => :plain, | |
:domain => "[email protected]" | |
}, | |
:from => '[email protected]', :subject => "cofactorbio.com DOWN", | |
:body => "OMG the server is down! I think." | |
) | |
=end | |
puts "\t closing the browser..." | |
browser.close | |
puts "\t done!" | |
## Example of more elaborate stuff webdriver can do | |
# a script to automatically generate and fetch a nutrition summary PDF every day | |
=begin | |
browser.text_field(:name => 'user[email]').set('[email protected]') | |
browser.text_field(:name => 'user[password]').set('password') | |
browser.button(:name => 'commit').click | |
browser.goto "http://dailyburn.com/nutrition" | |
browser.link(:text, 'PDF').click | |
browser.select_list(:name => 'report_date').wait_until_present | |
browser.select_list(:name => 'report_date').select('Yesterday') | |
browser.button(:name => 'commit').click | |
browser.div(:id, 'report_download').wait_until_present | |
report_pdf = browser.link(:href, /http:\/\/s3.*/) | |
# download the file | |
# open-uri RDoc: http://stdlib.rubyonrails.org/libdoc/open-uri/rdoc/index.html | |
open(report_pdf.href, "User-Agent" => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.9 (KHTML, like Gecko) Chrome/7.0.531.0 Safari/534.9', | |
"From" => "[email protected]", | |
"Referer" => "http://dailyburn.com/nutrition") { |f| | |
puts "Fetched document: #{f.base_uri}" | |
puts "\t Content Type: #{f.content_type}\n" | |
puts "\t Last Modified: #{f.last_modified}\n" | |
filename = f.base_uri.to_s.split('/')[4].split('?')[0] | |
# Save the response body | |
@response = f.read | |
} | |
File.open(filename, 'w') do |file| | |
file.puts @response | |
puts "\t got #{filename}..." | |
end | |
puts "\t logging out..." | |
browser.goto "http://dailyburn.com/users/logout" | |
puts "\t closing the browser..." | |
browser.close | |
=end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment