follow me icons

Saturday, October 30, 2010

How to get Colour in JRuby for window

I have to use JRuby to run my cucumber automation from ruby recently since the developers are using ant to run the build. So I was able to setup the automation code to use JRuby. However, I had issue in running my test using JRuby since it does not give a nice cucumber colour onto my command prompt screen.

After looking around, I found out that I need to install WAC.exe, Windows ANSI Color, on my Windows machine.

All I need to do is to download wac.exe and put it in my PATH variables.

Before



D:\code\> ant cucumber -Dtest.env=production

[java] ←[32mGiven I am on the google homepage←[90m
0m
[java] ←[31mAnd I search for "←[31m←[1m222 selenium webdriver←[0m←[0m←[31m" ←[90m
main_steps.rb:79←[0m←[0m
.
.
.
.


After: WAC.exe is able to interpret the ansi color


D:\code\> ant cucumber -Dtest.env=production | wac

[java] Given I am on the google homepage
[java] And I search for "Selenium Webdriver"
[java] When I click on the go button
.
.
.
.

Tuesday, October 26, 2010

How to get JSON responses using HTTP GET and POST request in ruby

JSON (JavaScript Object Notation) is a java script object notation that is used by computer to parse and generate data. It is a format that is quite widely used in some API implementation to exchange data. Last week I had to create an automation test to verify the JSON response result from an API. I eventually use 'net/http' gem to send the json request to the server and use json.parse to parse the JSON response data.

You would need to install "net/http" gem and "json" gem to use it. I created a HTTPResponse class to initialize and send the json request to the server.


class HTTPResponse
def self.get url
uri = URI.parse(URI::escape(url))
http = Net::HTTP.new(uri.host,uri.port)
headers={'content-type'=>'applications/json'}
http.get(uri.request_uri, headers)
end

def self.post url, payload
uri = URI.parse(URI::escape(url))
headers={'content-type'=>'applications/json'}
request = Net::HTTP::Post.new(uri.request_uri, header)
request.body = payload
Net::HTTP.new(uri.host,uri.port).start{|http| http.request(request)}
end
end


Then in your step definition you can just call the Get and Post method

GET

payload = {
"address" => customer_address,
"cust_id" => customer_id"
}.to_json

url = 'http://www.example.com/web/json/newcust=#{payload}'
@response, @body = HTTPResponse.post(url)

p @response #will return httpok status
p JSON.parse(@body) #will return a hash table which you can parse the value easily



POST

url = 'http://www.example.com/web/json/newcust'


payload = {
"address" => customer_address,
"cust_id" => customer_id"
}.to_json


@response, @body = HTTPResponse.post(url, payload)

p @response #will return httpok status
p JSON.parse(@body) #will return a hash table which you can parse the value easily

Monday, October 18, 2010

Debug your Web Test with ruby-debug

Ruby-debug has been giving me alot of help in debugging my cucumber web test. I use ruby-debug to pause my web testing so that I can check the variables values and to see what causes the test to fail on the websites.

To install ruby-debug gems, please visit this page

I created a simple steps definitions to invoke ruby-debug:


When /^I go into debug mode$/ do
require 'ruby-debug'
debugger
1
end


Then, I can simply use the steps definition in any cucumber scenario.


Scenario: A simple google search scenario
Given I am on the main google search
And I search for "site:qastuffs.blogspot.com"
And I click on the search button
And I go into debug mode
And I click on the first result
Then I should land on the qastuffs blog


The above cucumber scenario will run the browser to google.com to do a search on site:qastuffs.blogspot.com. Then it will hit the debug mode and it will pause. You will see the following text "(rdb:1) on your console where you can print your test variables.

To see all of commands that you can run in ruby-debug, simply type in "h" and press enter


(rdb:1) h
ruby-debug help v0.10.3
Type 'help ' for help on a specific command

Available commands:
backtrace delete enable help next quit show up
break disable eval info p reload step var
catch display exit irb pp restart thread where
condition down finish list ps save trace
continue edit frame method putl set undisplay

Friday, October 15, 2010

Ruby Devkit for windows native extensions

I receive the above error when trying to install ruby-debug in Windows.
gem install ruby-debug


C:\>gem install ruby-debug
Building native extensions.  This could take a while...
ERROR:  Error installing ruby-debug:
ERROR: Failed to build gem native extension.

C:/Ruby187/bin/ruby.exe extconf.rb
creating Makefile

make
'make' is not recognized as an internal or external command,
operable program or batch file.


Gem files will remain installed in 
C:/Ruby187/lib/ruby/gems/1.8/gems/linecache-0.43 for inspection.
Results logged to C:/Ruby187/lib/ruby/gems/1.8/gems/linecache-0.43/ext/gem_make.out

To fix it:
1. Download Devkit in the ruby website. Download the one under the "Development Kit"
2. Extract the file
3. run "ruby dk.rb init"
4. run "ruby dk.rb install"

Result:
D:\DevKit>gem install ruby-debug
Temporarily enhancing PATH to include DevKit...
Building native extensions.  This could take a while...
Building native extensions.  This could take a while...
Successfully installed linecache-0.43
Successfully installed ruby-debug-base-0.10.3
Successfully installed ruby-debug-0.10.3
3 gems installed
Installing ri documentation for linecache-0.43...
Installing ri documentation for ruby-debug-base-0.10.3...
Installing ri documentation for ruby-debug-0.10.3...
Installing RDoc documentation for linecache-0.43...
Installing RDoc documentation for ruby-debug-base-0.10.3...
Installing RDoc documentation for ruby-debug-0.10.3...

For more information about dev-kit please visit the github page