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.

10 comments:

  1. Very good article and well explained.

    ReplyDelete
  2. Great update and thanks for sharing the testing information and value of Mobile App testing in detail here. I would recommend Testvox for the quality Mobile App testing Kochi services at a limited cost. Appreciate the information shared.

    ReplyDelete
  3. Thank you for sharing your thoughts and knowledge on this topic. This is really helpful and informative.

    If you want to make custom website & application you can contact us on our Android Mobile App Development Company and Top Mobile App Development Company anytime.

    ReplyDelete
  4. Thanks for sharing knowledge but just wanted to know one more thing in for loop columns are hard coded how we can make it dynamic. please give some examples.

    ReplyDelete
  5. The way you are explaining is so smart and so coding part is very useful and understandable. Thanks for blogging.

    Farah from Way2Smile Solutions DMCC (Trusted Automation Testing Service provider in Dubai)

    ReplyDelete
  6. Extraordinary update and a debt of gratitude is in order for sharing the testing data and estimation of Mobile App testing in detail here. This is really helpful and informative.

    Best Regards - Vigneshwaran P ( Mobile App Development Company )

    ReplyDelete
  7. A data-driven testing framework in Selenium WebDriver with Python scripting empowers testers to efficiently execute tests by parameterizing test inputs and expected outcomes from external data sources such as Excel sheets or databases and you can visit CMOLDS one of the most advanced web development companies dubai offering great services in website development and app development with great expertise and skillset.

    ReplyDelete

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...