How do I run spreadsheet data-driven webriver code without it being in a massive for loop?

85 Views Asked by At

I'm trying to write a basic testing framework in python using a spreadsheet for data and webdriver.

I've managed to get so far with reading in my spreadsheet into a list of dictionaries and putting it into my own function called cell_value, which I'm quite pleased about. The second argument is an integer to call each row. The only way I can see to call a row, run some selenium script and then call the next row is to put it in a loop. The problem I have is all my selenium code has to be inside this loop. As do all my variables for storing the data e.g. web_address.

Is there any way of just having the loop present to call the rows in the sheet but to keep the selenium code and the data variables (e.g. web_address) outside of the loop and put them anywhere I want?

Below is my code:

from xlrd import open_workbook
from selenium import webdriver

#** Global Variables **#
book = open_workbook('test_sheet_YN.xls')
sheet = book.sheet_by_index(0)


def cell_value(name, run_row):
    first_row = []  # The row where we store the name of the column
    for col in range(sheet.ncols):
        first_row.append(sheet.cell_value(0, col))
    data = []
    for row in range(run_row):  # number is brackets is row number, starts at 1 (not zero) - it's start, stop, step.
        dict = {}
        for col in range(sheet.ncols):
            dict[first_row[col]] = sheet.cell_value(row, col)
        data.append(dict)
    return dict[name]  # 'name' is the sheet column-header that returns the value of the specified row, 'run_row'

for i in range(2,sheet.nrows+1):
    print cell_value("URL", i) + " #row# " + str(i)
    web_address = cell_value("URL", i)
    name_of_URL = cell_value("Name", i)
    if cell_value("Run", i) == 'Y' or 'y':
        print "jump to selenium class"
        # for now just do this...
         driver = webdriver.Chrome('/usr/local/chromedriver-Linux64')
         driver.get(web_address)
         driver.implicitly_wait(10)
         driver.quit()
    else:
        pass
0

There are 0 best solutions below