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

Thursday, October 14, 2010

Cucumber, Steps Definitions and Page Models

There are 3 different things involve in writing the automation test. The first one is about writing a cucumber scenario which is where the tester/BA writes the feature or acceptances test in plain text.

For more information about cucumber please visit the official sites of Cucumber which has heaps of information about cucumber. To learn how to write cucumber scenarios effectively please visit the Cucumber's Wiki page.

The second one is writing the steps definitions. This is where we define the code for our cucumber scenarios.

However, alot of times our steps definitions becomes cluttered with all of the xpath or css selections codes. Furthermore, if the web page changes the design, then we have to change all of our steps definition code to work with the new designs. This is where we need a page model design where we defined a particular web page design in a page model file. Gizmo is a really neat rubygems to help us write the page models. So instead of specifying the css or xpath selectors in the steps definitions, we just need to call the css or xpath selectors from the page models.

To see the implementation of page models, please see the "Setting up Cucumber" page where I use cucumber, steps definitions and page models in the project.

Wednesday, October 13, 2010

New Capybara version 0.4 coming soon

Apparently Capybara version 0.4 could be out this weekend. I have been using capybara version 0.3.9 and so far found no issues for my projects. I will try the new version soon and hopefully my project can run with no issues. One improvement in version 0.4 that might be beneficial for my project is that the CSS selectors can now support multiple selectors such as "h1, h2", previously it only recognises one selector.

So if you have the following code:

< div id='test_1' class='field_1 field_2' >.inner_text

Then you can only select @document.css(".field_1").inner_text in version 0.3.9. However, in version 0.4 I assume I should be able to do something like @document.css(".field_1, .field_2").inner_text.

The other bug fix that might make the test more stable is the fix for some obscure selenium bugs by changing the default from localhost to 127.0.0.1.

To see the complete changes to version 0.4.0.rc, please visit Capybara site.

Tuesday, October 12, 2010

How to setup Cucumber YAML Configuration File

Application configurations should be placed in one location and we can do this by using an external YAML file called config.yaml. On my blog on Setting up a cucumber project, I have not covered on how to create a config.yml file. This file is where you normally put your BASE_URL address and other configuration files for you to use in your projects.

For examples, you might want to run your test against a test, staging or production environments and for that you need to tell your code which url you want to test. So, here is how you write a config.yml file and how to use it.

I assume that you have the same project structure mentioned in here

1. Create a new config.yml file under features\support\config.yml

google_australia:
base_url: http://www.google.com.au

google_singpaore:
base_url: http://www.google.com.sg

2. Create a new "features\support\lib\configuration.rb" files to load the config.yml

require 'yaml'

class Configuration
def self.[] key
@@config[key]
end

def self.load name
@@config = nil
io = File.open( File.dirname(__FILE__) + "/../config.yml" )
YAML::load_documents(io) { |doc| @@config = doc[name] }
raise "Could not locate a configuration named \"#{name}\"" unless @@config
end

def self.[]= key, value
@@config[key] = value
end

end

raise "Please set the TEST_ENV environment variable" unless ENV['TEST_ENV']
Configuration.load(ENV['TEST_ENV'])

3. Add the following lines in your "features\support\env.rb" file

require File.dirname(__FILE__) + '/../lib/configuration';
BASE_URL = Configuration["base_url"]

4. Now modify "features\step_definitions\search_steps.rb"

Change

Given /^I am on the main google search$/ do
visit "http://www.google.com"
end

to


Given /^I am on the main google search$/ do
visit "#{BASE_URL}"
end

5. Now before you run the project you just need to set the TEST_ENV

set test_env=google_australia

This will run your test againts www.google.com.au. If you set the test_env=google_singapore then
your test will run againts www.google.com.sg

Thursday, October 7, 2010

undefined method `camelize' for "default_search_form":String (NoMethodError)

I encountered that camelize error a few weeks ago. It was quite frustrating since I have not modified my code and the error was quite unfamiliar for me. After doing a bit more research and trial and error I found out the error is caused by activesupport 3.0.

undefined method `camelize' for "default_search_form":String (NoMethodError)


I used a ruby gem called Gizmo which is a simple page model testing framework. At that time, I was using Gizmo version 0.1.0 which was using activesupport 3.0. Apparently, activesupport 3.0 is not compatible with Gizmo or some other gems that I am using in my code.

To fix the issue I simply downgraded my activesupport gem to be 2.3.3. However, Gizmo gem has now been updated to 0.1.1 which uses the correct version of activesupport.

Wednesday, October 6, 2010

Testing Pop up windows using Selenium webdriver

There are times when we need to test a pop up windows within the current browser. For example of this is when you try to test a pop up box for the user to fill in the user details or a print pop up box.

The problem is that the webdriver will still focus on the current browser and not the pop up windows and therefore any interaction will only go to the browser and not to the new pop up browsers.

To solve this simply switch the browser to point to the pop up.

Here is the implementation for selenium webdriver in ruby

def switch_to_new_pop_up
response.driver.browser.switch_to.window
(response.driver.browser.window_handles.last)
end

def close_active_window
response.driver.browser.close
response.driver.browser.switch_to.window
(response.driver.browser.window_handles[0])
end

Tuesday, October 5, 2010

Apt-get error in Ubuntu - 'Something wicked happened resolving...'

The Apt-get command in Ubuntu is a powerful tool to install application into the Ubuntu system. However, you may get the following error when trying to use the apt-get install command.


>sudo apt-get install ruby
Err http://us.archive.ubuntu.com/ubuntu/ lucid/main ruby 4.2
Something wicked happened resolving 'us.archive.ubuntu.com:http'
(-5 - No address associated with hostname)
Failed to fetch
http://us.archive.ubuntu.com/ubuntu/pool/main/r/ruby1.8/ruby1.8_1.8.7.249-2_i386.deb


This error is caused because you have not setup the correct proxy setting in your apt.conf.

To fix this issue simply add the following line into your /etc/apt/apt.conf file as setting up the proxy in the export http_proxy env variable won't solve this issue.


> sudo /etc/apt/apt.conf
Then add the following line into
>Acquire::http::Proxy "http://username:password@proxy:8080";