Parameterization in selenium – How To Do Parameterization In Pytest With Selenium?

Selenium Python – Concept of Parameterization

Parameterization in selenium: Sometimes, we come across situations where we need to execute the same test case, but with every execution, we need to use a different data set. Or sometimes, we need to create test data prior to our test suite execution. To resolve all these requirements, we should be familiar with the concept of parameterization.


  • Why do we need parameterization
  • What is parameterization
  • Creation of test data file
  • Parameterizing and login logout scenario


In this chapter, we will learn how we can use a text file to pass data to our tests and run it as many times as rows are available in our file. With this, we will understand the concept of parameterization.

Test data file

Test cases generally need test data for execution. When we write scripts to automate the test, it could be possible that we have hard-coded the test data within the test scripts. The drawback of this approach is if our test data needs to be changed for a test execution cycle, we will need to make changes at the test script level, thus making it prone to errors. So a good test script is when test data is kept outside the code.

To achieve the same, we need to parameterize our tests. In this, we replace the hard code values in the test with variables. At the time of execution, these variables are replaced by values that are picked from external data sources. These data sources could be text files, excel sheets, databases, JSON, XML, and others.

Parameterization and login logout scenario

Selenium doesn’t provide any provision to parameterize the tests. We write the code to parameterize the test using the programming language. In this chapter we will see how we can parameterize our test using a CSV file, and an excel file. The scenario we are going to pick is the login logout scenario, and we will parameterize it using two datasets—the first dataset will be for valid user and password combinations. And the second dataset will be for a bad username and password combination.

The data to be picked for the test is available in a file called login. CSV, which is kept in the dataset folder in the project. Refer to the following screenshot:

Selenium Python - Concept of Parameterization chapter 8 img 1

The dataset file login. CSV has the following data:, bpb@123,demo123

In a CSV file, the data is separated by a comma. The test script provided below reads the data using Python file handling commands and splits it based on a comma. It then passes these values for username and password in the script. The following test iterates twice, which is equal to the number of rows in this file:

from selenium import webdriver
import unitest

class Login(unitest.TestCase):
      def setup(self):
          self.driver ="D:\Eclipse\BPB\seleniumpython\seleniumpython\drivers\chromedriver.exe")

dex test_login(self):
    file = open("D:\Eclipse\BPB\seleniumpython\seleniumpython\datasets\login.csv", "r")
    for line in file:
         driver.find_element_by_link_text("My Account").click( )
         driver.find_element_by_id("tab1").click( )
         if(driver.page_source.find("My Account Information")l=-1):
            driver.find_element_by_link_text("log off").click( )
            driver.find_element_by_link_text("continue").click( )
            print("valid user credentail")
       print("Bad user credentail")
  file.close( )

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

    unitest.main( )

In the preceding program, we have written the scenario of login logout, but it is wrapped around a while loop. This while loop is basically reading file contents, until the end of the file is reached. So the entire script will execute as many times as the number of rows in the file.

As we execute the test, we will see that it passes for the first data set picked from the file, as it is a combination of a valid username and password. But for the second dataset, the test reports a failure. The preceding test will execute for as many rows as available in our login.csv dataset file.


In this chapter we have learned how to use test data for our tests, and the importance of keeping test data separate from the actual tests. We also saw how we can read data from a CSV file or a text-based file, and pass them to our test code.

In our next chapter, we will learn how to handle different types of web elements.

Related Articles: