上次介绍了如何利用healenium+java+selenium来实现selenium的自愈,这次介绍如何healenium+python+selenium。关于healenium+python+selenium网上资料更少,并且甚至是错误的。在著名的书籍《软件测试权威指南中》也是有一定问题的。现在介绍如下。
1.下载并安装postsql,并且建立数据库healenium(注意这个数据库请使用navicat手工建立,不要使用SQL语句建立)。
2.下载selenium-server-4.25.0.jar,由于本文selenium-server与node是在同一台机器上启动的。运行
java -jar selenium-server-4. 25.1.jar hub --port 4444
启动selenium-server,再运行
java -jar selenium-server-4.25.0.jar node --hub http://localhost:4444
注册到节点下。
在浏览器中输入
http://127.0.0.1:4444
出现如图界面。
记住这里请下载Selenium Grid Console
3 下载healenium-backend-3.4.8.jar,建立application.properties
# POSTGRESQL Connection Properties for Testcontainers overrided by DynamicPropertyRegistry
spring.datasource.url=jdbc:postgresql://localhost:5432/healenium?currentSchema=public
spring.datasource.username=postgres
spring.datasource.password=123456
spring.liquibase.default-schema=public
spring.jpa.properties.hibernate.default_schema=public
spring.datasource.hikari.schema=public
spring.datasource.hikari.connection-init-sql=SET search_path TO public
运行
java -jar healenium-backend-3.4.8.jar --server.port=7878
4 下载hlm-proxy-2.1.7.jar,建立application.properties
backend.url=http://localhost:7878
healenium.grid=http://localhost:4444
运行
java -jar hlm-proxy-2.1.7.jar
--backend.url=http://localhost:7878 --healenium.grid=http://localhost:4444
5.打开浏览器,输入127.0.0.1: http://localhost:7878/,出现
打开浏览器,输入127.0.0.1: http://localhost:7878/,出现如图http://127.0.0.1:4444一样的界面。输入http://localhost:8085/status,出现
{
"value":
{
"ready":
true,
"message":
"Selenium Grid ready.",
"nodes":
[
{
...
Python代码如下
import unittest
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class TestWithHealeniumServices(unittest.TestCase):
def test_web_application(self):
chrome_options = Options()
chrome_options.add_argument("--start-maximized")
chrome_options.add_argument("--disable-infobars")
chrome_options.add_argument("--ignore-certificate-errors")
# 设置基本的能力
chrome_options.set_capability("browserName", "chrome")
chrome_options.set_capability("browserVersion", "138.0") # 请更改为你本地的Chrome版本
# Healenium 配置
chrome_options.set_capability("healenium:backup", "true")
chrome_options.set_capability("healenium:capture", "true")
chrome_options.set_capability("healenium:initElements", "true")
chrome_options.set_capability("healenium:recovery-tries", "3")
chrome_options.set_capability("healenium:score", "0.7")
try:
logger.info("正在尝试连接Healenium代理...")
driver = webdriver.Remote(
command_executor='http://localhost:8085',
options=chrome_options
)
logger.info("WebDriver会话创建成功!")
driver.get("http://127.0.0.1:8080/sec/53/")
logger.info("成功打开页面")
button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.ID, "button"))
)
button.click()
logger.info("按钮点击成功")
except Exception as e:
logger.error(f"测试失败: {str(e)}")
raise
finally:
if 'driver' in locals():
driver.quit()
logger.info("浏览器已关闭")
if __name__ == "__main__":
unittest.main()
测试通过,将原文中的id="button",改为id="buttonClick"测试仍旧通过,说明自愈起作用。在这里特别强调,如果一开始
EC.element_to_be_clickable((By.ID, "button"))
写成
EC.element_to_be_clickable((By.ID, "buttonClick"))
测试是通不过的,只有第一次通过,后面自愈能力才会起作用。
写完此文,我觉得现在IT学术界的风气是不是太浮躁了。那么多不能运行的代码铺天盖地,满大街都是,请问,你们在发布之前自己运行过吗?