background-color · border · border-color · color · Ruby · watir-webdriver

Watir-Webdriver – Verify Elements Color , background-color and border

Description:

To Verify the Elements Color / border-color / border / background-color using Watir-webdriver

Code

require ‘rubygems’
require ‘watir-webdriver’

@browser= Watir::Browser.new :chrome

@browser.goto(“bit.ly/watir-webdriver-demo”)

def verify_element_color(element,property,content,message)
begin
rgb_color=element.style property
rgb_color=rgb_color.sub(“rgba(“,””)
rgb_color=rgb_color.sub(“)”,””)
hex_color=rgb_color.split(“,”)
color=”#%02X%02X%02X%02X” % hex_color

if color.downcase.include? content.downcase
puts “Passed – #{message}”
else
puts “Failed – #{message}”
end
rescue
puts “Failed – #{message}”
end
end

verify_element_color(@browser.text_field(:id,’entry_1000000′),”border-color”,”#BBBBBB”,”Verify that Name Text Field has Block Color Border”)
@browser.button(:name,’submit’).click
verify_element_color(@browser.text_field(:id,’entry_1000000′),”border-color”,”#C43B1D”,”Verify that Name Text Field has Block Color Border”)

Usage:

You can pass property as a “border-color”,”border”, “color”, “background-color”

nokogiri · open-uri · raveendran · Ruby · scrap

Nokogiri – Rubygem

Sample code to save the website in to html in local machine.

Benefit : We can scrap the content from the target website.

Code

require ‘open-uri’
require ‘nokogiri’

link=”http://google.com”

doc = Nokogiri::HTML(open(link))

file1=File.open(“test.html”,’w’)

file1.puts doc

file1.close

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

 

 

 

 

 

 

Automation · Jazzez · jquery · QA · Ruby · selenium · selenium-webdriver · watir-webdriver

Watir Webdriver and JQuery

Most of the time in internet explorer, JQuery automation is little bit tricky and we need to use some methods like below only for IE browsers.

Put the below methods in to your library folder

def trigger_jquery_change(element)
$browser.execute_script(“$(‘##{element}’).change()”)
end

def trigger_jquery_onblur(element)
$browser.execute_script(“$(‘##{element}’).blur()”)
end
Script section(Including page object)

@SOME_SELECT_FIELD=$browser.select_list(:id,’ID_OF_THE_TEXT_FIELD’)

@SOME_SELECT_FIELD.when_present.select(@ANY_OPTION)
if @browser_name == “ie”
trigger_jquery_change(@SOME_SELECT_FIELD)
end

if @browser_name == “ie”
trigger_jquery_onblur(@SOME_SELECT_FIELD)
end

 

I hope there is no need to explain this code. if required please feel free to end mail to jazzezravi@gmail.com

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

Configuration Management using YAML file for Watir/Selenium web-driver related frameworks

Requirement:

In watir/selenium webdriver related automation framework, how can we run the same script in multiple environments.

Code:

config.yml:

Beta:
url: "beta.google.com"

Gamma:
url: "gamma.google.com"

Prod:
url: "google.com"

Code.rb

require 'rubygems'
require 'yaml'
require 'watir-webdriver'
$env= ARGV[0]
config = YAML.load_file("config.yml")
$browser=Watir::Browser.new :ie
puts "Navigating to #{config[$env]["url"]}"
$browser.goto("#{config[$env]["url"]}")

Output:

in CMD PROMPT>ruby code.rb Beta

Navigating to beta.google.com

in CMD PROMPT>ruby code.rb Prod

Navigating to google.com

html to pdf · pdf · Ruby · watir-webdriver

Convert HTML webpage to PDF using WatirWebDriver + Pdfcrowd

Installation

Sign up and obtain your username and API key — https://pdfcrowd.com/user/sign_in/

$ gem install pdfcrowd
Or download pdfcrowd-2.3.0.gem and run

Get the API key from your account page

Code:

require 'rubygems'
require 'pdfcrowd'
require 'watir-webdriver'

# create an API client instance
username="YOUR USERNAME"
apikey="YOUR API KEY"
client = Pdfcrowd::Client.new(username, apikey)

# convert an HTML string and save the result to a file
browser=Watir::Browser.new :ff
browser.goto("https://raveendran.wordpress.com")
html=browser.html
File.open('html.pdf', 'wb') {|f| client.convertHtml(html, f)}

# retrieve the number of tokens in your account
ntokens = client.numTokens()

Output:

After ran the script, you will get the html.pdf file in the same directory.

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:

Blogroll · Ruby · xml

Ruby Gem xml-simple

Code:

1. Create test.xml file Ex. D:\xml\test.xml

2. Copy the XML content from http://msdn.microsoft.com/en-us/library/windows/desktop/ms762271(v=vs.85).aspx and paste in to test.xml file

3. Install xml-simple library CMD — >gem install xml-simple

4. Create new Ruby File code.rb Ex. D:\xml\code.rb

require 'rubygems'
require 'xmlsimple'
config = XmlSimple.xml_in('test.xml', { 'KeyAttr' => 'name' })
i=0
while i < 12
puts config["book"][i]["id"]
puts config["book"][i]["title"]
puts config["book"][i]["author"]
puts "-----------------------"
i+=1
end

Output :

bk101
XML Developer’s Guide
Gambardella, Matthew
———————–
bk102
Midnight Rain
Ralls, Kim
———————–
bk103
Maeve Ascendant
Corets, Eva
———————–
bk104
Oberon’s Legacy
Corets, Eva
———————–
bk105
The Sundered Grail
Corets, Eva
———————–
bk106
Lover Birds
Randall, Cynthia
———————–
bk107
Splish Splash
Thurman, Paula
———————–
bk108
Creepy Crawlies
Knorr, Stefan
———————–
bk109
Paradox Lost
Kress, Peter
———————–
bk110
Microsoft .NET: The Programming Bible
O’Brien, Tim
———————–
bk111
MSXML3: A Comprehensive Guide
O’Brien, Tim
———————–
bk112
Visual Studio 7: A Comprehensive Guide
Galos, Mike
———————–