From 051d66d1f9b61669ba82053e194e6d4bf3735c34 Mon Sep 17 00:00:00 2001 From: liangdaliang Date: Mon, 21 Apr 2025 11:40:34 +0800 Subject: [PATCH] =?UTF-8?q?UI=E6=B5=8B=E8=AF=95=E5=B7=A5=E5=85=B7=E7=B1=BB?= =?UTF-8?q?=E5=88=9D=E6=AD=A5=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test-common/pom.xml | 6 ++ .../com/test/common/utils/UITestUtil.java | 70 +++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 test-common/src/main/java/com/test/common/utils/UITestUtil.java diff --git a/test-common/pom.xml b/test-common/pom.xml index 40aa3f4..faa1abe 100644 --- a/test-common/pom.xml +++ b/test-common/pom.xml @@ -157,6 +157,12 @@ 1.1.2 + + org.seleniumhq.selenium + selenium-java + 4.30.0 + + diff --git a/test-common/src/main/java/com/test/common/utils/UITestUtil.java b/test-common/src/main/java/com/test/common/utils/UITestUtil.java new file mode 100644 index 0000000..28d16d2 --- /dev/null +++ b/test-common/src/main/java/com/test/common/utils/UITestUtil.java @@ -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(); + } + } +}