Make your automation script more Readable! Meaningful! Maintainable!

Just writing automation script with predefined steps is not enough! It will be easy to write but difficult to understand and maintain in near future. Let’s take example of Facebook Sign-In.

Feature: Facebook Sign-In
         As a user I should able to sign-in into Facebook.
 
 Scenario: Sign-In with valid credential
        Given I navigate to "https://www.facebook.com/"
        And I enter "abc@gmail.com" into input field having id "email"
        And I enter "123456" into input field having id "pass"
        Then I click on element having id "loginbutton"
        And I wait for 15 sec
  • Easy to write – Just copy & paste predefined steps and put parameters.

But

  • It’s not looking meaning-full.
  • Need to guess/verify on which field interaction is going on.
  • The user data (login credentails, test link) and object data (i.e. elements id, class, xpath) may changes, then need to do changes in script, multiple times.

Now let’s garnish above steps with meaning-full sentences.

Feature: Facebook Sign-In
         As a user I should able to sign-in into Facebook.
 
 Scenario: Sign-In with valid credential
        Given I open Facebook login page
        And enter username as "abc@gmail.com"
        And enter password as "123456"
        Then I click on login button
        And I wait for 15 sec

Run script and get implementation suggestions as follows.

You can implement step definitions for undefined steps with these snippets:

Given(/^I open Facebook login page$/) do |arg1|
  pending # express the regexp above with the code you wish you had
end

And(/^enter username as "(.*?)"$/) do |arg1|
  pending # express the regexp above with the code you wish you had
end

And(/^enter password as "(.*?)"$/) do |arg1|
  pending # express the regexp above with the code you wish you had
end

When(/^I click on login button $/) do 
  pending # express the regexp above with the code you wish you had
end

Now copy above steps and implement it in custom_steps.rb under steps_definitions like following.

# custom_steps.rb

require 'selenium-cucumber'

Given(/^I open Facebook login page$/) do
 step %[I navigate to "https://www.facebook.com/"]
end

And(/^enter username as "(.*?)"$/) do |arg1|
  step %[I enter "#{arg1}" into input field having id "email"]
end

And(/^enter password as "(.*?)"$/) do |arg1|
  step %[I enter "#{arg1}" into input field having id "pass"]
end

When(/^I click on login button$/) do
  step %[I click on element with id "loginbutton"]
end
  • Still object data (i.e. elements id, class, xpath) are hard-code and might be used multiple times.
  • So let’s create object_repo(page_objects) which contains variables which will store elements’ locators.
# object_repo.rb

$test_link = 'https://www.facebook.com/'
$username_textfield = 'email'
$password_textfield = 'pass'
$login_button = 'loginbutton'

Using object_repo

# custom_steps.rb

require 'selenium-cucumber'
require 'object_repo'

Given(/^I open Facebook login page$/) do
 step %[I navigate to "#{$test_link}"]
end

And(/^enter username as "(.*?)"$/) do |arg1|
  step %[I enter "#{arg1}" into input field having id "#{$username_textfield}"]
end

And(/^enter password as "(.*?)"$/) do |arg1|
  step %[I enter "#{arg1}" into input field having id "{$password_textfield}"]
end

When(/^I click on login button$/) do
  step %[I click on element with id "#{$login_button}"]
end

So overall structure become like this: structure

This way you can make your automation script more readable! meaningful! maintainable! Thank you readers!

Advertisements

About Sameer Sawant

I am postgraduate in computer science from pune university. Working as QA Engineer in an IT company.
This entry was posted in Uncategorized. Bookmark the permalink.

10 Responses to Make your automation script more Readable! Meaningful! Maintainable!

  1. Raj says:

    Thanks Sameer. I have been using ‘gmail’ gem and also ‘mail’ gem which interacts with mail clients. I was curious to know whether you have been working on these similar kind of scenarios.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s