Course Content
Selenium Python First Script
Selenium Python First Script
0/2
HTML Form elements
HTML Form elements
0/1
Handle HTML Frames
Handle HTML Frames
0/2
Handling Dropdown Lists
Handling Dropdown Lists
0/2
Selenium Action class
Selenium Action class
0/1
Selenium Python for Beginners [ 2024 ]
About Lesson

Test Data using CSV file

In this lesson, you will learn how to use test data using CSV files. The test reads the test data from the file and uses it in test logic. 

Create a test CSV file with the extension .csv. The test data in this file is separated by commas (, ).

Example

# OrangeHRM Login Page Test
# www.TestingDocs.com

import unittest
from selenium import webdriver
from LoginPage import LoginPage
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class TestLogin(unittest.TestCase):
    
    def setUp(self):
        self.driver = webdriver.Firefox()
        
    def test_successful_login(self):
        file = open("testdata.csv")
        for line in file:
            self.login_page = LoginPage(self.driver)
            self.login_page.load()
            tokens = line.split(",")
            self.login_page.login(tokens[0].strip(), tokens[1].strip())
            self.dropdown = (By.CLASS_NAME,"oxd-userdropdown")
            WebDriverWait(self.driver, 30).until(EC.presence_of_element_located(self.dropdown))
            heading = self.driver.find_element(By.CLASS_NAME, 'oxd-topbar-header-breadcrumb-module')
            self.assertIn("Dashboard", heading.text)
            self.driver.get("https://flowchartnow.com/hrm/web/index.php/auth/logout")

        
    def tearDown(self):
        self.driver.quit()

if __name__ == "__main__":
    unittest.main()

 

Join the conversation