UI测试工具类初步提交
This commit is contained in:
@@ -157,6 +157,12 @@
|
||||
<version>1.1.2</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.seleniumhq.selenium</groupId>
|
||||
<artifactId>selenium-java</artifactId>
|
||||
<version>4.30.0</version> <!-- 根据需要选择版本 -->
|
||||
</dependency>
|
||||
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
package com.test.common.utils;
|
||||
|
||||
import org.openqa.selenium.By;
|
||||
import org.openqa.selenium.WebDriver;
|
||||
import org.openqa.selenium.WebElement;
|
||||
import org.openqa.selenium.chrome.ChromeDriver;
|
||||
import org.openqa.selenium.support.ui.ExpectedConditions;
|
||||
import org.openqa.selenium.support.ui.WebDriverWait;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
/**
|
||||
* @author liangdaliang
|
||||
* @Description:
|
||||
* @date 2025-04-21 09:53
|
||||
*/
|
||||
public class UITestUtil {
|
||||
public static void main(String[] args) {
|
||||
// 设置 WebDriver 的路径 (根据你的实际路径修改)
|
||||
System.setProperty("webdriver.chrome.driver", "E:/AA/chromedriver-win64/chromedriver.exe");
|
||||
|
||||
// 初始化 ChromeDriver
|
||||
WebDriver driver = new ChromeDriver();
|
||||
|
||||
try {
|
||||
// 打开目标网页的登录页面
|
||||
String url = "https://demo.halo.run/login"; // 替换为实际的登录页面 URL
|
||||
driver.get(url);
|
||||
|
||||
// 最大化浏览器窗口
|
||||
driver.manage().window().maximize();
|
||||
|
||||
// 创建 WebDriverWait 实例,设置最大等待时间为 10 秒
|
||||
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(60));
|
||||
|
||||
// 等待用户名输入框可见并输入用户名
|
||||
WebElement usernameInput = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username")));
|
||||
usernameInput.sendKeys("ldlspace"); // 替换为实际用户名
|
||||
|
||||
// 等待密码输入框可见并输入密码
|
||||
WebElement passwordInput = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("plainPassword")));
|
||||
passwordInput.sendKeys("Ldlspace@163"); // 替换为实际密码
|
||||
|
||||
// 等待登录按钮可点击并点击
|
||||
WebElement loginButton = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(.,'登录')]")));
|
||||
loginButton.click();
|
||||
Thread.sleep(3000);
|
||||
String currentUrl = driver.getCurrentUrl();
|
||||
System.out.println(currentUrl);
|
||||
String targetUrl = "https://demo.halo.run/uc/profile"; // 替换为实际的目标页面 URL
|
||||
wait.until(ExpectedConditions.urlToBe(targetUrl)); // 验证 URL 是否为目标页面
|
||||
|
||||
// 等待登录成功后的页面标题变为预期值
|
||||
wait.until(ExpectedConditions.titleContains("我的 - Halo 演示站点")); // 根据实际情况修改标题内容
|
||||
|
||||
// 验证是否登录成功
|
||||
if (driver.getTitle().contains("我的 - Halo 演示站点")) {
|
||||
System.out.println("登录成功!");
|
||||
} else {
|
||||
System.out.println("登录失败!");
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
// 关闭浏览器
|
||||
driver.quit();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user