Ruby – Variables

November 12, 2009

Scenario 1: Local variable

class Ordin
def first
a=10
puts a
end
def second
puts a
end
end

obj=Ordin.new
obj.first
obj.second

output:

variable_explanation.rb:11:in `second’: undefined local variable or method `a’ for #<Ordin:0xb7d3c5e0> (NameError)
from variable_explanation.rb:17

10

Description:
a –> works only inside of first method

———————————————-

Scenario 2: Method variable

class Metho
def first
@a=10
puts “#{@a} from first method”
end
def second
puts “#{@a} from second method”
end
end

obj=Metho.new
obj.first
obj.second

output:

10 from first method
10 from second method

———————————————-

Scenario 3: Class Variable

class One
@@a=10
def first
puts “#{@@a=@@a+3} from first method”
end
def second
puts “#{@@a} from second method”
end
end

class Two < One
def third
puts @@a
end
end

obj=Two.new
obj.third

Ouput:

10

———————————————-

Scenario 4: Global Variable

class One
$a=10
def first
puts “#{$a} from first method”
end
def second
puts “#{$a} from second method”
end
end

class Two
def third
puts $a
end
end

obj=Two.new
obj.third

Ouput:

10

———————————————-

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


 

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

 

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

 

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 — Opens browser as a normal way how the user opens it. Selenium — Create object for browser and works with that object in mentioned/particular port.

Watir — Changing registry details(Proxy-http,socks) is very easy. Selenium — Changing registry details(Proxy-http,socks) is not easy work.

Selenium — If your net connection is very slow then Selenium MAY returns error because of waitForPageToLoad exceeds(We can handle it by its not easy for beginneers) and  also sleep commads most of the times fails.  Watir — Watir waits until the page was fully loaded. We can use sleep commands

 

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.(if you open more tahn 10 browsers tehn may be your machine hangs depends upon hardware)

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(or object) 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. Drawback: 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.

Learning Watir –> Without Ruby knowledge itsnot possible to write the watir script for one web application.

Learning Selenium –> Programming skill is not Major. But the QA should familiar with anyone of the language from Ruby/Java/perl/Puthon/c#/Groovy/PHP to implement the code for any web application.

 

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)

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.