How to Read Config File in Python using Selenium with Examples

Vinayak Sharma

Posted On: July 16, 2021

view count247797 Views

Read time10 Min Read

This article is a part of our Content Hub. For more in-depth resources, check out our content hub on Selenium Python Tutorial.

Before starting with this tutorial on Python read config file let’s understand Selenium. Selenium is one of the best automation tools used for running automated tests through which you can perform relevant actions with the WebElements on the page. The major advantage of Selenium is that it supports a number of popular programming languages (e.g., Python, Java, JavaScript, C#, PHP, and Ruby).

When working with Selenium WebDriver, we need to follow the best practices to make the most out of the Selenium automation framework. One of them could be adding useful parameters like platform, browsers, browser versions, or device names in a manner that scaling up, organizing, and debugging the tests becomes easy. In addition, it should involve minimal changes in the test implementation! This is where configuration files come in handy, as configuration data can be added to those files with super ease. This, in turn, helps in improved maintenance of the test code.

In this Selenium Python tutorial, we deep dive into python read config file using Selenium. If you are preparing for an interview you can learn more through Selenium interview questions.

How to Create Configuration Files With Constant Parameters Using Python

Configuration files usually contain information that is organized in key:value pairs, where value is recognized by key. Configuration files usually have extensions like .ini, .cnf, .conf, .cfg, and .cf. There are several types of modules and techniques to create and parse configuration files in Python.

Here are some of the commonly used configuration files in Python:

  • Python Configuration Files
  • JSON Configuration Files
  • YAML Configuration Files
  • INI Configuration Files
  • XML Configuration Files

How to Create python read config file

Though Python files are not used to store configuration information, the best part is that storing configuration in Python files is a simple task. The major advantage of this approach is the ease in the management of configuration variables. For example, to store these configurations, we can create a config.py file.

You can use this config file in the actual implementation as shown below:

As seen above, we simply imported the config file in the code before using the configuration parameters present in it. Now that we covered on python read config file, we now move to create JSON config files using the Python JSON module.

Note: If you are new to Python and want to get started step-by-step (with example), you can go through our Python Tutorial and become an expert in automation testing with Python.

This certification is for professionals looking to develop advanced, hands-on expertise in Selenium automation testing with Python and take their career to the next level.

Here’s a short glimpse of the Selenium Python 101 certification from LambdaTest:

How to Create JSON configuration file

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It provides information in a human-readable format, making it easy for machines to parse the information stored in the JSON file. Shown below is an example that demonstrates how to use Python’s JSON module for creating a config.json file.

You can use this config file in the actual implementation as demonstrated below:

How to create YAML configuration file

YAML (YAML Ain’t Markup Language) is a human-readable data serialization language. In Python, we use the YAML module to parse the YAML file.

Here is how you can read the YAML config file in Python:

How to create INI configuration file

INI configuration file is a text-based structured syntax file that comprises key-value pairs.

To read this config file in Python, we will use the ConfigParser module. Run the below command on the terminal to install the ConfigParser module:

Here is how you can parse the INI configuration file in Python:

How to create XML configuration file

XML (Extensible Markup Language) lets you define rules for encoding documents that are easy to understand by humans and machines. The beautifulsoup module in Python is used for leveraging the advantages of XML configuration files.

Run the below command on the terminal to install the beautifulsoup module:

To parse this file, we will use the bs4 or beautifulsoup module in the implementation. Once parsed, the Python file will look like this:

Each parsing technique has its share of advantages & disadvantages. Hence, it is recommended to choose a technique that meets the project needs and requirements.

In the remaining part of Python read config file tutorial, we would use the INI configuration file since INI is the widely preferred configuration file format by Python developers.

Read – Create TestNG XML File & Execute Parallel Testing

Writing Selenium scripts for testing “add” functionality on a cloud Selenium Grid

So far in this tutorial on Python read config file, we have covered the essentials of python read config file. So, how can you leverage the benefits offered by configuration files in Python to make Selenium tests more readable and maintainable? Well, first, you need to move to a scalable and reliable cloud-based Selenium Grid so that you can run Selenium tests across different browsers and platform combinations.

LambdaTest is a cloud-based platform that enables you to perform live, interactive, and automated cross browser testing across 2000+ browsers and operating systems online. There are umpteen benefits of cloud testing, something that cannot be achieved by running Selenium tests on a local test infrastructure. Porting existing Python code to the LambdaTest Selenium Grid involves minimal changes, making it easy for QA engineers to leverage the platform in the shortest possible time.

Read – Detailed Selenium Python Tutorial

For demonstrating Python config file usage with an example, we would use the LambdaTest ToDo App on which the relevant tests are performed. The desired browser capabilities are generated using the LambdaTest capabilities generator.

cross browser testing

Here are the set of operations performed on the LambdaTest Sample ToDo App:

  1. Mark the first item and second items as complete. For this, we select the corresponding element using the Name locator (i.e., li1 and li2). You can check out our learning hub on Selenium Locators to ensure that you follow the best practices when using the appropriate web locators.
  2. Find the text element using ID and enter the details of the new item using SendKeys in Selenium.
  3. After typing values in the text box, we will click the add button.
  4. For taking a screenshot, we will use the save_screenshot method provided by Selenium. You can read our in-depth blog that covers taking screenshots using Python and Selenium.
  5. Now comes the best part – creating an INI configuration file for storing the essential configuration parameters:) Create a configuration file named config.ini. This is the complete project structure:
  6. Now, create a config.ini file with the configurations that would be used in the test. The username and access key for using the LambdaTest Selenium Grid are available in the LambdaTest Profile Section.

You could either pass this configuration file as a Command-Line argument or directly define it in the code.

We will see both methods for parsing the configuration file:

  • Pre-defining configuration file details in the implementation
  • Passing config file name as a command-line argument

Though PyUnit is the default test automation framework in Python, QA engineers prefer PyTest for Selenium automation testing. To get started with PyTest, check out the below video from LambdaTest YouTube Channel.

Predefined configuration file name in Python implementation

In our case, we have named the Python configuration file as config.ini. This file will be parsed to read the relevant configuration information for usage in the code.

Here is how we open and read contents from the config file:

Here is the entire implementation where we read the LT_USERNAME and LT_ACCESS_KEY from the configuration file. The read information is further used in the code for accessing the LambdaTest Grid. We have the PyUnit framework for the entire demonstration. Please refer to our PyUnit Framework Tutorial to get more information on the said framework.

Pass configuration file name as a Command-Line argument

For accepting the command-line argument(s), we will use the sys module in Python to pass the configuration name.

Here are some of the Python modules that can be used for passing command-line argument(s):

  • Using sys module
  • Using argparse module
  • Using optparse module and more

For demonstration, we have used the sys module to read the command-line argument(s):

Trigger the following command on the terminal to read the command-line arguments:

Visit the Automation Dashboard on LambdaTest to get further information about the execution of tests on the cloud grid:

Automation dashboard

Do you know how to upload and download a file in Selenium? Watch this video to learn how to upload and download files in Selenium WebDriver using different techniques.

Conclusion

In this Selenium Java tutorial, we have learnt about python read config file using the common configuration file formats like .cnf, .conf, .cfg, .cf, and .ini. We have also seen how multiple configuration files are read together, and their results can be merged into a single configuration using ConfigParser.

We then performed Selenium automation testing to test the “add” functionality in the to-do list app and take a screenshot to verify our results using a cloud Selenium Grid.

Author Profile Author Profile Author Profile

Author’s Profile

Vinayak Sharma

Full stack python developer and a tech enthusiast with strong communication and interpersonal skills. Highly adaptable to new environments, challenges, and increasing levels of responsibilities.

Blogs: 5



linkedintwitter

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free