视觉测试对于确保 Web 应用程序的外观在更新或更改后保持一致和视觉正确至关重要。本博客将指导您使用 Selenium 进行浏览器自动化,并使用自定义图像比较实用程序来执行视觉测试。
视觉测试通过比较不同时间点拍摄的屏幕截图来帮助检测 UI 中的意外更改。在本指南中,我们将使用 Selenium 自动化 Web 交互并获取屏幕截图,然后使用称为视觉比较的图像比较实用程序来比较这些屏幕截图。
在我们开始之前,请确保您已安装以下软件:
安装硒:
pip install selenium
安装视觉比较包:
pip install Visual-comparison
让我们编写一个 Selenium 脚本,用于登录示例网站、截取屏幕截图并将其与基线图像进行比较。
第1步:初始化WebDriver并打开网页
首先,初始化WebDriver并导航到目标网页:
from selenium import webdriver from selenium.webdriver.common.by import By # Initialize the WebDriver driver = webdriver.Chrome() # Open the target webpage driver.get("https://www.saucedemo.com/v1/") driver.maximize_window() driver.implicitly_wait(5)
第 2 步:执行登录
接下来,填写用户名和密码字段并单击登录按钮登录网站。目前登录后可视化测试仪表板页面。您可以根据您的要求修改此代码:
# Login to the website username = driver.find_element(By.ID, "user-name") username.send_keys("standard_user") password = driver.find_element(By.ID, "password") password.send_keys("secret_sauce") # Click on the login button login_button = driver.find_element(By.ID, "login-button") login_button.click()` **Step 3: Take a Screenshot** After logging in, take a screenshot of the page and save it: # Take a screenshot after login to visualize the changes actual_image_path = "actual.png" driver.save_screenshot(actual_image_path) # Close the browser driver.quit()
第 4 步:比较图像
使用自定义图像比较实用程序将基线图像 (expected.png) 与新拍摄的屏幕截图 (actual.png) 进行比较:
from visual_comparison.utils import ImageComparisonUtil # Load the expected image and the actual screenshot expected_image_path = "expected.png" expected_image = ImageComparisonUtil.read_image(expected_image_path) actual_image = ImageComparisonUtil.read_image(actual_image_path) # Choose the path to save the comparison result result_destination = "result.png" # Compare the images and save the result similarity_index = ImageComparisonUtil.compare_images(expected_image, actual_image, result_destination) print("Similarity Index:", similarity_index) # Asserting both images match_result = ImageComparisonUtil.check_match(expected_image_path, actual_image_path) assert match_result
这是结合了所有步骤的完整脚本:
""" This python script compares the baseline image with the actual image. After any source code modification, the visual changes are compared easily through this script. """ from selenium import webdriver from selenium.webdriver.common.by import By from visual_comparison.utils import ImageComparisonUtil # Initialize the WebDriver driver = webdriver.Chrome() # Open the target webpage driver.get("https://www.saucedemo.com/v1/") driver.maximize_window() driver.implicitly_wait(5) # Login to the website username = driver.find_element(By.ID, "user-name") username.send_keys("standard_user") password = driver.find_element(By.ID, "password") password.send_keys("secret_sauce") # Click on the login button login_button = driver.find_element(By.ID, "login-button") login_button.click() # Take a screenshot after login to visualize the changes actual_image_path = "actual.png" expected_image_path = "expected.png" driver.save_screenshot(actual_image_path) # Close the browser driver.quit() # Load the expected image and the actual screenshot expected_image = ImageComparisonUtil.read_image(expected_image_path) actual_image = ImageComparisonUtil.read_image(actual_image_path) # Choose the path to save the comparison result result_destination = "result.png" # Compare the images and save the result similarity_index = ImageComparisonUtil.compare_images(expected_image, actual_image, result_destination) print("Similarity Index:", similarity_index) # Asserting both images match_result = ImageComparisonUtil.check_match(expected_image_path, actual_image_path) assert match_result
Output Similarity Index: 1.0 (i.e.No Visual Changes)
注意:在执行上述脚本之前创建基线图像/预期图像。参考此仓库GitHub链接
本指南演示了如何使用 Selenium 进行网络自动化和视觉比较包来比较屏幕截图来执行视觉测试。通过自动化视觉测试,您可以确保 UI 更改不会引入任何视觉缺陷,从而保持一致的用户体验。
以上是使用 Selenium 和视觉比较进行视觉回归测试的详细内容。更多信息请关注PHP中文网其他相关文章!