Archive

Archive for the ‘selenium’ Category

Cucumber: Passing Test Data as Table from Scenario

January 20, 2016 Leave a comment

While we can pass test data as table from the feature file through Scenario Outline:

Scenario Outline: User updates user information
 Given I login to application
 And I go to user profile page
 And I enter "<gender>" into field of "gender_element";
 And I enter "<age>" into field of "age_element";
 And I enter "<location>" into field of "location_element";
 Then I verify user updates are saved
Examples:
 | gender | age | location |
 | Female | 20 | Singapore |

Here’s another option to pass your data in table format through your step definition.

Scenario: User updates user information
 Given I login to application
 And I go to user profile page
 When I update user information with options:
  | Gender | Female |
  | Age | 20 |
  | Location | Singapore |
 Then I verify user updates are saved

#Step Definitions File

#step definition to populate textfields
Then /^I enter "(.*)" into field of "(.*)"$/ do |value, field|
 textfield(I18n.translate!(field)).click
 textfield(I18n.translate!(field)).send_keys value
end

#step definition that accepts test data from the feature file as table
When /^I update user information(?: with options:)?$/ do |table|

 options = table.rows_hash
 step %Q[I enter "#{options['Gender']}" into field of "gender_element"]
 step %Q[I enter "#{options['Age']}" into field of "age_element"]
 step %Q[I enter "#{options['Location']}" into field of "location_element"]
 step %Q[I press button labelled "bt_address_suggestion_use_this"] 

#step definition that accepts test data from the feature file as table with default value
When /^I update user information(?: with options:)?$/ do |table|
 default_value = {
 'Age' = "18",
 'Location' = "United States"
 }
 options = default_value.merge(table.rows_hash)
 step %Q[I enter "#{options['Gender']}" into field of "gender_element"]
 step %Q[I enter "#{options['Age']}" into field of "age_element"]
 step %Q[I enter "#{options['Location']}" into field of "location_element"]
 step %Q[I press button labelled "bt_address_suggestion_use_this"]
Advertisements
Categories: cucumber, ruby, selenium

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.

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

Automated Testing with Cucumber + Capybara

October 29, 2012 10 comments

In this post we will introduce another gem called Capybara.

Capybara is an acceptance testing framework with a higher level API and support for multiple backends, supports Selenium and runs in different browsers.

Others may ask, “Why would I use capybara if selenium could also drive the browser the way I want it?”  Well, one advantage I appreciate is Capybara’s higher-level API compared to selenium.

Let’s take for example a simple scenario of typing strings to an input textbox:

Selenium-webdriver snippet


require 'selenium-webdriver'

element = driver.find_element :name => "q"
element.send_keys "Cucumber tests"

Capybara snippet

require 'capybara'

fill_in "q", "Cucumber tests"

You can obviously see from this example that Capybara enforces easier writing scripts ability. For a complete documentation on Capybara you can check this link from Github which I found very helpful.

After installation setup discussed in my previous post Introduction to Cucumber, you need to have the following folder structure and files:

I. Base Folder

Base Folder Structure

where:

features – folder to host all your feature files

step_definitions – folder to host all your step definition Ruby files

support – folder to host your configuration files (env.rb)

Gemfile – defines the top-level gems to be used in your project

II. Features

– describes the features that a user will be able to use in the program

Sample: simple_search.feature


Feature: As a user I should be able to perform simple google search

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

III. Step Definition

– describes the actions that user will do for each step.

Sample: search_step.rb

Given /^I am on the main google search$/ do
 visit ('/')
end

When /^(?:|I )fill in "([^"]*)" with "([^"]*)"$/ do |field, value|
 fill_in(field, :with => value)
end

Then /^I click "([^"]*)" button$/ do |button|
 click_button(button)
end

Then /^I click on the first result$/ do
 find(:xpath, "//html/body/div[3]/div[2]/div/div[5]/div[2]/div[2]/div/div[2]/div/ol/li/div/h3/a").click
end

Then /^I should see "([^"]*)"$/ do |text|
 page.should have_content(text)
end

IV. Support

– hosts all configuration files

Sample: env.rb

require 'capybara'
require 'capybara/cucumber'

Capybara.default_driver = :selenium
Capybara.app_host = "http://www.google.com"
Capybara.default_wait_time = 20

World(Capybara)

V. Gemfile

– a format for describing gem dependencies required to execute Ruby codes

Sample: Gemfile


source "http://rubygems.org"

group(:test) do
 gem 'cucumber'
 gem 'capybara'
 gem 'rspec'
end

VI. Run

Using terminal go to your root project folder and type: cucumber or bundle exec cucumber

After the run, you should be able to see the results like this:

1 scenario (1 passed)
5 steps (5 passed)
0m9.461s

This example runs smoothly in Windows 7. Let me know if it works for you as well.

Resolving “Couldn’t open app window; is the pop-up blocker enabled?” Selenium error in IE 10

June 20, 2012 Leave a comment

Need to setup my Linux machine(master) to remotely access a Windows 8 machine(slave) with Internet Exporer 10 browser that will run my Selenium scripts.

master – machine where Selenium scripts, browser configurations are saved; one that will be sending requests to run the scripts

slave – machine where Selenium scripts will be run using a different platform (OS and browser); one that will accept the request to run the scripts

To do this, I configure the browser file from the master machine:

# for Internet Explorer
our $sel = Test::WWW::Selenium->new( host => "Windows Machine IP", port => 4445, browser => "*iexplore", browser_url => "application URL" );

From the slave machine, I needed to install the following:

– Java SE Runtime  Environment 1.7

– download latest Selenium Server from SeleniumHQ

To prepare the slave machine to accept the request, launch selenium server from the terminal using the same port set in the master machine:

java -jar selenium-server-standalone-2.23.1 -port 4445

To send request from the master machine, you may use “spec”, “rake” command depending on how you organized your test files. However after executing the command to run the scripts from the master machine, an error was encountered in the slave machine – “Couldn’t open app window; is the pop-up blocker enabled?”

IE 10 – Selenium error

Solution:
From the master machine, update browser configuration from “*iexplore” to “iexploreproxy”
# for Internet Explorer
our $sel = Test::WWW::Selenium->new( host => "Windows Machine IP", port => 4445, browser => "*iexploreproxy", browser_url => "application URL" );

Perl-Selenium Helpful String Manipulations

November 28, 2011 Leave a comment

Below are the string functions I found helpful during scripting in my current project using Selenium in Perl language:

Split(/PATTERN/,EXPR)
– Splits the string EXPR into a list of strings based on given PATTERN and returns that list.

# Scenario: Clicking Email link opens new window. Verify new location/URL displayed is correct
# Email element: <a onclick=”window.open(‘http:test.url’,’EMAIL’,’width=730,height=450,status=yes,toolbar=no,menubar=no,location=no’);” style=”color: rgb(38, 6, 2); font-size: 11px; text-decoration: underline;” href=”javascript: void(0);”>Email</a>

my $href = $sel->get_attribute("xpath=//html/body/div[2]/div[2]/div[2]/table/tbody/tr/td[2]/div[3]/a/\@onclick");
# $href = "window.open('http:test.url','EMAIL','width=730,height=450,status=yes,toolbar=no,menubar=no,location=no');"
my @url = split(/'+/, $href);
# @url = ['window.open(' , 'http:test.url' , ',' , 'EMAIL' , ',' , 'width=730,height=450,status=yes,toolbar=no,menubar=no,location=no' , ');' ]

Substr(EXPR,OFFSET,LENGTH)
– Extracts a substring out of EXPR and returns it based on defined OFFSET and LENGTH

# Scenario: Get the numeric value from the Total distance string element

my $str = $sel->get_text("css=div.olPopupContent>div.mainbubblecontent>div.mainbubbletabcontent>div.activetabcontent>div:nth-child(5)");
# $str = "Total distance: 100.25 miles";
my $distance = substr($str,16,5);
# $distance = '100.25';

Match (m/PATTERN/)
– match a string with a regular expression pattern

my $str = "Total distance: 100.25 miles";
 if ($str =~  m/ \d*.\d* miles/) {
   print "Pass";}
 else{
   print "Fail";}

Cmp_ok( $got, $op, $expected, $test_name )
– Test::More function that allows you to compare two arguments using any binary perl operator.

my $str = $sel->get_text("css=#panel > #collection_maneuvers > thead > tr > th");
# $str = "Total distance\: 100.25 miles"
cmp_ok($str, "=~", m/Total distance\: \d*.\d* miles/, "Verify estimated mileage is displayed");

Like( $got, qr/expected/, $test_name )
– Another Test::More function that evaluates any expression against a regular expression

my $str = $sel->get_text("css=#panel > #collection_poi > thead > tr.poi > th");
# $str = "5 locations found in your area"
like($str, qr/\d* locations found in your area/, "Verify number of locations is displayed");

Also came across this Perl documentation link (http://perldoc.perl.org) which is direct and comprehensive.

Selenium RC to test unsecured connection HTTPS

November 9, 2011 Leave a comment

It’s inevitable for software testers to run test in an environment with self-signed SSL certificates. This became one of my dilemma when trying to run my Selenium scripts in an HTTPS environment and was always prompted with “This Connection is Untrusted” error.

I have an existing Firefox Profile solely for Selenium, if you don’t have one, you can check this post.  Was able to resolve this issue by doing the following:

  1. Launch Profile Manager by typing “firefox -ProfileManager -no-remote” in your terminal (Linux user)
  2. Select Selenium profile then Start Firefox
  3. Access your web application URL in HTTPS
  4. Accept the SSL Certification:
      • Click “I Understand the Risks”
      • Click “Add Exception”
      • Click “Get Certificate”
      • Make sure “Permanently store this exception” tickbox is checked
      • Click “Confirm Security Exception”
  5. After successfully directed to the web application page, close Firefox
  6. Go to Selenium Profile folder ( in my case /home/girlie/.mozilla/firefox/selenium )
  7. Delete all files except for cert_override.txt and cert8.db files.

From here on, I rerun my Selenium scripts and didn’t encountered the “This Connection is Untrusted” error anymore 😀