01. Intro
It is a good idea to separate the test data from the code and put it in an external source for easy maintenance and avoid changes to the test code each time you want to update the values.
02. Reading values from CSV
Firstly, we make a CSV file for testing.
Create get_data() method: This accepts the path and name of the CSV file.
This method uses the CSV library to read the values from the file and returns a list of these values.
import csv
def get_data(file_name):
# create an empty list to store rows
rows = []
# open the CSV file
data_file = open(file_name, "rt")
# create a CSV Reader from CSV file
reader = csv.reader(data_file)
# skip the header
next(reader, None)
# add rows from reader to list
for row in reader:
rows.append(row)
return rows
03. Test Code
# get the data from csv file
@data(*get_data("D://selenium//DataDrivenTest//testdata.txt"))
@unpack
def test_search(self, search_value, expected_count):
# get the search textbox
search_field = self.driver.find_element(By.NAME, 'q')
search_field.clear()
# enter search keyword and submit
# use search_value argument to pass data
search_field.send_keys(search_value)
search_field.submit()
# get all the anchor elements which have product names
products = self.driver.find_elements(By.XPATH, "//h2[@class='product-name']/a")
expected_count = int(expected_count)
if expected_count > 0:
self.assertEqual(expected_count, len(products))
else:
msg = self.driver.find_element(By.CLASS_NAME, 'note-msg')
self.assertEqual("Your search returns no results.", msg.text)
04. Reading values from Excel
we make an Excel file for testing.
Reading value from the Excel spreadsheet will need another library called xlrd
pip install xlrd==1.2.0
def get_data(file_name):
# create an empty list to store rows
rows = []
# open the excel file
book = xlrd.open_workbook(file_name)
# get the first sheet
sheet = book.sheet_by_index(0)
# iterate through the sheet and get data
for row_idx in range(1, sheet.nrows):
rows.append(list(sheet.row_values(row_idx, 0, sheet.ncols)))
return rows
05. Test Code
# get the data from csv file
@data(*get_data("D://selenium//DataDrivenTest//testdata.xlsx"))
@unpack
def test_search(self, search_value, expected_count):
# get the search textbox
search_field = self.driver.find_element(By.NAME, 'q')
search_field.clear()
# enter search keyword and submit
# use search_value argument to pass data
search_field.send_keys(search_value)
search_field.submit()
# get all the anchor elements which have product names
products = self.driver.find_elements(By.XPATH, "//h2[@class='product-name']/a")
expected_count = int(expected_count)
if expected_count > 0:
self.assertEqual(expected_count, len(products))
else:
msg = self.driver.find_element(By.CLASS_NAME, 'note-msg')
self.assertEqual("Your search returns no results.", msg.text)
Reference: Learning Selenium Testing Tools with Python by Unmesh Gundecha
'Testing Automation > 01. Selenium' 카테고리의 다른 글
10. Selenium | Handling Mouse Events (0) | 2022.03.20 |
---|---|
09. Selenium | Switch_to (0) | 2022.03.19 |
07. Selenium | Data-driven testing (0) | 2022.03.09 |
06. Selenium | Wait ( Implicit / Explicit) (0) | 2022.03.05 |
05. Selenium | Alert (0) | 2022.03.04 |