How to read Config Files in Python using Selenium

Learn how to read configuration files in Python using Selenium, the different types of config files, and how to manage them with Config parser.

Get Started free
How-to-read-Config-Files-in-Python-using-Selenium
Home Guide How to read Config Files in Python using Selenium

How to read Config Files in Python using Selenium

Managing configuration settings efficiently is important when automating web applications with Selenium in Python. Config files let you store environment-specific parameters separately and load them dynamically within your Selenium scripts.

Overview

What are Configuration Files in Python?

Configuration files in Python are external files that store an application’s settings, parameters, and preferences. These files let developers separate configuration details from the main code.

Types of Configuration Files in Python

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

In this guide, learn more about various methods of reading config files in Python and integrating them effectively with Selenium automation.

What are Configuration Files in Python?

Configuration files in Python are external files that store settings, parameters, and preferences for an application. These files let developers separate configuration details from the main code which makes the application more modular and simpler to manage.

These files contain information like database credentials, API keys, file paths, and user-defined settings.

The common formats or types for configuration files in Python are INI files, JSON files, YAML files, and ENV files.

Why Are Configuration Files Needed in Python?

Configuration plays a crucial role in software development and automation and offers a lot of benefits:

  • Easier Maintenance: Updating settings without modifying source code reduces the risk of errors.
  • Security: Storing credentials separately prevents hardcoding sensitive information in scripts.
  • Portability: Allows the same script to work across different environments (development, testing, production) without modifications.
  • Automation: Important for frameworks like Selenium, where different browser settings and credentials are required for automation.

BrowserStack Automate Banner

Types of Configuration Files in Python

Python supports multiple types of configuration files, each with its unique structure and use case. These formats help store and manage application settings efficiently. This makes it easier to update configurations without modifying code.

This section covers some of the common types of configuration files, including INI, JSON, YAML, and ENV files.

1. JSON Configuration Files

It stands for JavaScript Object Notation. It stores information in a key-value pair.

Example:

{
"Example": {
"Language": [
"Python"
],
"Modules": [
"Selenium",
],
"Enabled": 1
}
}
Copied

2. YAML Configuration Files

It stands for YAML Ain’t Markup Language. It also stores information in a key-value pair. The structure is slightly different from JSON.

Example:

Language: 'Python'

  Modules:

    - 'Selenium'

  Enabled: 1
Copied

3. INI Configuration Files

It stands for INItialization file. It is very intuitive to parse and understand.

Example:

[example]

Language=Python

Modules=Selenium

enabled=1
Copied

4. XML Configuration Files

It stands for EXtensible Markup Language. It stores information in tags. Each tag name represents the property and the tag value corresponds to the tag value.

Example:

<example>
<language>Python</language>
<modules priority="1">Selenium</modules>
<enabled>1</enabled>
</example>
Copied

All the above four configuration files convey the same information. 

  • The configuration name is Example. 
  • The Language within that configuration is Python.
  • The Module within that configuration is Selenium.

The Enabled flag within that configuration is set to 1.

What is ConfigParser?

ConfigParser is a built-in Python module for managing configuration files in the INI format. It facilitates reading, writing, and modifying configuration settings stored in structured key-value pairs under different sections.

It is useful for managing settings like database connections, API credentials, and user preferences in a structured and readable format.

ConfigParser Benefits

  • Human-Readable Format: INI files are easy to read and understand, even for non-programmers.
  • Structured Data Management: Organizes configurations into sections and key-value pairs for better clarity.
  • Flexibility: Allows modification of configuration settings without changing the source code.
  • Built-in Support in Python: No need for external dependencies, as ConfigParser is included in Python’s standard library.

Managing Python Configuration Files with ConfigParser

There are multiple steps involved in managing configuration files using ConfigParser, from creating and reading files to updating and writing new configurations. Check out this step-by-step tutorial:

Step 1: Install and Import ConfigParser

Since ConfigParser is a built-in module in Python, no additional installation is required. You can simply import it:

import configparser
Copied

Step 2: Create a Configuration File

A configuration file should be created with sections and key-value pairs:

[Settings]

username = admin

password = secret

url = https://browserstack.com
Copied

Step 3: Read Configuration Files

config = configparser.ConfigParser()

config.read('config.ini')




username = config['Settings']['username']

password = config['Settings']['password']

url = config['Settings']['url']




print(f'Username: {username}, URL: {url}')
Copied

Step 4: Modify Configuration Files

To update an existing configuration setting:

config.set('Settings', 'username', 'new_admin')

with open('config.ini', 'w') as configfile:

config.write(configfile)
Copied

Step 5: Add New Sections and Keys

config.add_section('Database')

config.set('Database', 'host', 'localhost')

config.set('Database', 'port', '5432')




with open('config.ini', 'w') as configfile:

config.write(configfile)
Copied

Step 6: Remove Sections or Keys

To delete a section or a specific key:

config.remove_section('Database')

config.remove_option('Settings', 'password')

with open('config.ini', 'w') as configfile:

config.write(configfile)
Copied

Best Practices for Managing Configuration Files in Python

Here are some of the best practices to be followed for managing configuration files in Python:

  • Use the Right Format: Choose a format that best suits your application needs like INI, JSON, YAML, ENV.
  • Use Environment Variables for Sensitive Data: Avoid storing API keys and passwords in plain text within configuration files.
  • Use a Centralized Configuration Management System: For large-scale applications, consider using a centralized system like AWS Parameter Store or HashiCorp Vault.
  • Encrypt Sensitive Information: If storing credentials in a config file, use encryption or secure vaults to protect them.
  • Automate Configuration Loading: Use scripts or libraries like dotenv to automate loading configurations dynamically.

Understanding the role of Python & Selenium in Automation

Python is the most popular programming language in recent times. It has several libraries for catering use cases like web scraping, automation, etc. Selenium is a very popular python library commonly used for automation testing. It can be used to perform tasks like:

  1. Visit a webpage
  2. Click on several clickable elements of the page
  3. Scroll a page
  4. Type something on the Input box within a page
  5. Pressing keys
  6. Even playing browser-based games

There are several other applications of Selenium. In this article, we will focus on using Selenium for a python selenium config file test case. The test case will be stored in a config file, and we will extract the data from that config file in Python language and then execute the test case using Selenium.

Talk to an Expert

Prerequisites – Initial Requirements

Step 1: Install Python 

  • If you are using Windows OS, you need first to install Python. You can download Python using this link.
  • If you are using a Mac system, Python comes preinstalled on macOS since version 10.8
  • Python comes preinstalled on most Linux distributions
  • If you are a beginner and want to not get in the nitty-gritty of installing standalone python, you can download Anaconda. It is a simple framework that does all the heavy lifting installation, you can download it from here.

Step 2: Install Selenium

Once you have installed Python, you can open the command line (for windows) or the terminal (for Mac and Linux) and simply use the pip command like below to install Selenium.

pip install selenium
Copied

Step 3: Download chromedriver

Selenium needs a web driver to simulate a browser visit. Supported browsers are:

  • Chrome
  • Firefox
  • Internet Explorer
  • Safari
  • Opera
  • PhantomJS (invisible)

In this article, we will explore using Chrome and Firefox browsers.

For that we need to download the relevant chromedriver from the below location:

And relevant geckodriver for Firefox from below location:

You need to store the chromedriver.exe and GeckoDriver in the folder where you are running your code.

Problem Statement

Here you have a very simple test case of opening Google and searching for a search_term and pressing enter. This search_term will be stored in a config file browserstack_config_v2.json That file contains the below configuration

{
"browsers": [
{
"browser": "chrome",
"browser_version": "latest",
"search_term": "browserstack"
},
{
"browser": "firefox",
"browser_version": "latest",
"search_term": "browserstack automate"
}
]
}
Copied

Here, you will be guided on how to read this config file and perform the test case.

Step 1: Import necessary libraries

Python Code:

from selenium import webdriver 
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.options import Options
import json
import warnings
warnings.filterwarnings("ignore")
Copied

Step 2: Load the config file

We will use the JSON library to load the json file containing configuration.

Python Code:

with open('browserstack_config_v2.json') as json_file:

    config_data = json.load(json_file)

print(config_data)
Copied

Output:

{'browsers': [{'browser': 'chrome',

   'browser_version': 'latest',

   'search_term': 'browserstack'},

  {'browser': 'firefox',

   'browser_version': 'latest',

   'search_term': 'browserstack automate'}]}
Copied

Now, the config_data variable contains the configuration stored as a dictionary.

Step 3: Load the configuration of the first browser and perform the test.

We will load the first configuration, and write our entire test case. We will also print whether the test is successful or unsuccessful. Once, the test case runs, we will also close the driver

Python Code:

curr_config = config_data['browsers'][0]
curr_browser = curr_config['browser']
curr_search_term = curr_config['search_term']

if(curr_browser=='chrome'):
driver = webdriver.Chrome('path to your chromedriver.exe')
elif(curr_browser=='firefox'):
driver = webdriver.Firefox(executable_path=''path to your geckodriver.exe')
try: 
print('1. Opening Website')
url = 'http://www.google.com'
driver.get(url)
print('2. Performing test')
inputElement = driver.find_element(By.NAME, 'q')
inputElement.send_keys(curr_search_term)
inputElement.submit()
print('3. Test is Successful')
except Exception as e:
print('Test is Unsuccessful')
driver.close()
Copied

It will invoke a Selenium session i.e. it will open a Chrome browser and do the actions we coded.

Also, additionally, it will print whether the test is successful or unsuccessful. For our example, the test ran successfully, so we see the below output:

  1. Opening Website
  2. Performing test
  3. Test is Successful

Step 4: Putting it all together (we will save the entire python code as selenium_test.py)

from selenium import webdriver 
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.by import By
import json
import warnings
warnings.filterwarnings("ignore")

with open('browserstack_config_v2.json') as json_file:
config_data = json.load(json_file)


for curr_config in config_data['browsers']:
curr_browser = curr_config['browser']
curr_search_term = curr_config['search_term']

if(curr_browser=='chrome'):
driver = webdriver.Chrome('path to your chromedriver.exe')
elif(curr_browser=='firefox'):
driver = webdriver.Firefox(executable_path=’path to your geckodriver.exe')

try: 
print('1. Opening Website')
url = 'http://www.google.com'
driver.get(url)
print('2. Performing test')
inputElement = driver.find_element(By.NAME, 'q')
inputElement.send_keys(curr_search_term)
inputElement.submit()
print('3. Test is Successful')
except Exception as e:
print('Test is Unsuccessful')
driver.close()
Copied

Step 5: Execute the command to run the test

You can open the command line (for windows) or the terminal (for Mac and Linux) and execute the below command

python selenium_test.py
Copied

Output:

  1. Opening Website
  2. Performing test of searching: browserstack
  3. Test is Successful

Conclusion

Reading configuration files in Python using Selenium improves browser automation by making test codes more maintainable, scalable, and easy to understand. Using libraries like configparser for .ini files, json for JSON, and yaml for YAML allows testers to manage browser settings, credentials, and other parameters efficiently. This helps to define tests to run across multiple devices and browsers.

Also, it simplifies cross-browser testing through consistent and reliable tests across multiple browsers. Harnessing the full potential of Selenium and Python goes a long way in enabling developers and QA to inspect, automate, and validate website functionality. Using tools like BrowserStack along with Selenium can help you run cross-browser and real-device tests seamlessly across 3500+ real-device-browser-OS combinations.

Run Selenium Python Tests with BrowserStack

Useful Resources for Selenium and Python

Tags
Automation Testing Selenium