Debug School

rakesh kumar
rakesh kumar

Posted on

Write a test scripts for Data Driven Framework for Multiple Data sets in Selenium Python

write a program How to test the login/Register to a webpage with 100 different data sets of usernames and passwords in in Selenium python

write a program How to test the login/Register to a webpage with Excel sheet in in Selenium python

Testing login and registration to a webpage with 100 different data sets of usernames and passwords in Selenium with Python can be accomplished using a Data-Driven approach. In this example, I'll demonstrate how to read data from a CSV file containing 100 different sets of credentials and perform login and registration actions using Selenium.

Prepare the Data:
Create a CSV file named userdata.csv with two columns: "Username" and "Password." Populate it with 100 different data sets of usernames and passwords.

Here's a sample data format for userdata.csv:

Username,Password
user1,password1
user2,password2
user3,password3
...
user100,password100
Enter fullscreen mode Exit fullscreen mode

Write the Test Script:

import csv
from selenium import webdriver

# Function to perform registration
def perform_registration(username, password):
    driver = webdriver.Chrome()
    driver.get("https://your-website.com/register")

    # Find registration form elements and fill them with data
    driver.find_element_by_name("username").send_keys(username)
    driver.find_element_by_name("password").send_keys(password)
    driver.find_element_by_name("confirm_password").send_keys(password)
    driver.find_element_by_name("register_button").click()

    # Add validation or other test steps as needed

    driver.quit()

# Function to perform login
def perform_login(username, password):
    driver = webdriver.Chrome()
    driver.get("https://your-website.com/login")

    # Find login form elements and fill them with data
    driver.find_element_by_name("username").send_keys(username)
    driver.find_element_by_name("password").send_keys(password)
    driver.find_element_by_name("login_button").click()

    # Add validation or other test steps as needed

    driver.quit()

# Read data from the CSV file and execute registration and login for each set of data
with open('userdata.csv', 'r') as file:
    data = csv.DictReader(file)
    for row in data:
        username = row['Username']
        password = row['Password']

        print(f"Registering with username: {username}, password: {password}")
        perform_registration(username, password)

        print(f"Logging in with username: {username}, password: {password}")
        perform_login(username, password)
Enter fullscreen mode Exit fullscreen mode

Output:
When you run the script, it will execute registration and login actions for each set of credentials. You will see output messages indicating the actions being performed with the provided data.

Additional Considerations:

Customize the functions perform_registration and perform_login to match the structure and functionality of the registration and login pages on your website.
Enhance the script with assertions, validations, and error handling to thoroughly test the behavior of your application.
You can easily extend the number of data sets by adding more entries to the CSV file.

Another Example

Creating a Data-Driven Framework in Selenium with Python allows you to run the same test script with multiple sets of data. In this example, I'll show you how to design a basic Data-Driven Framework using Python and Selenium:

Prepare the Data:
First, you need to prepare your test data. In this example, we'll use a CSV file (testdata.csv) to store the data. Create a CSV file with columns for test cases and their associated data. For instance:

Test Case,Username,Password
TC1,user1,password1
TC2,user2,password2
TC3,user3,password3
Enter fullscreen mode Exit fullscreen mode

Write the Test Script:
Create a Python script to read the data from the CSV file and execute the test cases using the Selenium WebDriver.

import csv
from selenium import webdriver

# Function to perform the login
def perform_login(username, password):
    driver = webdriver.Chrome()
    driver.get("https://example.com")
    driver.find_element_by_name("username").send_keys(username)
    driver.find_element_by_name("password").send_keys(password)
    driver.find_element_by_name("loginButton").click()

    # Add validation or other test steps as needed

    driver.quit()

# Read data from the CSV file and execute test cases
with open('testdata.csv', 'r') as file:
    data = csv.DictReader(file)
    for row in data:
        test_case = row['Test Case']
        username = row['Username']
        password = row['Password']

        print(f"Executing {test_case} with username: {username}, password: {password}")
        perform_login(username, password)
Enter fullscreen mode Exit fullscreen mode

Output:
When you run the script, it will execute the test cases for each set of data in the CSV file. You'll see output messages indicating which test case is being executed along with the provided data.

Additional Considerations:

You can add assertions, validations, or other test steps within the perform_login function to verify the behavior of your application.
To use this script with a different set of data, simply update the CSV file with new test cases and data.
This is a simple example of a Data-Driven Framework. In a real-world scenario, you might need more extensive error handling, reporting, and logging to make your framework robust and maintainable. You can also explore other data sources like Excel files or databases to store and retrieve test data.

write a program How to test the login/Register to a webpage with Excel sheet in in Selenium python

Creating a Data-Driven Framework in Selenium with Python allows you to run the same test script with multiple sets of data. In this example, I'll show you how to create a basic Data-Driven Framework using Python, Selenium, and an Excel file to store the data.

Step 1: Prepare the Data

First, create an Excel file (e.g., testdata.xlsx) to store your test data. This example uses the openpyxl library to read data from an Excel file. The Excel file should contain a sheet with headers and test data. Each row represents a set of data for a test case. For this example, you can structure your Excel file like this:

Image description

Step 2: Install openpyxl

You'll need to install the openpyxl library to read data from the Excel file. You can do this with the following command:

pip install openpyxl
Enter fullscreen mode Exit fullscreen mode

Step 3: Write the Test Script

Now, let's write a Python script that reads the test data from the Excel file and uses it to perform a test action with Selenium.

import openpyxl
from selenium import webdriver

# Load the Excel workbook and select the desired sheet
workbook = openpyxl.load_workbook('testdata.xlsx')
sheet = workbook['Sheet1']

# Function to perform a test action (e.g., login)
def perform_test_action(username, password):
    driver = webdriver.Chrome()
    driver.get('https://example.com')  # Replace with your test URL
    username_input = driver.find_element_by_name('username')
    password_input = driver.find_element_by_name('password')
    login_button = driver.find_element_by_name('loginButton')  # Replace with the actual button element
    username_input.send_keys(username)
    password_input.send_keys(password)
    login_button.click()

    # Add validation or other test steps as needed

    driver.quit()

# Iterate through the rows in the Excel sheet and execute test actions
for row in sheet.iter_rows(min_row=2, values_only=True):
    test_case, username, password = row
    print(f"Executing {test_case} with username: {username}, password: {password}")
    perform_test_action(username, password)
Enter fullscreen mode Exit fullscreen mode

Step 4: Output

When you run the script, it will execute the test actions for each set of data in the Excel file. You'll see output messages indicating which test case is being executed along with the provided data.

Step 5: Additional Considerations

Customize the perform_test_action function to match the test actions you want to perform on your website or application.
Enhance the script with assertions, validations, and error handling to thoroughly test the behavior of your application.
You can easily extend the number of data sets by adding more rows to the Excel file.

Top comments (0)