How to create a Watir report in a .txt file? - ruby

I have a little test script to run in Watir that searches "books" on google images and then takes a screenshot of the result.
require "watir-webdriver"
browser = Watir::Browser.new :ie
browser.goto "http://www.google.com/"
puts browser.url
browser.a(:text => "Images").click
puts browser.title
browser.text_field(:name => "q").set "book"
browser.button(:value => "Search by image").click
browser.screenshot.save 'screenshots\search-results.png'
browser.close
However I would also like to include a log in a .txt file of the information I am "putting" into the console.
How would I go about doing this?

To do this I used:
require "watir-webdriver"
require 'logger'
$log = Logger.new('logs\search-books.log')
$log.info("** TEST 1 - Search books on google images and screenshot results **")
browser = Watir::Browser.new :chrome
browser.goto "http://www.google.com/"
$log.info("** PAGE URL **")
$log.info browser.url
browser.a(:text => "Images").click
$log.info("** PAGE TITLE **")
$log.info browser.title
browser.text_field(:name => "q").set "book"
browser.button(:value => "Search by image").click
browser.screenshot.save 'screenshots\search-results.png'
browser.close
By using Logger it allows you to create a log file (.log) and insert into it as you go along the script.

Related

Capture the response from watir-webdriver

Is there a way to capture the entire response from a query submitted via a watir-webdriver object/request. For example if I took the sample code below, is there an element that will capture the entire page from the form submission so that I may parse it for errors?
#!/usr/bin/ruby
require 'watir-webdriver'
b = Watir::Browser.new
b.goto 'bit.ly/watir-webdriver-demo'
b.text_field(:id => 'entry_0').set 'your name'
b.select_list(:id => 'entry_1').select 'Ruby'
b.select_list(:id => 'entry_1').selected? 'Ruby'
b.button(:name => 'submit').click
b.text.include? 'Thank you'
The .html element will return the page's source.
puts b.html will return the source of the current page.

How to open one browser in automation using ruby

I just want to know how to open one browser when i am going to test a loop. or how to clear the last inputted values in the login page to change it in the new values.
Assuming you're using watir-webdriver, you can instantiate a browser like this:
require 'watir-webdriver'
b = Watir::Browser.new
b.goto 'bit.ly/watir-webdriver-demo'
If you want to clear text entry fields, you can use the clear method:
require 'watir-webdriver'
b = Watir::Browser.new
b.goto 'bit.ly/watir-webdriver-demo'
b.text_field(:id => 'entry_1000000').set 'your name'
b.text_field(:id => 'entry_1000000').clear
I pulled this info from http://watirwebdriver.com/, so you may want to check that out.

How to run multiple firefox browser parallely having different proxy in Watir

The code is given Below , where it is launching three firefox browser
, all browser has different proxy settings. Using watir how launch all three browser same time using tread in watir???
require 'selenium-webdriver'
require 'rubygems'
require 'watir'
require 'rautomation'
require './CLReport.class'
require 'win32ole'
# TO INITIATE FIRST FIREFOX BROWSER
# THE PROXY DATA CAN BE parameterized from Excel sheet
profile = Selenium::WebDriver::Firefox::Profile.new
profile.proxy = Selenium::WebDriver::Proxy.new :http => 'myproxy.com:8080', :ssl => 'myproxy.com:8080'
$b1 = Watir::Browser.new :firefox, :profile => profile
$b1.goto("https://google.com")
# TO INITIATE SECOND FIREFOX BROWSER
# THE PROXY DATA CAN BE parameterized from Excel sheet
profile = Selenium::WebDriver::Firefox::Profile.new
profile.proxy = Selenium::WebDriver::Proxy.new :http => 'myproxy.com:8081', :ssl => 'myproxy.com:8081'
$b2 = Watir::Browser.new :firefox, :profile => profile
$b2.goto("https://google.com")
# TO INITIATE THORD FIREFOX BROWSER
# THE PROXY DATA CAN BE parameterized from Excel sheet
profile = Selenium::WebDriver::Firefox::Profile.new
profile.proxy = Selenium::WebDriver::Proxy.new :http => 'myproxy.com:8082', :ssl => 'myproxy.com:8082'
$b3 = Watir::Browser.new :firefox, :profile => profile
$b3.goto("https://google.com")
Now my question is how to join $b1,$b2,$b3 in a single browser using thread so that
only $browser.link(:text, "form application")click should work for all three browser parallely insted of writing
$b1.link(:text, "form application").click
$b2.link(:text, "form application").click
$b3.link(:text, "form application").click
i.e single line of code work work in three firefox browser same time parallely.
It is not possible because $b1,$b2,$b3 are instances of different browser,You can not make them equal.What are you doing is right. Or You can do some thing like that.
array = [$b1,$b2,$b3]
array.each { |browser|
browser.link(:text, "form application").click
}

Nokogiri is not loading "browser" from Watir

I have this so far:
require 'watir-webdriver'
require 'date'
require 'nokogiri'
browser = Watir::Browser.start 'https://example/ViewReport.aspx'
browser.link(:text, 'Combined Employee Performance Report').click
today = Date.today
yesterday = today.prev_day.strftime('%m' '%d' '%Y')
t = browser.text_field :id => 'UC255_txtStart'
t.set yesterday
t = browser.text_field :id => 'UC255_txtEnd'
t.set yesterday
btn = browser.button :value, 'Run Report'
btn.exists?
btn.click
page = Nokogiri::HTML.parse('browser')
links = page.css("a")
puts links.length
When I try to parse browser, the variable that Watir is using for the site URI, it gives me a blank HTML page.
Problem
When you do
page = Nokogiri::HTML.parse('browser')
You are asking Nokogiri to parse the string 'browser'.
Solution
What you actually want to parse is the html in the browser.
To get the browser's html, you do:
browser.html
So to parse it, you would do:
page = Nokogiri::HTML.parse(browser.html)

unable to locate element, using {:id=>"", :tag_name=>"select"}

I want to select value from drop down box using Ruby with watir-webdriver. Here is the command
browser.select_list(:id, "ctl00_SampleContent_ComboBox1_ComboBox1_OptionList").select("Whiskey")
and i got an error
unable to locate element, using {:id=>"ctl00_SampleContent_ComboBox1_ComboBox1_OptionList", :tag_name=>"select"}
Any ideas what is wrong?
Here is full code:
# 1.Open http://www.asp.net/ajax/ajaxcontroltoolkit/Samples/Default.aspx
#browser = Watir::Browser.new
#browser = Watir::Browser.new :ie
profile = Selenium::WebDriver::Firefox::Profile.from_name 'WatirWebDriver'
#profile.add_extension 'autoauth-2.1-fx+fn.xpi'
browser = Watir::Browser.new :firefox, :profile => profile
browser.goto 'http://www.asp.net/ajax/ajaxcontroltoolkit/Samples/Default.aspx'
#2.Click ComboBox link on the left pane of the page
browser.a(:id, 'ctl00_SamplesLinks_ctl15_SamplesLink').click
#3.Verify that http://www.asp.net/ajax/ajaxcontroltoolkit/Samples/ComboBox/ComboBox.aspx URL opened
if browser.url.eql? "http://www.asp.net/ajax/ajaxcontroltoolkit/Samples/ComboBox/ComboBox.aspx"
puts "Error loading page \"http://www.asp.net/ajax/ajaxcontroltoolkit/Samples/ComboBox/ComboBox.aspx URL opened\""
return false
end
#4.Select “Whiskey” in the combo-box
#browser.select_list(:id, 'ctl00_SampleContent_ComboBox1_ComboBox1_OptionList').select_value('Whiskey')
puts "!!!"
browser.select_list(:id, "ctl00_SampleContent_ComboBox1_ComboBox1_OptionList").when_present.select("Whiskey")
This does the job:
require "watir-webdriver"
browser = Watir::Browser.new
browser.goto "http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/ComboBox/ComboBox.aspx"
browser.button(id: "ctl00_SampleContent_ComboBox1_ComboBox1_Button").click
browser.ul(:id, "ctl00_SampleContent_ComboBox1_ComboBox1_OptionList").li(text: "Whiskey").click

Resources