Wednesday, November 3, 2010

Cucumber Automation


What is Cucumber actually?

  • Behavior driven development testing tool.
  • Bunch of behaviors in the form of scenarios.
  • Written mainly by non-technical people
  • Written in plain English.

What should you do actually?

  • Write a behavior in the form of scenarios in a feature files.
  • Write the code in order to satisfy the scenario.
  • Run the feature file.
  • Watch it fail
  • Write the automation scripts with the template provided by the cucumber.
  • The behavior of the application is satisfied by the test script.

Cucumber Installation on Windows Platform:

 1. Ruby Installation.

·        First, go to Ruby site and download the latest version of Ruby installer. Click on Rube installer exe file and then install the ruby.

·        Set up the classpath for Ruby by adding home directory path of the ruby into the Classpath variable.

·        Setup the Path system variable for Ruby by add the path string of the ruby bin dir into the path system variable.

·        Make sure ruby installed properly by open the command prompt and type the command ‘gem update --system’.
Note: More details about Ruby http://www.sapphiresteel.com/IMG/pdf/LittleBookOfRuby.pdf

2. Install Cucumber:

·        Make sure Ruby installed properly by doing the steps of the Ruby installation.
·        Open command prompt and execute the command “gem install cucumber win32console” and wait for some time for installation.
·        Once installation is done and make sure cucumber installed or not by the command “cucumber --version”.
3. Install Rspec gem:
·        RSpec Cucumber is an engine that lets you map English statements (or any language actually) to executable code in order to validate software specifications written as scenarios with the code written to fulfill them.
·        Install Rspec by the command “gem install rspec”

4. Install watir gem:
·        Watir is an open-source family of Ruby libraries for automating web browsers. It allows you to write tests that are easy to read and maintain. It is simple and flexible.
·        Install watir by the command “gem install watir”

Demo Project:

Demo in windows platform (Please refer “Cucumber Installation on Windows Platform” section for installations before starting the project).

Let’s consider the test case, search some word in Google search home page).

1. First, create a sample project (i.e. create any directory) and then create a sub directory “features’.

2. Create a feature file with extension ‘.feature’ inside the features directory.

3. Copy the below info into the feature file.

Feature: Google Search
In order to find out more about cucumber benefits
I need to able to search Google

Scenario: Google Search for cucumber benefits
Given that I am on the Google Homepage
When I search for cucumber benefits
Then I should see "Cucumber Benefits for Great Skin and Eyes"

Let’s consider the sample project dir structure as below

4. Now go to command prompt, run the command “cucumber features” as below
 

It will generate the template of the step definitions (blue text in screen shot)) for the above scenario

5. Now create a ‘step_definitions’ directory inside of the features directory and then create a ruby file and then copy the step definitions template.

6. Now, implement the logic for the step definitions by using Ruby, Watir Driver scripts.

Please find below the sample step-definitions file.


7. Finally, execute the features in command prompt as above.

Monday, November 1, 2010

Selenium with CSS locators.

XPath locator is one of the most quirky and one of the most precise locator. This disadvantage is clearly seen while running the tests under IE while Firefox works with xpath pretty well. This problem is related to internal browser features in particular to the libraries being used for elements location definition. But this is another story. The main thing is that tests which intensively use XPath work extremely slow under IE and this feature is the cause of huge variety of problems related to execution speed as well as quality of the tests itself especially during operations with dynamic content. For this reason CSS locators can be good alternative of XPath.
What can we do with CSS locators?

First of all,
CSS locators also give the ability to describe object hierarchy. For instance, the XPath locator like:
Xpath=//div//span/a

can be described via
CSS in the following way:
css=div * span > a

From the sample above we may see analogy for XPath and
CSS namely:
a) Character "/"  meaning next level of
DOM hierarchy corresponds to CSS operator ">".
b) Character "//" meaning any object hierarchy level under current object corresponds to
CSS operator "*".

Secondly, just like XPath, the
CSS locators can locate object by thier attribute values. For example the locator like:
Xpath=//div[@id="some_id"]

can be described via
CSS in the following way:
css=div[id=some_id]

Also there\'s abitity to check for partial attribute value matching. But
CSS locators are a little bit limited in this area. There\'s ability to verify that attribute value consists of several space-separated words but one word equals to some value. It means that the XPath locator like:
xpath=//div[contains(@title,"title")]

can be expressed via
CSS like:
css=div[@title~=title]

Both the locators can detect the element like 

<div title="some multiword title" />  while the element like  <div title="my_title" />
  
can\'t be detected by CSS locator from above.

Also
CSS has simplified form for some attributes. For instance, the element having the id attribute like  <div id=some_id /> can be located by the following CSS:
css=div#some_id

In addition to that
CSS locators have specific way to define the object by the class attribute. The XPath locator like:
xpath=//div[@class="myclass"]

has the following
CSS analog:
css=div.myclass

Such features make
CSS suitable in some cases.

 More details about CSS can be found by the forllowing URL: http://www.w3.org/TR/CSS2/selector.html

So, in some cases CSS locators are the analogs of XPath locators with one serious advantage: CSS works fast no matter what the browser we use.

Of course,
CSS locators have their own limitations like: problems to locate objects by index, problem to locate objects higher in the object hierarchy or so.