Ruby – Scrap the content from HTML Source
November 3, 2009
CODE.rb
code=’<html>
<head>
<title>title</title>
<meta content=”title” name=”keywords”/>
<meta content=”text/html; charset=iso-8859-1″ http-equiv=”Content-
Type”/>’
puts content= code.scan(%r{content=”(.*?)”}im).flatten[0].to_s
puts name=code.scan(%r{name=”(.*?)”}im).flatten.to_s
Output:
title
keywords
Ruby – Get ASCII value
October 30, 2009
Example 1:
puts ?r #=> 114
puts ?a #=> 97
Example 2:
output=”raveendran”
puts output[0] #=>114 ‘value of r’
puts output[1] #=> 97 ‘value of a’
Example 3:
output=”raveendran”
output.each_byte do |a|
puts a
end
>ruby ascii_from_ruby.rb
114
97
118
101
101
110
100
114
97
110
>Exit code: 0
Watir – handling hidden process in windows
October 30, 2009
Code:
def running(a)
running=`tasklist`
if running.include?(a)
puts “#{a} is already running in windows machine”
else
puts “#{a} is not running”
end
end
runnning(‘java.exe’)
#=> java.exe is already running in windows machine
runnning(‘notepad.exe’)
#=> notepad.exe is not running
Where this code will useful:
We can able to chaeck the process is runnign or not — When Script need to close some opened Firefox windows or chrome windows
Ruby – Identifying opened applications in windows machine
October 28, 2009
Code
require ‘rubygems’
require ‘win32ole’
def window_name_opened(name)
w=WIN32OLE.new(‘WScript.Shell’)
output=w. AppActivate(name)
puts output
end
window_name_opened(‘notepad’)
#=> false
Step 2 –> Now open one note pad and run the ruby program again it should returns TRUE
Watir — Basic Authentication window Handling
October 28, 2009
Code:
require ‘rubygems’
require ‘firewatir’
include FireWatir
browser=Firefox.new
#(Use next line instead of –>browser.goto(“http://website.com”) and entered username , password)
browser.goto(“http://username:password@website.com”)
# (If the authentication window occurs any other pages then simply call that LINK URL with username,password like this.)
Watir vs Selenium
October 28, 2009
Watir is a Ruby library that wraps the COM interface to Internet Explorer. COM is a long-standing Windows-based technology for making libraries accessible to various languages. This allows access to the Document Object Model (DOM) so it doesn’t matter where on the page an object exists, what matters is how you identify it (id, name, title, etc).
Selenium uses a unique mechanism for driving browsers. Selenium’s automation engine is written in JavaScript and runs inside a browser. The engine, called a browser bot, is embedded in a page that accesses the application under test in a separate frame. Because of cross-site scripting, Selenium’s browser bot has to be served from the same site as the application under test – requiring installation on the server.
Selenium supports a command language, called Selenese. Tests can be embedded in an HTML table, which will be read in by the browser bot and then executed.
Both tools run tests directly in a browser, and both do it in a way that allows the browser to be minimized while the tests are running, which means you don’t have to dedicate a machine to running tests.
Watir was designed to be easy to learn, it allows page elements to be identified by index, name, ID, value or adjacent text. Now Watir supports IE, Firefox,chrome,Safari with the help of Firewatir, SafariWatir,chrome Watir. Complaint: Watir is currently limited to IE browsers on Windows, Firewatir supports firefox browsers. So we need to write separate code for each and every browsers. But Selenium does in Single attempt.
Selenium was designed for breadth of coverage- multiple browsers and platforms. It was expected to be used by the same developers who built the application. Complaint: Selenium requires a server-side installation.
You can also express Selenium tests in a programming language, taking advantage of language-specific drivers that communicate in Selenese to the browser bot.
Reference: http://www.newsqa.com/watir-vs-selenium/
How to run Selenium RC with Ruby codes
October 12, 2009
1. Download seleniumRC from –> http://bit.ly/j_sel_rc
2. Unzip the file
3. commans prompt>java -version –> Version should grater than 1.6.0_5
4. command prompt go to –> C:\selenium-remote-control-1.0.1\selenium-server-1.0.1>java -jar selenium-server.jar
Now Selenium runs on 4444 port.
5. cmd prompt>gem install selenium-client
6. Open new command prompt and goto ruby program path.In Ex, c:\test>ruby code.rb
7. code.rb
require “selenium”
require “test/unit”
class Untitled < Test::Unit::TestCase
def setup
@verification_errors = []
if $selenium
@selenium = $selenium
else
@selenium = Selenium::SeleniumDriver.new(“localhost”, 4444, “*chrome”, “http://change-this-to-the-site-you-are-testing/”, 10000);
@selenium.start
end
@selenium.set_context(“test_untitled”)
end
def teardown
@selenium.stop unless $selenium
assert_equal [], @verification_errors
end
def test_untitled
@selenium.open “/”
@selenium.click “link=jazzez-news.exe released (using Mechanize – ruby gem)”
@selenium.wait_for_page_to_load “30000″
@selenium.click “link=exact:http://bit.ly/j_news_demo”
end
end
8. Selenium RC will give some outputs in new firefox window.
Any more details need then just ping me at jazzezravi@gmail.com
jazzez-news.exe released (using Mechanize – ruby gem)
September 19, 2009
jazzez-news.exe released (using Mechanize – ruby gem)
To read news from CNN,NDTV without broswer support
Live Demo –> http://bit.ly/j_news_demo
1. Download the jazzez-news.exe from http://bit.ly/j_news
2. Just click and read it
Coming soon – More news websites under this jazzez-news.exe
It’s my 100th Post for this blog http://raveendran.wotdpress.com
Jruby
September 10, 2009
Jruby
Website – http://jruby.org/
Code:
ravi.rb
require “java”
stringHello= “Raveendran in Ruby”
stringDate = java.util.Date.new
puts “#{stringHello.to_s}”
puts “Now in Java –> Date := #{stringDate.to_s}”
Run the JRuby code:
Go to command promt>jruby ravi.rb
Output:
C:\raveendran\jruby_examples>jruby ravi.rb
Raveendran in Ruby
Now in Java –> Date := Thu Sep 10 17:03:30 GMT+05:30 2009
C:\raveendran\jruby_examples>
Any issues in installation of JRuby in windows mnachine then just post a comemnt here.
Joker – Ruby gem
September 10, 2009
Joker – Ruby gem
Good Alternate for Regular expressions
require ‘rubygems’
require ‘joker’
wild = Wildcard['Fairy?ake*']
puts wild =~ ‘Fairycake’ #=> true
puts wild =~ ‘Fairyfakes’ #=> true
puts wild =~ ‘Fairylake is a cool place’ #=> true
puts wild =~ ‘Dairycake’ #=> false
puts wild =~ ‘Fairysteakes’ #=> false
puts wild =~ ‘fairycake’ #=> false
puts wildi = Wildcard['Fairy?ake*\?', true]
puts wildi =~ ‘FairyCake?’ #=> true
puts wildi =~ ‘fairyfakes?’ #=> true
puts wildi =~ ‘FairyLake IS A COOL Place?’ #=> true
Wildcard.quote(‘*?\\’) #=> ‘\\*\\?\\\\’
For more details about this gem –> http://bit.ly/L9kcg