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://sso.gxnews.com.cn/"; // 替换为实际的登录页面 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.name("username")));
            usernameInput.sendKeys("ldlspace"); // 替换为实际用户名

            // 等待密码输入框可见并输入密码
            WebElement passwordInput = wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("password")));
            passwordInput.sendKeys("Ldl08844"); // 替换为实际密码

            // 等待登录按钮可点击并点击
            WebElement loginButton = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"button\"]")));
            loginButton.click();
            Thread.sleep(3000);
            String currentUrl = driver.getCurrentUrl();
            System.out.println(currentUrl);
            WebElement welcome = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"list\"]/ul/li[1]")));
            String welcomeText = welcome.getText();
            if (welcomeText.equals("ldlspace,欢迎您登录")) {
                System.out.println("登录成功!");
            } else {
                System.out.println("登录失败！");
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭浏览器
            driver.quit();
        }
    }
}
