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()
        self.driver.maximize_window()
        if url is not None:
            self.driver.get("http://google.com")

    def teardown_func(self):
        self.driver.close()

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

                    self.teardown_func()
                    #TearDown
            return deco_func
        return wrapper

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

            #Setup
            #TODO add setup stpes

            #Your Test case
            func(self)

            #TearDown
            #TODO Add teardown steps
        return deco_func

How to use this decorator


class Demo(AppManager):
    
    @AppManager.gui_test(url="http://www.google.com")
    def test_setup(self):
        #self.driver.get("http://google.com")
        search_editbox =self.driver.find_element_by_name("q").submit()
        search_editbox.send_keys("gaurang")
        search_editbox.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:
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')
elem.send_keys('janedoe')

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

Better, Aint't it ?

Nose:
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 nose.tools import assert_true

__author__ = 'Gaurang_Shah1'


import unittest
import splinter


class GoogleTest(unittest.TestCase):

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

    def tearDown(self):
        self.browser.quit()

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


class GooglePage(object):

    def __init__(self, browser):
        self.browser = browser
        self.browser.visit("http://google.co.in")

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

    def verify_text_on_page(self, text):
        assert_true(self.browser.is_text_present('splinter.cobrateam.info'))