123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- package cn.cslg.pas.novelty;
- import cn.cslg.pas.common.vo.GetReportInfoFromWDVO;
- import cn.cslg.pas.exception.XiaoShiException;
- import cn.cslg.pas.service.permissions.GetReportInfoFromWDService;
- import com.alibaba.fastjson2.JSONObject;
- import org.junit.jupiter.api.Test;
- 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.chrome.ChromeOptions;
- import org.openqa.selenium.support.ui.ExpectedCondition;
- import org.openqa.selenium.support.ui.WebDriverWait;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
- import java.io.IOException;
- import java.time.Duration;
- import java.util.*;
- import java.util.concurrent.TimeUnit;
- @SpringBootTest
- public class GetReportInfoFromWDTest {
- private static String strFileSavePath = "F:\\file";
- private static boolean isHeadless = true;
- private static String account = "caiyangl";
- private static String password = "j)wx*lier*@3";
- private static String chromeDriverPath = "E:\\test\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe";
- private static String iPEasyWeb = "http://47.106.94.35/index.aspx";
- @Autowired
- private GetReportInfoFromWDService getReportInfoFromWDService;
- @Test
- public void test11() throws IOException {
- final String res = getReportInfoFromWDService.getReportInfoFromWD("PAUS2417955");
- System.out.println(res);
- final GetReportInfoFromWDVO wdvo = JSONObject.parseObject(res, GetReportInfoFromWDVO.class);
- System.out.println(wdvo);
- // String token = getReportInfoFromWDService.getTokenByWDLogin();
- // System.out.println(token);
- }
- @Test
- public void test() {
- Map<String, String> map = dynamicCaseInfo("5");
- // Map<String, String> map = dynamicCaseInfo("P");
- System.out.println(map);
- }
- public Map<String, String> dynamicCaseInfo(String caseNo) {
- ChromeOptions options = new ChromeOptions();
- options.addArguments("user-data-dir=" + strFileSavePath);
- options.addArguments("--lang=nl");
- options.addArguments("--disable-popup-blocking");
- options.addArguments("--disable-images");
- options.addArguments("--remote-allow-origins=*");
- if (isHeadless) {
- // options.addArguments("headless");
- }
- WebDriver driver = null;
- Map<String, String> retObject = new HashMap<>();
- try {
- System.setProperty("webdriver.chrome.driver", chromeDriverPath);
- driver = new ChromeDriver(options);
- driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
- driver.manage().timeouts().pageLoadTimeout(100, TimeUnit.SECONDS);
- driver.get(iPEasyWeb);
- //进入登录界面
- WebElement userField = driver.findElement(By.id("txtUser"));
- WebElement pwdField = driver.findElement(By.id("txtPwd"));
- WebElement loginButton = driver.findElement(By.id("btnLogin"));
- //输入用户名和密码
- userField.clear();
- userField.sendKeys(account);
- pwdField.clear();
- pwdField.sendKeys(password);
- //点击登录按钮
- loginButton.click();
- //关闭提示遮罩层
- WebElement closeOverlay = null;
- try {
- closeOverlay = driver.findElement(By.id("jpwClose"));
- if (closeOverlay.isDisplayed()) {
- closeOverlay.click();
- }
- } catch (Exception e) {
- }
- //点击顶部菜单栏中的案件管理菜单
- WebElement caseManagementMenu = driver.findElement(By.name("71A7CC35-F597-40E1-9FEF-BE622A3A3B63"));
- caseManagementMenu.click();
- //点击左侧 查询 菜单
- WebElement searchMenu = driver.findElement(By.name("c3266ab3-521a-4815-8aaf-7dd0bc5a76af"));
- searchMenu.click();
- //切换到自定义报表Frame
- driver.switchTo().frame(1);
- WebElement inputSearch = driver.findElement(By.id("case_volume"));
- inputSearch.sendKeys(caseNo.trim());
- WebElement btnSearch = driver.findElement(By.id("btn_Search"));
- btnSearch.click();
- WebElement caseLink = null;
- try {
- caseLink = driver.findElement(By.xpath("//a[contains(text()," + caseNo + ")]"));
- } catch (Exception e) {
- throw new XiaoShiException("未查询到案件基本信息");
- }
- caseLink.click();
- driver.switchTo().parentFrame().switchTo().frame(2);
- // IPCE8AE7FA-6F04-4860-B515-0F350F1A4CFB
- // WebElement libase = driver.findElement(By.id("libase"));
- // libase.click();
- WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(20));
- //等待页面上ID属性值为submitButton的元素加载完成
- WebElement myElement = wait.until((ExpectedCondition<WebElement>) wd -> {
- try {
- if (wd != null) {
- wd.switchTo().parentFrame().switchTo().frame(2);
- return wd.findElement(By.id("libase"));
- }
- } catch (Exception e) {
- return null;
- }
- return null;
- });
- myElement.click();
- // 案件号
- retObject.put("CaseNo", caseNo.trim());
- //案件名称
- retObject.put("CaseName", driver.findElement(By.id("p_case_info__case_name")).getAttribute("value"));
- //客户名称
- retObject.put("CustomerName", driver.findElement(By.id("p_case_info__customer_id")).getAttribute("value"));
- //业务类型
- retObject.put("BusinessType", driver.findElement(By.id("p_case_info__business_type_id")).getAttribute("value"));
- retObject.put("CaseState", getSelectText(driver.findElement(By.id("p_case_info__case_status_id"))));
- retObject.put("ApplicationType", getSelectText(driver.findElement(By.id("p_case_info__apply_type_id"))));
- //案件类型
- retObject.put("CaseType", driver.findElement(By.id("p_case_info__case_type_id")).getAttribute("value"));
- //委案日期
- retObject.put("EntrustingDate", driver.findElement(By.id("p_case_info__charge_date")).getAttribute("value"));
- //案件备注
- retObject.put("CaseMemo", driver.findElement(By.id("p_case_info__remark")).getAttribute("value"));
- WebElement expandInfoButton = driver.findElement(By.xpath("//span[contains(text(),'扩展信息')]"));
- expandInfoButton.click();
- retObject.put("CaseCoefficient", getSelectText(driver.findElement(By.id("p_case_info__case_coefficient_id"))));
- } catch (Exception e) {
- e.printStackTrace();
- throw new RuntimeException("An error occurred while retrieving case info", e);
- } finally {
- if (driver != null) {
- driver.quit();
- // killChromProcess() equivalent in Java can be achieved by ensuring the driver process is killed
- // This can be platform-specific and might not be necessary if driver.quit() is sufficient
- }
- }
- return retObject;
- }
- public static String getSelectText(WebElement element) {
- String strValue = element.getAttribute("value");
- if (strValue == null || strValue.isEmpty()) {
- return null;
- }
- List<WebElement> ops = element.findElements(By.tagName("option"));
- for (WebElement op : ops) {
- if (op.getAttribute("value").equals(strValue)) {
- return op.getText();
- }
- }
- return null;
- }
- //
- // public static void killChromeProcesses() {
- // try {
- // // 获取所有名为 "chrome" 的进程
- // List<ProcessHandle> chromeProcesses = ProcessHandle.allProcesses()
- // .filter(ph -> ph.info().command().orElse("").toLowerCase().contains("chrome"))
- // .collect(Collectors.toList());
- //
- // for (ProcessHandle chromeProcess : chromeProcesses) {
- // try {
- // chromeProcess.destroy(); // 尝试终止进程
- // chromeProcess.waitFor(10, TimeUnit.SECONDS); // 等待进程退出,超时为10秒
- // } catch (InterruptedException | TimeoutException e) {
- // // 处理异常,例如进程没有在指定时间内退出
- // e.printStackTrace();
- // }
- // }
- //
- // // 获取所有名为 "chromedriver" 的进程
- // List<ProcessHandle> chromedriverProcesses = ProcessHandle.allProcesses()
- // .filter(ph -> ph.info().command().orElse("").toLowerCase().contains("chromedriver"))
- // .collect(Collectors.toList());
- //
- // for (ProcessHandle chromedriverProcess : chromedriverProcesses) {
- // try {
- // chromedriverProcess.destroy(); // 尝试终止进程
- // chromedriverProcess.waitFor(10, TimeUnit.SECONDS); // 等待进程退出,超时为10秒
- // } catch (InterruptedException | TimeoutException e) {
- // // 处理异常,例如进程没有在指定时间内退出
- // e.printStackTrace();
- // }
- // }
- // } catch (IOException e) {
- // // 处理获取进程列表时可能发生的异常
- // e.printStackTrace();
- // }
- // }
- }
|