许多文章都介绍healenium Java,但是都没有讲透,下面进行详细介绍。Healenium分为服务器端和客户端,必须二者都配好才可以运转。
一、Healenium服务器端
1.建立项目MyHealenium,采用Meaven架构。修改pom.xml。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com</groupId>
<artifactId>MyHealenium</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<maven-compiler-plugin-version>2.3.2</maven-compiler-plugin-version>
<selenium.version>4.34.0</selenium.version>
<healenium.version>3.5.6</healenium.version>
<junit.version>5.10.1</junit.version>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.epam.healenium</groupId>
<artifactId>healenium-web</artifactId>
<version>${healenium.version}</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>${selenium.version}</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>${selenium.version}</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-support</artifactId>
<version>${selenium.version}</version>
</dependency>
</dependencies>
</project>
注意:${selenium.version}一定要用最新版本。
2.建立Java程序MyTest.java
package com.jerry;
import com.epam.healenium.SelfHealingDriver;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class MyTest{
@Test
public void TestClickButton(){
WebDriver delegate =new ChromeDriver();
SelfHealingDriver driver=SelfHealingDriver.create(delegate);
driver.get("http://127.0.0.1:8080/sec/53/");
driver.findElement(By.id("button")).click();
driver.quit();
}
}
3.其中http://127.0.0.1:8080/sec/53/的HTML代码为。
<html>
<body>
<button id="button" style="height:50px; width:200px">
点击我
</button>
<div>
</div>
</body>
</htm1>
以JUnit5运行MyTest.java,测试通过,修改
以JUnit5运行MyTest.java,测试失败。
4.在test/resources/healenium.properties做如下设置
recovery-tries = 1
score-cap = 0.5
heal-enabled = true
hlm.server.url = http://localhost:7878
hlm.imitator.url = http://localhost:8081
含义如下
二、 Healenium客户端
1.在https://github.com/healenium/healenium-backend/下载healenium-backend.zip
2.解压healenium-backend.zip
3.一般网站上介绍在Dock中运行PostgreSQL,由于我在Windows运行,Dock不是很稳定,到PostgreSQL官网下载。然后运行。建立数据库healenium(可使用Premium建立)。
4.在%HEALENIUM_BACKEND_HOM \build\libs下建立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
server.port=8081
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,
5,采用
C:\Tools\healenium-backend\build\libs>java -jar healenium-backend-3.4.8.jar --server.port=7878
启动healenium-backend。
在浏览器中输入
http://localhost:7878/,出现
6,运行MyTest.java,
<button id="button" style="height:50px; width:200px">
测试通过。修改为
<button id="buttonClick" style="height:50px; width:200px">
运行MyTest.java,测试仍旧通过。
三、healenium原理
如下图所示