appium · Ruby · Ruby 1.9 · selenium-webdriver · watir-webdriver

Appium – ruby

Step to follow:

1. Setup Appium (http://appium.io/) for ruby

2. Start the Appium server

3. Connect real time device r simulator using adb

4. In cmd prompt >adb devices     –> should list at-least 1 test server

5. Run the below code (Assuming that you are comfortable with selenium and watir- webdriver — if not reach out to jazzezravi@gmail.com for detailed explanation)

require ‘rubygems’
require ‘appium_lib’
require ‘watir-webdriver’
require ‘selenium-webdriver’

# The address and port of our Appium server
server_url = “http://localhost:4723/wd/hub/”

# Hash with information selenium webdriver needs
capabilities =
{
platformName: ‘Android’,
platformVersion: ‘4.4’,
deviceName: ‘Android Emulator’,
browserName: ‘Chrome’
}

#Setup webdrivers to talk to Appium and mobile chrome and use implicit_waits to poll with long timeout to see if pages are loaded
driver = Selenium::WebDriver.for(:remote, :desired_capabilities => capabilities, :url => server_url)#, :profile => profile)
browser = Watir::Browser.new driver
browser.driver.manage.timeouts.implicit_wait = 30
puts “new ANDROID Watir browser object instantiated”

#automated browser code using Watir browser object and methods
browser.goto “google.com”

puts browser.text
puts “————-“
browser.text_field(:name,’q’).set(“Raveendran”)
browser.button(:name,’btnG’).click
puts browser.text.include?(“Raveendran”)
puts browser.text_field(:id,’jazzez’).exist?
puts browser.text.include?(“ruby selenium”)
puts browser.select_list(:id,’watir’).exist?

date · Ruby · Ruby 1.9

Date @ Ruby — In between dates during particular interval

Code:

require ‘date’

def check_weeks(dd1,dd2)
i=0
while dd1 <= dd2
dd1=dd1+0
if dd1+@interval < dd2
dd3=dd1+@interval
else
dd3=dd2
end
puts “Week from #{dd1} to #{dd3}”
dd1=dd3+1

i+=1
end
end

x=”2014/07/01″
y=”2014/07/30″

d1= Date.new(x.split(“/”)[0].to_i, x.split(“/”)[1].to_i,x.split(“/”)[2].to_i)
d2= Date.new(y.split(“/”)[0].to_i, y.split(“/”)[1].to_i,y.split(“/”)[2].to_i)

#Chnage the interval
@interval = 2

check_weeks(d1,d2)

 

Output:

 

>ruby helper.rb
Week from 2014-07-01 to 2014-07-03
Week from 2014-07-04 to 2014-07-06
Week from 2014-07-07 to 2014-07-09
Week from 2014-07-10 to 2014-07-12
Week from 2014-07-13 to 2014-07-15
Week from 2014-07-16 to 2014-07-18
Week from 2014-07-19 to 2014-07-21
Week from 2014-07-22 to 2014-07-24
Week from 2014-07-25 to 2014-07-27
Week from 2014-07-28 to 2014-07-30
>Exit code: 0

 

Change the @interval = 2 to any interval and implement it in your code

 

 

 

 

 

 

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:

Jazzez · pdf-reader · Prawn · Ruby · Ruby 1.9 · ruby excercise · watir

Convert Webpage into PDF files using Ruby gems — Watir-Webdriver + Prawn

Installation : 

1. Install Ruby 1.8.7 or 1.9.2

2. Install WatirWebdriver — Refer  http://rubygems.org/gems/watir-webdriver

a. CMD>gem install watir-webdriver

3. Install Prawn Library

a. CMD>gem install prawn

Sample Code:

require 'rubygems'
require 'watir-webdriver'
require 'prawn'

ff=Watir::Browser.new :ff

ff.goto("https://raveendran.wordpress.com")

Prawn::Document.generate('c:\\test123\\hello.pdf') do |pdf|
pdf.text(ff.text)
end

ff.close

Output:

hello PDF  file contains all the TEXT from Webpage

 

Open Issues:

Alignment issue occurs depends upon web page Design. But You wont miss to collect all the Text from Webpage.

Blogroll · Google Charts · Ruby · Ruby 1.9 · watir · watir-webdriver

Chart Creation using Ruby Gems — Google Charts + Watir WebDriver

Installation : 

1. Install Ruby 1.8.7 or 1.9.2

2. Install WatirWebdriver — Refer  http://rubygems.org/gems/watir-webdriver

a. CMD>gem install watir-webdriver

3. Install Google Charts Library — Refer http://googlecharts.rubyforge.org/

Sample Code:

require 'rubygems'
require 'watir-webdriver'
require 'gchart'

ff=Watir::Browser.new :ff

first=Gchart.bar(:data => [300, 100, 30, 200])
ff.goto(first)
ff.driver.save_screenshot 'c:\\test123\\first.jpg'

second=Gchart.pie_3d(:title => 'Open Source Tools', :size =>
'400x200',:data => [45, 35, 20], :labels => ["Watir", "Selenium", "Others"] )
ff.goto(second)
ff.driver.save_screenshot 'c:\\test123\\second.jpg'
three=Gchart.line(:data => [300, 100, 30, 200, 100, 200, 300, 10],
:axis_with_labels => 'x,r',:axis_labels => [['Jan','July','Jan','July','Jan'], ['2005','2006','2007']])
ff.goto(three)
ff.driver.save_screenshot 'c:\\test123\\three.jpg'

ff.close

Output:

1. Usually Google Charts gives  http urls as a output. Using Watir WebDriver we can open the URL and navigate to the mentioned page and we can save that web page as a Image.

2.  Run the code –> The out folder contains the images

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

Blogroll · Ruby · Ruby 1.9

What’s new in Ruby 1.9

Comparison between Ruby1.8.6 and Ruby1.9.1


Number RUBY_VERSION => 1.8.6 RUBY_VERSION => 1.9.1
1 “ruby”[0] #=>114 “ruby”[0] #=> “r”
2 “ruby”.unpack(‘U*’)
#=> [114,117,98,121]
“ruby”.unpack(‘U*’)
#=> [1603,1604,1605,1575,1578]
3 item=1
2.upto(4) do |item|
p item
end
# Outputs:
# 2
# 3
# 4
item
#=> 4
item=1
2.upto(4) do |item|
p item
end
# Outputs:
# 2
# 3
# 4
item
#=> 1
4 d=2
-> {d = 1 }.()
d
#=> 1
(changed the outer variable)
d=2
-> (;d) {d=1}.()
d
#=> 2
(this didn’t, but you still get the shadowing warning)
5 conferences.select do |name, _|
name == :lsrc
end
#=> [[:lsrc, “Austin”]]
conferences.select do |name, _|
name == :lsrc
end
#=> {:lsrc => “Austin”}
6 conferences.select do | data|
p data
end
# [:lsrc, “Austin”]
# [:scotland_on_rails, “Edinburgh”]
# [:railsconf_europe, “Berlin”]
(warning : multiple valiues for a block parameter(2 for 1 )
conferences.select do | data|
p data
end
# :lsrc
# :scotland_on_rails
# :railsconf_europe
conferences.select do | name,city|
p [name,city]
end

# [:lsrc, “Austin”]
# [:scotland_on_rails, “Edinburgh”]
# [:railsconf_europe, “Berlin”]

7 >> {‘name’, “Akhil”}
=> {“name”=>”Akhil”}
>> {‘name’, “Akhil”}
=> {“name”=>”Akhil”}
1. >> {‘name’, “Akhil”}
=> syntax error, unexpected ‘,’, expecting tASSOC

>> {name: “Akhil”}
=> {:name=>”Akhil”}
>> {‘name’, “Akhil”}
=> syntax error, unexpected ‘,’, expecting tASSOC

>> {name: “Akhil”}
=> {:name=>”Akhil”}

8 >> hash = {:a=> ‘A’, :b=>’B’, :c=>’C’, :d=>’D’}
=> {:b=>”B”, :c=>”C”, :d=>”D”, :a=>”A”}
>> hash.to_a
=> [[:b, “B”], [:c, “C”], [:d, “D”], [:a, “A”]]
>> hash.keys
=> [:b, :c, :d, :a]
>> hash.values
=> [“B”, “C”, “D”, “A”]
>> hash = {:a=> ‘A’, :b=>’B’, :c=>’C’, :d=>’D’}
=> {:b=>”B”, :c=>”C”, :d=>”D”, :a=>”A”}
>> hash.to_a
=> [[:b, “B”], [:c, “C”], [:d, “D”], [:a, “A”]]
>> hash.keys
=> [:b, :c, :d, :a]
>> hash.values
=> [“B”, “C”, “D”, “A”]
>> hash = {:a=> ‘A’, :b=>’B’, :c=>’C’, :d=>’D’}
=> {:a=>”A”, :b=>”B”, :c=>”C”, :d=>”D”}
>> hash.to_a
=> [[:a, “A”], [:b, “B”], [:c, “C”], [:d, “D”]]
>> hash.keys
=> [:a, :b, :c, :d]
>> hash.values
=> [“A”, “B”, “C”, “D”]
>> hash = {:a=> ‘A’, :b=>’B’, :c=>’C’, :d=>’D’}
=> {:a=>”A”, :b=>”B”, :c=>”C”, :d=>”D”}
>> hash.to_a
=> [[:a, “A”], [:b, “B”], [:c, “C”], [:d, “D”]]
>> hash.keys
=> [:a, :b, :c, :d]
>> hash.values
=> [“A”, “B”, “C”, “D”]
9 >> hash = {:a=> 1, :b=>2, :c=>3, :d=>4}
=> {:b=>2, :c=>3, :d=>4, :a=>1}
>> hash.to_s
=> “b2c3d4a1”
>> hash = {:a=> 1, :b=>2, :c=>3, :d=>4}
=> {:b=>2, :c=>3, :d=>4, :a=>1}
>> hash.to_s
=> “b2c3d4a1”
>> hash = {:a=> 1, :b=>2, :c=>3, :d=>4}
=> {:a=>1, :b=>2, :c=>3, :d=>4}
>> hash.to_s
=> “{:a=>1, :b=>2, :c=>3, :d=>4}”
>> hash = {:a=> 1, :b=>2, :c=>3, :d=>4}
=> {:a=>1, :b=>2, :c=>3, :d=>4}
>> hash.to_s
=> “{:a=>1, :b=>2, :c=>3, :d=>4}”
10 >> hash = {:a=> 1, :b=>2, :c=>3, :d=>4}
=> {:b=>2, :c=>3, :d=>4, :a=>1}
>> hash.each{|x| p x}
[:b, 2]
[:c, 3]
[:d, 4]
[:a, 1]
=> {:b=>2, :c=>3, :d=>4, :a=>1}
>> hash.each_pair{|x| p x}
(irb):48: warning: multiple values for a block parameter (2 for 1)
from (irb):48
[:b, 2]
(irb):48: warning: multiple values for a block parameter (2 for 1)
from (irb):48
[:c, 3]
(irb):48: warning: multiple values for a block parameter (2 for 1)
from (irb):48
[:d, 4]
(irb):48: warning: multiple values for a block parameter (2 for 1)
from (irb):48
[:a, 1]
=> {:b=>2, :c=>3, :d=>4, :a=>1}
>> hash = {:a=> 1, :b=>2, :c=>3, :d=>4}
=> {:b=>2, :c=>3, :d=>4, :a=>1}
>> hash.each{|x| p x}
[:b, 2]
[:c, 3]
[:d, 4]
[:a, 1]
=> {:b=>2, :c=>3, :d=>4, :a=>1}
>> hash.each_pair{|x| p x}
(irb):48: warning: multiple values for a block parameter (2 for 1)
from (irb):48
[:b, 2]
(irb):48: warning: multiple values for a block parameter (2 for 1)
from (irb):48
[:c, 3]
(irb):48: warning: multiple values for a block parameter (2 for 1)
from (irb):48
[:d, 4]
(irb):48: warning: multiple values for a block parameter (2 for 1)
from (irb):48
[:a, 1]
=> {:b=>2, :c=>3, :d=>4, :a=>1}
# >> hash = {:a=> 1, :b=>2, :c=>3, :d=>4}
=> {:a=>1, :b=>2, :c=>3, :d=>4}
>> hash.each{|x| p x}
[:a, 1]
[:b, 2]
[:c, 3]
[:d, 4]
=> {:a=>1, :b=>2, :c=>3, :d=>4}
>> hash.each_pair{|x| p x}
[:a, 1]
[:b, 2]
[:c, 3]
[:d, 4]
=> {:a=>1, :b=>2, :c=>3, :d=>4}
>> hash = {:a=> 1, :b=>2, :c=>3, :d=>4}
=> {:a=>1, :b=>2, :c=>3, :d=>4}
>> hash.each{|x| p x}
[:a, 1]
[:b, 2]
[:c, 3]
[:d, 4]
=> {:a=>1, :b=>2, :c=>3, :d=>4}
>> hash.each_pair{|x| p x}
[:a, 1]
[:b, 2]
[:c, 3]
[:d, 4]
=> {:a=>1, :b=>2, :c=>3, :d=>4}
11 >> hash = {:a=> 1, :b=>2, :c=>3, :d=>4}
=> {:b=>2, :c=>3, :d=>4, :a=>1}
>> hash.select{|k,v| k == :c }
=> [[:c, 3]]
>> hash = {:a=> 1, :b=>2, :c=>3, :d=>4}
=> {:b=>2, :c=>3, :d=>4, :a=>1}
>> hash.select{|k,v| k == :c }
=> [[:c, 3]]
>> hash = {:a=> 1, :b=>2, :c=>3, :d=>4}
=> {:a=>1, :b=>2, :c=>3, :d=>4}
>> hash.select{|k,v| k == :c }
=> {:c=>3}
12 Not Available “his name is Raveendran”.match(/name is(?\S+)/ )[:name]
#=> “Raveendran”
13 Not Available File.read(“input.txt”).encoding
#=> #
14 Not Available File.read(“input.txt”, encoding: ‘ascii-8bit’).encoding
# => #
15 Not Available result = File.open(“input.txt, “r:euc-jp”) do |f|
f.read
end
result.encoding
# => #
result.valid_encoding?
# => true
16 Not Available %w(Jazzez Ravi Kumar).map.with_index do |name,offset|
“{name} is #{offset +1}”
end
#=> [“Jazzez is #1″,”Ravi is #2″,”Kumar is #3”]
17 Not Available [1,2,3,4].reduce(:+)
#=> 10
(Symbol or Symbol#to_proc)
[1,2,3,4].reduce(&:+)
#=> 10
18 Not Available array=[1,2,3,4,5]
array.take(3)
#=> [1,2,3]
array
#=> [1,2,3,4,5]
19 Not Available array=[1,2,3,4,5]
array.drop(3)
#=> [4,5]
array
#=> [1,2,3,4,5]
20 Not Available :foo[1]
#=> “o”
21 Not Available :this === “this”
#=> true