i am on Beginning stage in SELENIUM. Now, i am trying functional testing on selenium web-driver. As driving EXCEL data is very essential on functional testing, i am struggling a lot in this are.
How to use Excel data?
i am on Beginning stage in SELENIUM. Now, i am trying functional testing on selenium web-driver. As driving EXCEL data is very essential on functional testing, i am struggling a lot in this are.
How to use Excel data?
What you are looking for is Data Driven Test or for short is DDT. You can use Excel as external source of data for you tests.
I assume you are using Python's unittest framework to write test cases.
First you will need to install ddt library by running:
pip install ddt
To read values from spreadsheet you will need to install another library by running:
pip install xlrd
Here is how you can import data from your sheet and use in your testcases:
import xlrd, unittest
from ddt import ddt, data, unpack
from selenium import webdriver
def get_data(file_name):
# create an empty list to store rows
rows = []
# open the specified Excel spreadsheet as workbook
book = xlrd.open_workbook(file_name)
# get the first sheet
sheet = book.sheet_by_index(0)
# iterate through the sheet and get data from rows in list
for row_idx in range(1, sheet.nrows):
rows.append(list(sheet.row_values(row_idx, 0, sheet.ncols)))
return rows
@ddt
class [Your Class Name](unittest.TestCase):
def setUp(self):
[Insert your setUp code here]
# get the data from specified Excel spreadsheet
# by calling the get_data function
@data(*get_data("TestData.xlsx"))
@unpack
def test_[your_test_name](self, [your_argument_1], [your_argument_2]):
[your test method]
def tearDown(self):
# close the browser window
self.driver.quit()
if __name__ == '__main__':
unittest.main()
The @unpack decorator is used to unpack spreadsheet values into multiple arguments. The test_[your_test_name] method accepts your value arguments([your_argument_1]...), which will be mapped to the spreadsheet values by ddt.