zero 1 年之前
父节点
当前提交
501e8ae93c
共有 1 个文件被更改,包括 206 次插入0 次删除
  1. 206 0
      src/test/java/cn/cslg/pas/novelty/GetReportInfoFromWDTest.java

+ 206 - 0
src/test/java/cn/cslg/pas/novelty/GetReportInfoFromWDTest.java

@@ -0,0 +1,206 @@
+package cn.cslg.pas.novelty;
+
+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.boot.test.context.SpringBootTest;
+
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.time.Duration;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.concurrent.TimeUnit;
+
+@SpringBootTest
+public class GetReportInfoFromWDTest {
+
+    public static Map<String, String> dynamicCaseInfo(String caseNo) {
+        Properties properties = new Properties();
+        try {
+            properties.load(new FileInputStream("path/to/your/config.properties"));
+        } catch (IOException e) {
+            e.printStackTrace();
+            throw new RuntimeException("Failed to load configuration properties");
+        }
+
+        String strFileSavePath = properties.getProperty("IPEasySetting.DownloadFileSavePath");
+        boolean isHeadless = Boolean.parseBoolean(properties.getProperty("IPEasySetting.isHeadless"));
+        String account = properties.getProperty("IPEasySetting.Account");
+        String password = properties.getProperty("IPEasySetting.Password");
+        String chromeDriverPath = properties.getProperty("IPEasySetting.ChromeDriverPath");
+        String iPEasyWeb = properties.getProperty("IPEasySetting.IPEasyWeb");
+
+        ChromeOptions options = new ChromeOptions();
+        options.addArguments("user-data-dir=" + strFileSavePath);
+        options.addArguments("--lang=nl");
+        options.addArguments("--disable-popup-blocking");
+        options.addArguments("--disable-images");
+
+        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.sendKeys(account);
+            pwdField.sendKeys(password);
+            //点击登录按钮
+            loginButton.click();
+
+            //关闭提示遮罩层
+            WebElement closeOverlay = driver.findElement(By.id("jpwClose"));
+            if (closeOverlay.isDisplayed()) {
+                closeOverlay.click();
+            }
+
+            //点击顶部菜单栏中的案件管理菜单
+            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 = driver.findElement(By.xpath("//a[contains(text()," + caseNo + ")]"));
+            caseLink.click();
+
+            driver.switchTo().parentFrame().switchTo().frame(2);
+
+            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();
+//        }
+//    }
+
+}