Archive

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
where:

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


google:
 app_host: http://www.google.com

bing:
 app_host: http://www.bing.com

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(ERB.new(File.read(File.expand_path('../../../config/environments.yml', __FILE__))).result)
 @@env_config = environments[env.to_s]
 raise "No config found for environment: #{env}" unless @@env_config
 end

def env_config
 @@env_config
 end

end

World(CustomConfig)

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:

env.rb


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

World(Capybara)

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.

Advertisements

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}
end

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

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

Examples:
 | 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: , ,