Tuesday, March 17, 2015

Selenium WebDriver with Python

In the following blog I will show you how to setup and write your first test case using Webdriver in Python Language.

Install Python:

If you are using Linux or Mac you need not do this as Python comes pre-installed on these OS. however if you are using windows (most of us do), Please follow the steps mentioned blow to install python on windows platform.
  • Download the Python from following location for your windows OS and install it.
    https://www.python.org/downloads/windows/
  • Once installed you need to set the following paths in your system PATH variable. (Python folder name may be depends on the version of the Python you have installed)
    • C:\Python27
    • C:\Python27\Scripts
  • Now, let's test if Python has installed or not. Open command prompt (CMD) and enter following command, if it shows you version number then python has successfully installed.
    python --version

Now let's Install WebDriver for Python

Installing WebDriver in python is different than installing in Java, actually Installing webdriver for Python is pretty simple.  To install WebDriver, Just open command prompt (CMD) and enter following command. 
  • pip install selenium

First WebDriver Test in Python.

test_google.py
from selenium import webdriver
import unittest

class TestGoogle(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.get("https://google.com")

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

    def test_search(self):
        search_edit_box = self.driver.find_element_by_name("q")
        search_edit_box.send_keys("webdriver with python")
        search_edit_box.submit()

if __name__ == '__main__':
    unittest.main()

Now let's see How to run Above Webdrive Test

  • First of all copy above file and paste it in any file and save as test_google.py
  • Open command prompt (CMD) and navigate to folder where you have stored this file. 
  • Now run the following command
    python test_google.py


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()