follow me icons

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";