You are using an older browser that might negatively affect how this site is displayed. Please update to a modern browser to have a better experience. Sorry for the inconvenience!

Automating Salesforce Testing by Using Cucumber


By: Radha

Cucumber is a software testing tool used to automate ‘Acceptance tests’. Cucumber supports Ruby & Java for writing test scripts, and it uses “Selenium Web Driver or Watir Web Driver” for simulating browser windows. Also, it follows BDD (Behavior Driven development)approach in testing.

Following is the link that consists of steps involved in Cucumber installation:

http://www.agileforall.com/2011/08/getting-started-with-ruby-cucumber-and-capybara-on-windows/

This article explains how Salesforce application is tested using Cucumber. Also, we are going to automate the following testing activities of Salesforce:

  1. Field Verification
  2. Field Validation
  3. Validating Workflow

Cucumber uses the following structure:

Ruby mine is an IDE used to execute automation scripts for Cucumber. Following is the folder structure used by Cucumber for automating Test Scripts.

 

A Cucumber program contains following files:

  1. Feature file:

It uses ‘Gherkin Syntax’ for writing the expected software behavior. The files are created with .feature extension. Following link provides the detailed information about the ‘Feature’ file.

https://github.com/cucumber/cucumber/wiki/Gherkin

  1. Step Definition file:

This file contains the logic written using Ruby script, and the files are created with .rb extension. Following link contains the syntax of the ‘Step Definition’ file.

https://github.com/cucumber/cucumber/wiki/Step-Definitions

  1. Environment File:

This file contains the gems that are needed for executing the Test script. The files are created with .rb extension. Following link contains the structure of the ‘Environment.

https://github.com/cucumber/aruba

 

An example scenario for automating Salesforce using Cucumber:

The following Test Script is used to automate Salesforce testing activities:

  1. Field Verification:

Check whether the custom fields are created as per the requirement, from an excel sheet, in Salesforce and correct results are returned.

Feature file:

@Custom_field_Verification

Scenario: Verify whether the Custom fields listed in the Excel sheet are present in Salesforce or not.

Given I am selecting the “Accounts” Tab

When I Click the side arrow

Then I could click the “View Fields” Link

And I am verifying the fields with Excel sheet

When the Fields Present in the object

Then Print the result on it

Step Definition file: 

Given(/^I am selecting the “(.*?)” Tab$/) do |object| click_link “#{object}”
end
When(/^I Click the side arrow$/) do
find(:xpath, “//*[@id=’devSlide’]/div[1]”).click
end

Then(/^I could click the “(.*?)” Link$/) do |arg1|
click_link “#{res}”
end

Then(/^I am verifying the fields with Excel sheet$/) do
ExHandler.instance.open_excel
@worksheet=ExHandler.instance.worksheet
username = (@worksheet.cells[$i, 1].value)
password = (@worksheet.cells[$j, 2].value)
end
When(/^the Fields Present in the object$/) do
nwindow= page.driver.browser.window_handles.last page.driver.browser.switch_to.window(nwindow)
url=page.current_url
str=(url.split(“/”))
key=(str[3])
key = key.split(“?”)
puts key[0]
if key[0].length == 1
$j = 2
while $j <= 500
if (page.has_selector?(:xpath, “//*[@id=’CustomFieldRelatedList_body’]/table/tbody/tr[#{$j}]/th”))
$fn = (find(:xpath, “//*[@id=’CustomFieldRelatedList_body’]/table/tbody/tr[#{$j}]/th”).text)
$dt = (find(:xpath, “//*[@id=’CustomFieldRelatedList_body’]/table/tbody/tr[#{$j}]/td[4]”).text)
username = (@worksheet.cells[$j, 1].value)
password = (@worksheet.cells[$j, 2].value)
$row_num = @worksheet.UsedRange.Rows.Count
puts “Row Limit : #{$row_num}”
$x = 2
while $x <= $row_num do
username = (@worksheet.cells[$x, 1].value)
password = (@worksheet.cells[$x, 2].value)
username = username.strip
password = password.strip
if username == $fn && password == $dt
@worksheet.Cells($x,3).Value = “Available”
$x = $x + 20
end
$x = $x + 1
end
else
$j = 500
end
$j=$j+1
end
else
$j = 0
$i = 2
while $i <= 500
if (page.has_selector?(:xpath, “//*[@id=’#{key[0]}_CustomFieldRelatedList_body’]/table/tbody/tr[#{$i}]/th”))
$dt = (find(:xpath, “//*[@id=’#{key[0]}_CustomFieldRelatedList_body’]/table/tbody/tr[#{$i}]/th”).text)
$fn = (find(:xpath, “//*[@id=’#{key[0]}_CustomFieldRelatedList_body’]/table/tbody/tr[#{$i}]/td[4]”).text)
$row_num = @worksheet.UsedRange.Rows.Count
puts “Row Limit : #{$row_num}”
$x = 2
while $x <= $row_num do
username = (@worksheet.cells[$x, 1].value)
password = (@worksheet.cells[$x, 2].value)
username = username.strip
password = password.strip
if username == $fn && password == $dt
@worksheet.Cells($x,3).Value = “Available”
$x = $x + 20
end
$x = $x + 1
end
else
$i = 500
end
$i=$i+1
end
end
end
Then(/^Print the result on it$/) do
$x = 2
while $x <= $row_num do
$result = (@worksheet.cells[$x, 3].value)
$result = $result.class
if $result == String
else
@worksheet.Cells($x,3).Value = “Unavailable”
end
$x = $x + 1
end
end

Custom Fields in Salesforce:

 

The above scripts compares whether the fields listed in the Excel sheet exist in Salesforce or not. As you see from the output below, the tool automatically identifies the fields missing in Salesforce.

Output:

 

  1. Field Validation:

Perform a field validation for number fields. Similarly, we can perform the validations for Email, Text, Phone and Currency fields in salesforce.

Feature file:

@Validating_Numeric_Field

Scenario Outline: validating a numeric field of an object

Given a field name to evaluate “Employees” field is Numeric field

When I enter “<value>”

And click the save button

Then you could get an Error Message

Examples:
|value |
| @# |
| AlphaNumeric1 |
| Hella |

Step Definition File: 

Given(/^a field name to evaluate “(.*?)” field is Numeric field$/) do |nfield|
puts “these are data taken from feature : #{nfield}”
value = (page.has_content?(“#{nfield}”))
if value == true
else
puts “No I can’t See the Field”
end
$data = nfield
end

When(/^I enter  “(.*?)” as$/) do |tab|
puts $data
fill_in “#{$data}”, :with => tab
page.driver.browser.save_screenshot(“F:/#{screenshot_numeric + “#{tab}”}.png”)
end

And (/^click the save button$/) do
first(:button,”Save”).click
end

Then(/^you could get a Error Message$/) do
puts page.has_content?(“Error: Invalid number”)
end

Number field validation in Salesforce:

 

  1. Validating Workflow for Task creation:

Verify whether a Task is created or not, using Workflow and by creating an Account.

Feature File:

@Task_update

Scenario: Task Update work flow

Given I Enter required field to the Record

And Click the Save button

When Detail page is displayed verify your record.

Then you should see the Task Details in Open Activities grid

 

Step Definition file: 

Given(/^I Enter required field to the Record$/) do
fill_in “Account Name”, :with => “test1”
fill_in “Phone”, :with => “0123456789”
end

And(/^Click the Save button$/) do
first(:button,”Save”).click
end

When(/^Detail page is displayed verfy your record\.$/) do
val = (find(:xpath, “.//*[@id=’acc2_ilecell’]”).text)
val = val.split
puts val[0]
end

Then(/^you should see the Task Details in Open Activites grid$/) do
url=page.current_url
str=(url.split(“/”))
$key=(str[3])
puts “Record ID = #{$key}”
puts (find(:xpath, “//*[@id=’#{$key}_RelatedActivityList_body’]/table/tbody/tr[2]/td[8]”).text)
end

 

Output:

 

Difficulties faced in automating salesforce:

  1. When a Task is created, a new Record id is generated dynamically for each record by Salesforce. Automation tools could not identify the record id for the tasks created, so the testing will be interrupted.

To handle this, we are going to get the Record-Id from the URL of the record, by using the following code:

url=page.current_url
str=(url.split(“/”))
$key=(str[3])
puts “Record ID = #{$key}”
puts (find(:xpath, “//*[@id=’#{$key}_RelatedActivityList_body’]/table/tbody/tr[2]/td[8]”).text)

We can make use of this Record ID where ever in the code.

  1. Automation tools could not execute a specific module in the test script, and so we needed to run the entire script every time.

 

To overcome this issue in Cucumber, we are using Tags. Refer the following link to understand more about Cucumber Tags:

https://github.com/cucumber/cucumber/wiki/Tags

 

Example: Below are the few scenarios from Feature file in Cucumber

 

@Selecting_App

Scenario: Selecting an App

When I select the “Sales” App

And I should see the following object of the app

@selecting_Object

Scenario: Verifying Fields

When Select the “Leads” object

And Select the Click the New Button

 

Consider a feature file that contains multiple scenarios. If we want to run a specific scenario, we can go with Tags in Cucumber. Here, ‘Selecting App’ is one of the scenarios in Feature File, and to execute this particular scenario, we need to specify it in the execution part as ‘cucumber –tags @Selecting_App’

 

Summary

 

Cucumber is an effective behavior-driven development and test automation tool that eliminates manual testing, and thereby improves productivity of organizations.