Monday 18 November 2019

Data Driven Testing Framework in Selenium Webdriver with python scripting

What is Data-driven Testing Framework?

In a data-driven testing framework, a set of data is created in an excel sheet, and those set of data are imported from an excel sheet to testing tool for testing.

For example, I have taken the www.myntra.com as my demo site for which I will be performing some data-driven test for sign in.

To Sign in a manual user have to follow below steps:
1. Enter www.myntra.com
2. Click on Profiles
3. Click on LOG IN
4. Enter Email address and password
5. Click on LOG IN button





Same we will do using selenium data-driven test framework. For repetitive login we will use excel sheet with email address and passwords.


So first step will create a XLUtil.py file which will import data from excel to automation framework ie. it will put on the website

import openpyxl

def getRowCount(file,sheetName):
    workbook = openpyxl.load_workbook(file)
    sheet = workbook.get_sheet_by_name(sheetName)
    return(sheet.max_row)

def getColumnCount(file,sheetName):
    workbook = openpyxl.load_workbook(file)
    sheet = workbook.get_sheet_by_name(sheetName)
    return(sheet.max_column)

def readData(file,sheetName,rownum,colnum):
    workbook = openpyxl.load_workbook(file)
    sheet = workbook.get_sheet_by_name(sheetName)
    return sheet.cell(row=rownum,column=colnum).value

def writeData(file,sheetName,rownum,colnum,data):
    workbook = openpyxl.load_workbook(file)
    sheet = workbook.get_sheet_by_name(sheetName)
    sheet.cell(row=rownum,column=colnum).value = data
    workbook.save(file)

So in XLUtil.py,
1. getRowCount method will get the max row count
2. getColoumnCount method will get the max column count
3. readData method would read the data from the particular cell
4. writeData method would write the data to a particular cell

Below is the snippet for the login.py

import XLUtils

from selenium import webdriver
from time import sleep
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys

# For pop up alerts of webpage
option = Options()
option.add_argument("--disable-infobars")
option.add_argument("start-maximized")
option.add_argument("--disable-extensions")

# Pass the argument 1 to allow and 2 to block
option.add_experimental_option("prefs", {
    "profile.default_content_setting_values.notifications": 1})

driver = webdriver.Chrome(chrome_options=option, executable_path=
                          'chromedriver.exe')
driver.maximize_window()

driver.get('https://www.myntra.com')
assert 'Online Shopping for Women, Men, Kids Fashion & Lifestyle - Myntra' in 
            driver.title

search_box = driver.find_element_by_class_name("desktop-searchBar")

# Login to the website
profile = driver.find_element_by_class_name("desktop-userTitle").get_attribute
                                           ("data-reactid")
print profile
driver.find_element_by_class_name("desktop-userTitle").click()
sleep(1)
print driver.find_element_by_link_text("LOG IN").get_attribute("text")
login = driver.find_element_by_link_text("LOG IN").click()
sleep(1)
assert 'Login' in driver.title

path = "C:\Data\Roshni\selenium_Framework\Myntra_app_automate\details.xlsx"
rows = XLUtils.getRowCount(path,'Sheet1')

for r in range(2,rows+1):
    username = XLUtils.readData(path,'Sheet1',r,1)
    password = XLUtils.readData(path,'Sheet1',r,2)

    # On login page use email and password to login
    # Enter email text field
    print driver.find_element_by_class_name("login-user-input-email")
                                            .get_attribute("name")
    driver.find_element_by_class_name("login-user-input-email")
                                            .clear()
    driver.find_element_by_class_name("login-user-input-email")
                                            .send_keys(username)
    driver.find_element_by_class_name("login-user-input-email")
                                            .send_keys(Keys.TAB)
    sleep(2)

    # Enter Password text field    
    print driver.find_element_by_class_name("login-user-input-password")
                                                     .get_attribute("name")
    driver.find_element_by_class_name("login-user-input-password")
                                                     .clear()
    driver.find_element_by_class_name("login-user-input-password")
                                                     .send_keys(password)
    sleep(1)

    # Press Log In button    
    print driver.find_element_by_class_name("login-login-button-container")
                                             .get_attribute("text")
    driver.find_element_by_class_name("login-login-button-container")
                                             .click()
    sleep(2)
    if driver.title == "Online Shopping for Women, Men, Kids Fashion & 
             Lifestyle - Myntra":
        print("Test Passed")
        XLUtils.writeData(path,'Sheet1',r,3,"Test Passed")

        # For logout
        profile = driver.find_element_by_class_name("desktop-userTitle")
                                   .get_attribute("data-reactid")
        print profile
        driver.find_element_by_class_name("desktop-userTitle").click()
        sleep(1)
        driver.find_element_by_xpath(
            "/html/body/div[1]/div/div/header/div[2]
                  /div[2]/div/div[2]/div[2]/div[2]/div[3]/div/div").click()
        sleep(5)

        # Login to the website
        profile = driver.find_element_by_class_name("desktop-userTitle")
                         .get_attribute("data-reactid")
        print profile
        driver.find_element_by_class_name("desktop-userTitle").click()
        sleep(1)
         print driver.find_element_by_link_text("LOG IN").get_attribute("text")
        login = driver.find_element_by_link_text("LOG IN").click()
        sleep(1)
        assert 'Login' in driver.title

    else:
        print("Test Failed")
        XLUtils.writeData(path, 'Sheet1', r, 3,"Test Failed")

sleep(5)
driver.quit()

So in login.py,
1. We import the XLUtil.py as we are going to call the methods related to Excel
2. Launch the www.myntra.com.
3. Click on profile
4. Click on login
5. Enter email address and password
6. Click on login

So far email address and password, we are importing those values from excel and then writing on website, if user login, then the test is pass which is written in excel sheet with writeData(). If the login fails then Test failed is written in excel sheet as a test result of the test.

Test Automation Frameworks

What is the Test Automation Framework?

It is a set of guidelines used for designing and creating the test scripts. These guidelines include the coding standards, concepts, processes, practices, project hierarchies, modularity, reporting mechanism, test data injections etc. to pillar automation testing. A framework should be scalable and maintainable.

Advantages of Framework:

  1. Reusability of code
  2. Maximum Coverage
  3. Easy Reporting mechanism
  4. Minimal manual intervention
  5. Low-cost maintenance
Types of Test Automation Framework
  1. Linear Scripting Framework
  2. Modular Testing Framework
  3. Data-Driven Testing Framework
  4. Keyword Driven Testing Framework
  5. Hybrid Testing Framework
  6. Behaviour Driven Testing Framework


Let us start with Data-driven testing framework in detail with selenium + python as scripting language

Feature Posts

Python Appium - Step by step procedure for capturing screenshot

 Why To capture screenshot? It is as important as your logs. If there is any failure for any test scenario, we can provide screenshot for th...