RSpec for test automation scripts

I am not going to discuss about what is RSpec. In short – Rspec is a behavior-driven development (BDD) framework for the Ruby programming language. For more details you can Google it.

Here I am mainly going to discuss about how to implement this framework’s block structure in our test automation script.

I am going to give examples according to my day to day job. You can find more scopes while you are working. Here I am going to mention ‘automation script’ term everywhere as I am focusing on making automation scripts. You can use this structures in other cases if you need

RSpec Blocks:

So far I have used the following blocks provided by rspec.

  • before
  • after
  • describe
  • context
  • it

This blocks have covered my work areas

1.”before” block:

before is a block under which you write some executable methods you want to run before your main automation script starts. It can be – connection to the database, inserting data into the database, creating the browser instance etc. ‘before’ block can be break down in to two state-

before (:all) and before (:each)

before(:all) will be used if we want something to execute once before our main script starts. Example- instance of a browser for all test cases.

before (:each) will be used if we want something to execute before each test case or behavior in our script. Example- separate instance of a browser for each test case

These will be clearer with an example which I am going to give at the end all examples are with ruby and selenium webdriver

2. “after” block:

after is a block under which you write some executable methods you want to run after your main automation script ends. It can be – inserting data into the database, quitting the browser instance etc. ‘after’ block can be break down in to two state-

after (:all) and after (:each)

after(:all) will be used if we want something to execute once after all our main scripts ends. Example- quitting instance of a browser for all test cases.

after (:each) will be used if we want something to execute after each test case or behavior in our script. Example- quitting each instance of a browser (if we launch different instance of browser for each case) after each test case execution.

3. “describe” block:

Describe block is such kind of block where we can give the description of our executable method. In case of our automation scripts we can describe our test case in this block. Under this block we will write our automation script

4. ”context” block:

           We are going to give some context if required, in this block. Again all this things are going to be clear after viewing an example.

5. ”it” block:

           It block is the main block where we are going to write our main codes of script for different actions or behavior we are going to perform  like login or logout etc.

** Some important things to mention:

Blocks are just to organize our scripts and generating a structured report so that we can understand the whole condition of our automation execution. Blocks are nothing more than that. It just give description to make our automation script more understandable. We will arrange and use this block according to our requirement.

Example:


before(:all) do

  @driver=Selenium::WebDriver.for :remote, :url => "http://localhost:4444/wd/hub", :desired_capabilities => :firefox

  @driver.manage.window.maximize
  @server = "server address";
  @driver.get {server};

end

After(:all) do

 @driver.quit

end

describe 'Admin Approval Tests' do
  describe 'Approve a pending user request for user john and Edit user role' do
    context 'user Admin' do
      it 'should Sign in as admin user' do
         driver.get {server};
         driver.manage.window.maximize
         driver.find_element(:id, 'username').clear
         driver.find_element(:id, 'username').send_keys("username")
         driver.find_element(:id, 'password').clear
         driver.find_element(:id, 'password').send_keys("password")
         driver.find_element(:xpath, '//button[@type= 'test']').click
       end

       it 'should Approve the request for pending user request for john' do
         *scripts goes here
       end

       it 'should Give the sender role to the approved user john’ do
        *scripts goes here
       end

       it 'should Sign out form Admin user account' do
        *scripts goes here
       end
    end
  end
end

*if we use (before(:each) and after (:each)) then the browser instance will be created before each case and will quit after each case execution

In this example the block structure is like this

Before
After
Describe
  Describe
    Context
       It
       It
       It
       It

This structure I made to make my script more understandable. There are two describe block where I specify and describe the test case I am going to execute.

In ‘context’ block my context is admin. The action going to be performed under this block is by admin user that’s why I give my context admin. It’s totally according to my way how I want generate the report how I am going to specify the contexts.

In the ‘it’ block I write my scripts for login, approving user request, assigning role and then logout. I use separated ‘it’ block for each action. It is much helpful to maintain the automation scripts while execution and for assertions.

The report we generate through this –

RSpec report sample

so far I have used these blocks to organize and generate report for my test automation scripts there may be more but I have used this much so far and I am really fan of this frame work now . Next I will discuss about using assertion in our test automation scripts.

Advertisements

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