In the current technological era, machine learning (ML) and artificial intelligence (AI) are increasingly ubiquitous, providing user-friendly solutions that enhance the efficiency and simplicity of our lives. Thus, it is paramount that our test automation frameworks evolve beyond conventional methods. This article delves into the integration of open-source AI for image detection within test automation frameworks, making them quicker, more accurate, and efficient.
To implement AI image detection in your test automation framework, follow these key steps:
Capture Element Screenshots:
Import Necessary Libraries:
PyAutoGUI
and Selenium
.Create Methods for Image Interactions:
Utilize Methods in Framework:
Capturing Screenshots:
Importing Libraries:
import pyautogui
from selenium import webdriver
Creating Methods:
def click_on_image(image_path):
location = pyautogui.locateCenterOnScreen(image_path)
if location:
pyautogui.click(location)
else:
raise Exception("Image not found on screen")
This method takes the image path as an input, locates the image on the screen, retrieves the coordinates of the center, and clicks on the identified coordinates.
Running Demo:
Here is an example implementation for a demo:
Capturing Screenshots:
- resources/username.png
- resources/password.png
- resources/sign_in.png
Importing Libraries:
import pyautogui
from selenium import webdriver
Creating Methods:
def click_on_image(image_path):
location = pyautogui.locateCenterOnScreen(image_path)
if location:
pyautogui.click(location)
else:
raise Exception("Image not found on screen")
def fill_field(image_path, text):
click_on_image(image_path)
pyautogui.write(text)
Utilizing Methods:
driver = webdriver.Chrome()
driver.get("http://example.com")
fill_field("resources/username.png", "myusername")
fill_field("resources/password.png", "mypassword")
click_on_image("resources/sign_in.png")
This method demonstrates launching a website, locating fields using captured screenshots, performing text entry, and simulating a button click based on the detected image coordinates.
This approach eliminates the need for complicated XPaths or locators and replicates human interaction more closely. While it necessitates keeping the screen on during test script execution, it addresses numerous challenges in quick UI-based automation.
Q1: What are the prerequisites for implementing this method in a test automation framework?
A1: You need to capture screenshots of the elements, import necessary libraries like PyAutoGUI
and Selenium
, and integrate the methods into your framework.
Q2: Do I need to keep the screen on while running the test scripts?
A2: Yes, the screen must remain on during script execution to facilitate real-time image detection.
Q3: How does this approach handle complex XPaths or dynamic elements?
A3: By using image detection, this approach bypasses the need for XPaths, simplifying the interaction with complex or dynamically changing elements.
Q4: What happens if the image is not found on the screen?
A4: The script raises an exception indicating that the image was not found, prompting further investigation or script adjustment.
In addition to the incredible tools mentioned above, for those looking to elevate their video creation process even further, Topview.ai stands out as a revolutionary online AI video editor.
TopView.ai provides two powerful tools to help you make ads video in one click.
Materials to Video: you can upload your raw footage or pictures, TopView.ai will edit video based on media you uploaded for you.
Link to Video: you can paste an E-Commerce product link, TopView.ai will generate a video for you.