Archive for November, 2012

Cucumber: Read from File

November 1, 2012 1 comment

As your automated testing mature, you don’t want to limit users hard coding their application under test or browser driver all in the ENVironment file(env.rb). You can customized your environment by creating a configuration file where you can define variables like applications URL, timeout, database configurations and the like.

From the basic folder structure discussed in Automated Testing with Cucumber + Capybara post, add two more files:

config > environments.yml
support > custom_config.rb

environments.yml – contains all the environment variables that you can use



custom_config.rb – ruby code where you configure your code to read from a specific file

require "erb"

module CustomConfig
 unless defined? @@env_config
 puts "loading environments.yml..."
 env = (ENV['ENVIRONMENT'] && ENV['ENVIRONMENT'].to_sym) || :google
 environments = YAML.load('../../../config/environments.yml', __FILE__))).result)
 @@env_config = environments[env.to_s]
 raise "No config found for environment: #{env}" unless @@env_config

def env_config



Your base folder should look like these by now:

Revised Base Folder

Then edit your env.rb by adding the following lines to your environment file.

require File.expand_path(‘../custom_config’, __FILE__)
include CustomConfig

Also update Capybara.app_host definition to look up to @@env_config. Your env.rb file should be edited to something like this:


require 'capybara'
require 'capybara/cucumber'
require File.expand_path('../custom_config', __FILE__)
include CustomConfig

Capybara.default_driver = :selenium
Capybara.app_host = env_config['app_host']
Capybara.default_wait_time = 20


Notice in line #07 of your env.rb you are basically pointing app host to whatever environment you set in your custom_config.rbenv variable, (see line #06) for this example :google

From here on you should still be able to run your simple_search.feature file without error, the only difference is your code reads now from a specific file – environments.yml, through custom_config.rb.

Improve Writing your Cukes

November 1, 2012 Leave a comment

Based from the simple_search.feature that we have in Automated Testing with Cucumber + Capybara, we don’t want to limit users by writing element locators like id, css or xpaths, hardcoding search strings and validations in our Cucumber scenarios:

Simple search scenario

Scenario: A simple google search scenario
Given I am on the main google search
When I fill in "q" with "Cucumber test"
And I click "gbqfb" button
And I click on the first result
Then I should see "Cucumber lets software development teams describe how software should behave in plain text."

We can improve this 5-liner scenario by creating new step definition that accepts variable and reusing the previous step definitions in the new step.

Better search scenario

Scenario: The better way to do google search
Given I am on the main google search
When I search for "Cucumber test"
Then I verify first search result have "Cucumber lets software development teams describe how software should behave in plain text."

In order to make this new scenario running, we need to define the new step definitions:

Given /^I search for "([^\"]*)"$/ do |query|
 step %{I fill in "q" with "#{query}"}
 step %{I click "gbqfb" button}

Then /^I verify first search result have "([^\"]*)"$/ do |text|
 step %{I click on the first result}
 step %{I should see "#{text}"}

But wait, there’s even a better approach to present this scenario where we will use one of the Gherkin keyword “Scenario Outline.” Outline allows us to parameterized our test data by passing it to Examples.

Best search scenario

Scenario Outline: The best way to do google search
 Given I am on the main google search
 When I search for "<String>"
 Then I verify first search result have "<Search criteria>"

 | String         | Search criteria   |
 | Cucumber tests | Cucumber          |
 | Selenium       | What is Selenium? |

Hope you had a great time, just like me. Happy Cuking!

Categories: cucumber, selenium Tags: , ,