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

 

 

 

 

 

 

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.

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

rautomation

RAutomation – Rubygem

RAutomation is a small and easy to use library for helping out to automate windows and their controls for automated testing. RAutomation provides: * Easy to use and user-friendly API (inspired by Watir http://www.watir.com) * Cross-platform compatibility * Easy extensibility – with small scripting effort it’s possible to add support for not yet supported platforms or technologies

Installation:

>gem install rautomation
>gem install ffi

Sample Code:

require “rautomation”
window = RAutomation::Window.new(:title => /part of the title/i)
window.exists? # => true
window.title # => “blah blah part Of the title blah”
window.text # => “Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ultricies…”
window.text_field(:class => “Edit”, :index => 0).set “hello, world!”
button = window.button(:text => “&Save”)
button.exists? # => true
button.click
all_windows = RAutomation::Window.windows
all_windows.each {|window| puts window.hwnd}
window = RAutomation::Window.new(:title => /part of the title/i)
windows = window.windows
puts windows.size # => 2
windows.map {|window| window.title } # => [“part of the title 1”, “part of the title 2”]
window.windows(:title => /part of other title/i) # => all windows with matching specified title
window.buttons.each {|button| puts button.value}
window.buttons(:value => /some value/i).each {|button| puts button.value}
window2 = RAutomation::Window.new(:title => “Other Title”, :adapter => :autoit) # use AutoIt adapter
# use adapter’s (in this case AutoIt’s) internal methods not part of the public API directly
window2.WinClose(“[TITLE:Other Title]”)

cucumber · cuRF

Cucumber — DRY up your Steps

Way 1:

Feature File:

Scenario: Google
Given I am in Google Search page
When the user clicks on the next button
When clicks next
When selects Next
Then the title should be “Raveendran – Watir”

In your ruby file,

Given /^I am in Google Search page$/ do
puts “I am in google page”
end

When /^(?:the? )?(?:user? )?(?:clicks|selects)?(?: on? )?(?:the?)?(?: next?)?(?: button?)?$/i do
puts “The same script working for 3 step files 🙂 ”
end

Then /^the title should be (.*)$/ do |arg1|
#Code for verification
puts “It works”
end

Solution :

Only 1 step is enough Instead of 3 steps if it targets same script.

Way 2:

Feature File:

Scenario: Google
Given I have verified the result page
When nothing changed
Then the title should be “Raveendran – Watir”

In Your Ruby file,

Given /^I have verified the result page$/ do
Given “I am in Google Search page”
And “the user clicks on the next button”
And “clicks next”
And “selects Next”
And “the title should be “Raveendran – Watir””
end

When /^nothing changed$/ do
#Some Code
end

Then /^the title should be “([^\”]*)”$/ do |arg1|
#Some Code
end

File concepts

Ruby – Rename files in a single directory

Code:

def rando
letter=(“a”..”z”).to_a
return @name=
letter[rand(letter.length)]+”#{rand(9)}”+letter[rand(letter.length)]+
letter[rand(letter.length)]+letter[rand(letter.length)]+”#{rand(9)}”+letter[rand(letter.length)]+
letter[rand(letter.length)]+”#{rand(9)}”+letter[rand(letter.length)]+letter[rand(letter.length)]+
letter[rand(letter.length)]+letter[rand(letter.length)]+”#{rand(9)}”
end

dir = “../setup/videos”
$files = Dir.entries(dir)
puts “Before rename”
puts $files
$files.each do |f|
next if f == “.” or f == “..”
oldFile = dir + “\\” + f
newFile = dir + “\\#{rando;@name}” + “.”+f.split(“.”)[1]
File.rename(oldFile, newFile)
@file_path=File.expand_path(newFile)
@file_path=@file_path.gsub(“/”,”\\”)
end
puts “After Renamed”
$files = Dir.entries(dir)
puts $files

OUTPUT:

Before rename
.
..
four.flv
one.flv
three.flv
two.flv
After Renamed
.
..
f5vdc7zw3vplz8.flv
h5yyy5xj1jmbg8.flv
m6roa8sp5lqbq1.flv
q4rtq5ui0zdvx0.flv

random names

Random names in Ruby

Code:

def rando
letter=(“a”..”z”).to_a
return @name=
letter[rand(letter.length)]+”#{rand(9)}”+letter[rand(letter.length)]+
letter[rand(letter.length)]+letter[rand(letter.length)]+”#          {rand(9)}”+letter[rand(letter.length)]+
letter[rand(letter.length)]+”#    {rand(9)}”+letter[rand(letter.length)]+letter[rand(letter.length)]+
letter[rand(letter.length)]+letter[rand(letter.length)]+”#{rand(9)}”
end

rando
puts @name

Output:

o1epp7yn0clbk4