Archive

Posts Tagged ‘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.

Advertisements

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.

Introduction to Cucumber

October 29, 2012 1 comment

More than a testing tool, Cucumber is a collaboration tool.

It is designed to accommodate both the technical(developers, automation testers) and non-technical(stakeholders, product owners) members of the software development team.

Cucumber supports behavior-driven development(BDD). In BDD, users(business analysts, product owners) first write scenarios or acceptance tests that describes the behavior of the system from the customer’s perspective, for review and sign-off by the product owners before developers write their codes.

When you run your test, Cucumber reads through user-readable files called features, parse it to scenarios which contains set of steps that are then matched to a step definitions of Ruby code using a regular expression.

Feature files could be deceiving. It may look simple and plain in the outside. But complex in the inside, within step definition or the ruby files which controls the flow of actions and where all the magic happens.

In order for Cucumber to understand the feature files, it uses a basic syntax called Gherkin. Gherkin makes use of the following keywords for documentation and readability — Feature, Background, Scenario, Given, When, Then, And, But, *, Scenario Outline and Examples.

To dive more information about Cucumber, I would recommend you read The Cucumber Book  which have valuable information you would need in learning this new technology.

In preparation to  your Cucumber testing experience, will be needing to setup the following in your local machine.

1. Java installation – JRE will do, mine is Java(TM) SE Runtime Environment 1.6

2. Ruby installation – visit their Downloads page. I have Jruby 1.6.7.2 installed in my box.

Don’t forget to define Java and Ruby Path in your system’s environment variables as well.

3. RubyGems installation – use “gem install <name of gem>” command. Here are some of the basic, helpful gems:

– Cucumber

– Capybara

– Rspec

If you’re all setup, feel free to jump to the next post – Automated Testing with Cucumber + Capybara

Reference: The Cucumber Book by Matt Wynne and Aslak Hellesoy

Categories: cucumber Tags: , ,

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 😀

Selenium-Ruby Installations in Linux

March 30, 2011 Leave a comment

In relation to my previous post on Selenium and Ruby setup in Windows, here’s an installation guide for Linux peeps:

1. Install JRE

– In your terminal type: sudo apt-get install sun-java6-jre

– To verify type: java -version

Note: Java version should be 1.5 or higher versions

2. Install Ruby

– In your terminal, type: sudo apt-get install ruby

– To verify type: ruby –version

Note: ruby 1.8.7 works with rspec version <= 1.3.1

3. Install Rubygems

– From the terminal, type: sudo apt-get install rubygems OR you can follow this tutorial Installing RubyGems

– To verify type:  gem –version

4. Install other useful gems

  • Rake ( to create a task that runs set of tests )

– Type: sudo gem install rake

– To verify type:  rake –version

  • Selenium-client ( API to drive Selenium tests from Ruby )

– Type: sudo gem install selenium-client -v 1.2.18

– To verify type: gem list selenium-client

Note: selenium-client 1.2.18 works with rspec version 1.2.8

  • Rspec ( to define executable examples of the expected behaviour of your code )

– Type: sudo gem install rspec -v 1.2.8

– To verify type: spec –version

  • Faker ( to easily generate fake data: names, addresses, phone numbers, etc. )

– Type: sudo gem install faker

– To verify type: gem list faker

Categories: ruby, selenium, ubuntu Tags: , , ,