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/”

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 🙂
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
———————–

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

Blogroll · rasta · Ruby · watir

Ruby Gem – Rasta with watir

Rasta  (Ruby Sporeadsheet Test Automation)

Installation:

>gem install rasta

dependencies :

>gem install rspec -v 1.2.9

Note: The latest version 1.3.0. have some issues with rasta. So please install the version 1.2.9

What is Rasta ?

Rasta is a keyword-driven test framework using spreadsheets to drive testing.
It’s loosely based on FIT, where data tables define parmeters and expected
results. The spreadsheet can then be parsed using your test fixtures.

Rasta  with Watir:

you’re more likely to use Rasta with another tool like Watir so let’s start
with an example doing some automation with Google’s web site.

Let’s say we wanted to create test cases for testing a search phrase to see if
it qualifies as a Googlewhack. Googlewhacks are two word queries on Google
that produce one and only one result. The best place would be to start
with your spreadsheet.

search_term is_googlewhack?()
cornball logocentrisms
squirreling dervishes
quantum quizzer
despotism fluidics
megalomaniacal dipsomaniac

So now we have a few data elements and we’re ready to create the test fixture.
Let’s call the class ‘Googlewhack’ so name the tab in your spreadsheet
should also be ‘Googlewhack’ – that’s how Rasta knows which class to call.
First we’ll put in the watir plumbing.

require 'watir'
require 'rasta/fixture/rasta_fixture'

class Googlewhack
  include Rasta::Fixture::RastaFixture
  
  def before_all
    @browser = Watir::IE.new
  end
  
  def before_each
    @browser.goto('www.google.com')
  end
end

When you include the Rasta fixture in a class you can use before_all(),
after_all(), before_each() and after_each() to do any setup or teardown you
need. Just for grins, let’s go ahead and run our script and see what we get.
I’m assuming here that you have a directory examples\fixtures where you
placed googlewhack.rb.

rasta -f examples\fixtures googlewhack.xls

The IE browser starts like we’d expect and you can see it go to the Google
home page with each row, but we got an error in the cells for the search terms. To
see the error, run your mouse over the cell and it should show something like:

method: search_term()
expected no Exception,
got #<NoMethodError: undefined method `search_term’ for #>

This is telling us that search_term is not recognized by our test fixture. We
should include it as a class attribute.

require 'watir'
require 'rasta/fixture/rasta_fixture'

class Googlewhack
  include Rasta::Fixture::RastaFixture
  attr_accessor :search_term
  
  def before_all
    @browser = Watir::IE.new
  end
  
  def before_each
    @browser.goto('www.google.com')
  end

end

Give it another run. There are now no more errors in the spreadsheet but on the
commandline we see:

Finished in 0.0 seconds
0 examples, 0 failures
Test results are available in C:/workspace/rasta-trunk/rasta_test_results

So no tests were run. That must be because we never created the is_googlewhack?()
method. We’ll add support for the search box and hitting the button first.
To know how to call the HTML elements you can either view the source or use the
handy IE Developer Toolbar.

require 'watir'
require 'rasta/fixture/rasta_fixture'

class Googlewhack
  include Rasta::Fixture::RastaFixture
  attr_accessor :search_term
  
  def before_all
    @browser = Watir::IE.new
  end
  
  def before_each
    @browser.goto('www.google.com')
  end
  
  def is_googlewhack?
    @browser.text_field(:name, 'q').value = @search_term
    @browser.button(:name, 'btnG').click
  end

end

Try running the script again.

Finished in 0.0 seconds
0 examples, 0 failures
Test results are available in C:/workspace/rasta-trunk/rasta_test_results

Still no tests run. One thing to remember is that if there is no data in the
spreadsheet (or if the cell is italicized) then nothing happens. The fixture
is correctly setting the attributes, in this case setting search_term, but
there’s no method being called. We need a return value for is_googlewhack?().
It’s not clear that the method returns anything so for now put ‘nil’ in one
of the cells.

search_term is_googlewhack?()
cornball logocentrisms nil
squirreling dervishes
quantum quizzer
despotism fluidics
megalomaniacal dipsomaniac

If you run it again, we’re closer. Now the function is getting run for the
first row but the test is still failing because the button click is
returning a decimal instead of nil. That’s OK because we still need to
check the results and give a good return.

require 'watir'
require 'rasta/fixture/rasta_fixture'

class Googlewhack
  include Rasta::Fixture::RastaFixture
  attr_accessor :search_term
  
  def before_all
    @browser = Watir::IE.new
  end
  
  def before_each
    @browser.goto('www.google.com')
  end
  
  def is_googlewhack?
    @browser.text_field(:name, 'q').value = @search_term
    @browser.button(:name, 'btnG').click
    resulttext = "Results 1 - 1 of 1 for #{@search_term}"
    @browser.contains_text(resulttext) ? true : false
  end

end
search_term is_googlewhack
cornball logocentrisms TRUE
squirreling dervishes TRUE
quantum quizzer TRUE
despotism fluidics TRUE
megalomaniacal dipsomaniac TRUE

Running the script now should look a lot better. It’s very likely that none of
these pass because googlewhacks don’t stay that way for long but you can visit the
site to see some current phrases that should qualify. In addition to the spreadsheet,
the commandline output should show you the details for any failures and how the
test run went.

rasta -f examples\fixtures examples\googlewhack.xls
…F.F…F

1)
‘Googlewhack[B3] is_googlewhack?() should == true’ FAILED
expected: true,
got: false (using ==)

2)
‘Googlewhack[B4] is_googlewhack?() should == true’ FAILED
expected: true,
got: false (using ==)

3)
‘Googlewhack[B6] is_googlewhack?() should == true’ FAILED
expected: true,
got: false (using ==)

Finished in 8.547 seconds
10 examples, 3 failures
Test results are available in C:/workspace/rasta_test_results

10 examples? That’s odd because we only set up 5 tests. What’s happening here
is that every test case actually creates two tests. One to run the method,
and the other to report any exceptions found while the command was run. This
is more apparent when you look at the RSpec html output in
rasta_test_results\results.html.

What’s probably a better test though is to instead have the method return the
number of results found so that when the test fails we can have a little more
information on why it failed. You could instead create a method like google_hits()
and parse the results to return the number of hits. Something like this should
work and you can change the spreadsheet from true/false to reflect the number
of hits you expect.

def google_hits 
  @browser.text_field(:name, 'q').value = @search_term
  @browser.button(:name, 'btnG').click
  google_results =
  /Results \d+ - \d+ (of|of about) ([\d,]+) for #{@search_term}/
  @browser.contains_text(google_results)[2].to_i
end


Reference -- http://rasta.rubyforge.org/
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