This script reads login data from testdata.json, opens a browser, enters the data, clicks login, and checks if login succeeded.
import json
from selenium import webdriver
from selenium.webdriver.common.by import By
# Load test data from JSON file
def load_test_data(filename):
with open(filename) as file:
return json.load(file)
# Test function using Selenium and JSON data
def test_login():
data = load_test_data('testdata.json')
driver = webdriver.Chrome()
driver.get(data['url'])
# Find username and password fields and enter data
driver.find_element(By.ID, 'username').send_keys(data['username'])
driver.find_element(By.ID, 'password').send_keys(data['password'])
# Click login button
driver.find_element(By.ID, 'loginBtn').click()
# Check if login was successful by looking for logout button
logout_button = driver.find_element(By.ID, 'logoutBtn')
assert logout_button.is_displayed(), 'Login failed!'
driver.quit()
# Run the test
if __name__ == '__main__':
test_login()