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

 

 

 

 

 

 

net/ftp · Uncategorized

net/ftp – Rubygem

Download and read the file using net/ftp

  

require ‘rubygems’
require ‘net/ftp’

ftp=Net::FTP.new(‘100.444.211.144’)

ftp.login
ftp.chdir(“/Main_flder/Sub_folder1/Sub_folder2/”)
fileList = ftp.nlst(‘*.txt’)  #instead of .txt you can use any file extension

filedata = ‘ ‘

ftp.retrlines(“RETR ” + fileList[0]) do |block|
filedata << block

end

puts filedata    # It will print the content of the entire TEXT FILE

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

Blogroll

rubyzip — Ruby gem to zip the folder and files

rubyzip — Ruby gem to zip the folder and files

Requirement:

Zip the file for some folders and files

 

Steps:

1. Create a folder chrome and put some files in it.

2. create a ruby file in same location and paste the below code

a. >gem install rubyzip

def zip_it(path)
require ‘rubygems’
require ‘zip/zip’
require ‘zip/zipfilesystem’

path.sub!(%r[/$],”)
archive = File.join(path,File.basename(path))+’.zip’
FileUtils.rm archive, :force=>true

Zip::ZipFile.open(archive, ‘w’) do |zipfile|
Dir[“#{path}/**/**”].reject{|f|f==archive}.each do |file|
zipfile.add(file.sub(path+’/’,”),file)
end
end
end

zip_it(“chrome”)

3. save and run the ruby file.

4. Zipped file will be created with in the chrome folder.

Blogroll

Running Watir webdriver scripts in Browser Stack Environment

Running Watir webdriver scripts in Browser Stack Environment

Why it is required?:

If we need to run the scripts in more than 5 machines with different OS and browser versions then we may need to invest more in infrasturure. To aviod this, we will have Test server in Browserstack.

 

1. Create trail account in http://www.browserstack.com/

2. Copy the Authentication URL from your acoutn page

3. Run the below code

require ‘rubygems’
require ‘watir-webdriver’

include Selenium

caps = WebDriver::Remote::Capabilities.htmlunit(:javascript_enabled => true)
caps.platform = :WINDOWS
caps[:name] = “Watir WebDriver”
caps[:browser] = “chrome”
caps.version = 20
caps[“browserstack.debug”] = “true”
caps[“browserstack.tunnel”] = true

browser = Watir::Browser.new(:remote,
:url => “PASTE BTASCK URL HERE”, # Sample http://raveendran:x6XqqrJzJ9gUZ7sr@hub.browserstack.com/wd/hub
:desired_capabilities => caps)

browser.goto “http://google.com/&#8221;

sleep 9 # You should use wait methods instead of sleep
puts browser.title
browser.quit

Pros:

Easy implementation with any exisitng framework

Cons:

The execution speed will be 2x~3x slow when comparing with your local machine execution.

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 🙂