Friday, October 3, 2014

Alternative to Setup and TearDown - Python Decorators

I recently designed a Automation Framework using Selenium Webdriver and Python. Everything was working fine until few days back when we realized that we have some test cases which verifies back-end and doesn't require browser to be opened. I never knew this while I was designing the framework, and so I  wrote one common Setup and Teardown, now problem is it opens the browser and does so many other stuffs even for the test cases which doesn't require it.

So, I came up with solution, I wrote down two decorators, one for UI test cases and one for backend test cases.

Following is the code.

import traceback
import unittest
from selenium import webdriver

class AppManager(unittest.TestCase):

    def setup_func(self, url):
        self.driver = webdriver.Firefox()
        if url is not None:

    def teardown_func(self):

    def gui_test(url=None):
        def wrapper(func):
            def deco_func(self):
                    #Your Test case
                    except Exception as e:
                        print traceback.print_exc()

            return deco_func
        return wrapper

    def backend_test(func):
        def deco_func(self):

            #TODO add setup stpes

            #Your Test case

            #TODO Add teardown steps
        return deco_func

How to use this decorator

class Demo(AppManager):
    def test_setup(self):
        search_editbox =self.driver.find_element_by_name("q").submit()

Sunday, July 27, 2014

WebDriver with Python using Splinter and Nose

Till now I have been writing all my post in java, However after spending almost an year in python, Why not I write post in python. So here is my first post about how to use WebDriver in Python. If you are reading this please don't forget to put the comment.

I have used two frameworks for writing Webdriver tests in Python

Splinter is basically a wrapper around the Webdriver API, which ease you to write code as compared to webdriver.

For example if you have to fill a form with web driver, your code might look
elem = browser.find_element.by_name('username')

However, if you use splinter, it will look like this
browser.fill('username', 'janedoe')

Better, Aint't it ?

Nose is an unit testing framework extended from python unittest. As I told you it's extended from unittest which mean it provides all the features provided by unittest plus few more

Enough of theory, let's see the code.

from import assert_true

__author__ = 'Gaurang_Shah1'

import unittest
import splinter

class GoogleTest(unittest.TestCase):

    def setUp(self):
        self.browser = splinter.Browser()

    def tearDown(self):

    def test_google_search(self):
        google = GooglePage(self.browser)'splinter - python acceptance testing for web applications').verify_text_on_page(''')

class GooglePage(object):

    def __init__(self, browser):
        self.browser = browser

    def search(self, keyword):
        self.browser.fill('q', keyword)
        return self

    def verify_text_on_page(self, text):

Thursday, June 26, 2014

REST API Testing using REST-assured

This is my second post about doing Automation of REST API Testing, few days back I write the post to do REST API Testing using Frisby.js ( a Javascript tool), today i will post about how to do this in JAVA using REST-assured API.

Please down and put following JAR files in your build path. 

Test Case. 
For this demo purpose, I am going to use site which return simple JSON response. 
1. visit site 

1. verify that it returns 200 OK 
2. verify it return correct JSON 

Test Script. 
 * @author Gaurang_Shah

//import com.jayway.restassured.RestAssured;
import static com.jayway.restassured.RestAssured.baseURI;
import static com.jayway.restassured.RestAssured.given;
import static com.jayway.restassured.RestAssured.useRelaxedHTTPSValidation;
import static com.jayway.restassured.RestAssured.when;
import static org.hamcrest.Matchers.equalTo;

import org.junit.Test;

import com.jayway.restassured.authentication.FormAuthConfig;

public class TestATP {

 public void testDemo(){
      "country_name",equalTo("United States"),

Friday, May 30, 2014

REST API Testing using Frisby.js

Couple of days back I was searching ways to Automate REST API, I was searching for tool which is open source, free and has good support. The first thing which I came across is Frisby.js

Frisby.Js  is a framework for testing REST API built on node.js. (Yes, that means you will have use javascript)

  • It's fast and easy to use. 
  • It generated JUnit XML report.
  • Doesn't have much facilities
  • very limited support group. 
However't that's what I feel, you can explore this and can comment if feels other wise. Now let me show you how to install it. 


  • First install the node.js from following location
  • Now go the command line and install the frisby.js using npm (node package manager)
    npm install -g frisby
  • You will also require to install jasmine as it is being used by frisby.js, Goto the command line and install jasmine using following command.
    npm install -g jasmine-node
  • Now all is set, you can go ahead and create the test case. 

Write Test Cases

  • Now firsby.js is using jasmine, name of the test case file must end with spec.js.
  • You also need to create the folder named spec and then folder names api inside that. 
  • Now copy the following file in spec/api.
  • Please not that, the first line is the location of the code may change depends where frisby module is located on your local machine. 

In following test cases, we will get the details for site using and then verify the JSON reponse. 
var frisby = require('C:/Users/gaurang_shah/AppData/Roaming/npm/node_modules/frisby');

frisby.create('Get IP From Host Name')
  .expectHeaderContains('content-type', 'application/json')
  country_name:"United States",
  region_code: 'CA',
  region_name: 'Californ',
  city: 'Sunnyvale',
  zipcode: '94089',
  latitude: 37.4249,
  longitude: -122.0074,
  metro_code: '807',
  area_code: '408' 

Run the Test Cases

  • open the command prompt and make sure you are in the parent directory of spec/api folder. 
  • Enter the following command
    jasmine-node spec\api\getHostDetails_spec.js
  • If you have multiple spec file and if you want to run all, then you can run using following command
    jasmine-node spec/api

Results & Reports 

  • Above test case will fail, as we are expecting region_name to be Californ and actual is California.
  • To Generate the Junit report, you just need to run the test cases using following command
    -node spec/api/ --junitreport

Monday, January 20, 2014

WebDriver: Verify If JavaScript Alert is Present or Not

WebDriver has provided decent API to handle JavaScript Alert, however there isn't any API present which lets you know if JavaScript Alert is present on page or not. And sometime we are not sure if Alert is present or not, if it is present then we need to cancel it.

Implementation of isAlertPresent is very simple, Following code shows how to check if alert is present or not and if present cancel it.

 * If Javascript Alert is present on the page cancels it. 
public void handleAlert(){
  Alert alert = driver.switchTo().alert();

 * @return True if JavaScript Alert is present on the page otherwise false
public boolean isAlertPresent(){
  return true;
 }catch(NoAlertPresentException ex){
  return false;