Generative AI with Python Selenium
Oct 22, 2024 2 Min Read 1143 Views
(Last Updated)
In the world of automation, Selenium has long been a cornerstone for web applications. However, the advent of Generative AI has introduced a new dimension to automation which promises to enhance efficiency, accuracy, and creativity.
This blog post delves into the exciting intersection of Generative AI with Python Selenium exploring how Python can be leveraged to harness the power of both technologies.
By combining the robust web automation capabilities of the Selenium framework with the innovative potential of Generative AI, automation testers can create more intelligent, adaptive, and efficient automation solutions. Let us see more about it in this article!
Table of contents
- Prerequisites for Generative AI with Python Selenium
- Conclusion
Prerequisites for Generative AI with Python Selenium
For the Generative AI in action with Python Selenium we need to have a problem statement first. With that problem we will request the Google Gemini AI model to write an automation testing test-case.
Followed by which we will see and evaluate whether the code works and what more we can do to enhance the quality of our Python code.
The journey is going to be exciting as we humans will be going to evaluate the Python code written by a Generative AI model.
The problem statement for our Blog is:-
“Write a Python script for automation testing using Python Selenium framework and Pytest to login into the OrangeHRM dashboard using the Username “admin” and Password “admin123”. “
Now let us see what type of Python automation testing code the Google Gemini AI model makes for us!
The full code is as follows
import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By
@pytest.fixture
def driver():
driver = webdriver.Chrome() # Replace 'chromedriver' with the path to your ChromeDriver
yield driver
driver.quit()
def test_orangehrm_login(driver):
# Navigate to OrangeHRM login page
driver.get("https://opensource-demo.orangehrmlive.com/web/index.php/auth/login")
# Find username and password fields
username_field = driver.find_element(By.ID, "username")
password_field = driver.find_element(By.ID, "password")
# Enter credentials
username_field.send_keys("admin")
password_field.send_keys("admin123")
# Submit login form
login_button = driver.find_element(By.XPATH, "//button[@type='submit']")
login_button.click()
# Assert successful login (e.g., check for a specific element on the dashboard)
assert "Dashboard" in driver.title
Now, if we look into the code it is quite certain that it will work and run properly. Great! We do not need to write any code as Generative AI will take all the pains of automation testers now.
But ! Here is a Catch !!! The code fails to follow the rules and guidelines set by Page Object Model (POM).
The correct folder structure is
The entire code is given below which follows the Page Object Model (POM)
# data.py
# This file consists of Test Information like username, password, XPATH etc
# Python Class for Username and Password
class Suman_Data:
username = "Admin"
password = "admin123"
# Python Class for Selenium Selectors
class Suman_Selectors:
input_box_username = "username"
input_box_password = "password"
login_xpath = '//*[@id="app"]/div[1]/div/div[1]/div/div[2]/div[2]/form/div[3]/button'
# testSuman.py
from selenium import webdriver
from selenium.webdriver.common.by import By
from Test_Data import data
import pytest
import time
class Test_Suman:
url = "https://opensource-demo.orangehrmlive.com"
# Booting Method for running the Python Tests
@pytest.fixture
def booting_function(self):
self.driver = webdriver.Firefox()
yield
self.driver.close()
def test_get_title(self, booting_function):
self.driver.get(self.url)
assert self.driver.title == 'suman'
print("SUCCESS # Web Title Captured Successfully")
def test_login(self, booting_function):
self.driver.get(self.url)
time.sleep(5)
self.driver.find_element(by=By.NAME, value=data.Suman_Selectors.input_box_username).send_keys(data.Suman_Data.username)
self.driver.find_element(by=By.NAME, value=data.Suman_Selectors.input_box_password).send_keys(data.Suman_Data.password)
self.driver.find_element(by=By.XPATH, value=data.Suman_Selectors.login_xpath).click()
assert self.driver.title == 'OrangeHRM'
print("SUCCESS # LOGGED IN WITH USERNAME {username} and PASSWORD {password}".format(username=data.Suman_Data.username, password=data.Suman_Data.password))
From the codes written by Generative Model and by human-being we can say one thing that Generative AI is going to help us to write day-to-day codes without any issues but to manage and maintain code quality we need human interference which can ensure that the code which is being written will be
- Reusable
- More readable
- Sans with duplication
In case you want to learn more about automation testing with Selenium, consider enrolling for GUVI’s certified Automation Testing with Selenium Course online course that teaches you everything from scratch and also provides you with an industry-grade certificate!
Conclusion
Therefore, we can say that the combination of Python Selenium and Generative AI opens up a world of possibilities for automation, and testing. By leveraging the strengths of both technologies we can automate repetitive tasks, generate personalized content, and enhance user experiences. As Generative AI and automation continue to evolve we can expect to see even more groundbreaking advancements in the future and a bright future for budding and working automation testers as well.
Did you enjoy this article?