GetReportInfoFromWDTest.java 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package cn.cslg.pas.novelty;
  2. import org.openqa.selenium.By;
  3. import org.openqa.selenium.WebDriver;
  4. import org.openqa.selenium.WebElement;
  5. import org.openqa.selenium.chrome.ChromeDriver;
  6. import org.openqa.selenium.chrome.ChromeOptions;
  7. import org.openqa.selenium.support.ui.ExpectedCondition;
  8. import org.openqa.selenium.support.ui.WebDriverWait;
  9. import org.springframework.boot.test.context.SpringBootTest;
  10. import java.io.FileInputStream;
  11. import java.io.IOException;
  12. import java.time.Duration;
  13. import java.util.HashMap;
  14. import java.util.List;
  15. import java.util.Map;
  16. import java.util.Properties;
  17. import java.util.concurrent.TimeUnit;
  18. @SpringBootTest
  19. public class GetReportInfoFromWDTest {
  20. public static Map<String, String> dynamicCaseInfo(String caseNo) {
  21. Properties properties = new Properties();
  22. try {
  23. properties.load(new FileInputStream("path/to/your/config.properties"));
  24. } catch (IOException e) {
  25. e.printStackTrace();
  26. throw new RuntimeException("Failed to load configuration properties");
  27. }
  28. String strFileSavePath = properties.getProperty("IPEasySetting.DownloadFileSavePath");
  29. boolean isHeadless = Boolean.parseBoolean(properties.getProperty("IPEasySetting.isHeadless"));
  30. String account = properties.getProperty("IPEasySetting.Account");
  31. String password = properties.getProperty("IPEasySetting.Password");
  32. String chromeDriverPath = properties.getProperty("IPEasySetting.ChromeDriverPath");
  33. String iPEasyWeb = properties.getProperty("IPEasySetting.IPEasyWeb");
  34. ChromeOptions options = new ChromeOptions();
  35. options.addArguments("user-data-dir=" + strFileSavePath);
  36. options.addArguments("--lang=nl");
  37. options.addArguments("--disable-popup-blocking");
  38. options.addArguments("--disable-images");
  39. if (isHeadless) {
  40. options.addArguments("headless");
  41. }
  42. WebDriver driver = null;
  43. Map<String, String> retObject = new HashMap<>();
  44. try {
  45. System.setProperty("webdriver.chrome.driver", chromeDriverPath);
  46. driver = new ChromeDriver(options);
  47. driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
  48. driver.manage().timeouts().pageLoadTimeout(100, TimeUnit.SECONDS);
  49. driver.get(iPEasyWeb);
  50. //进入登录界面
  51. WebElement userField = driver.findElement(By.id("txtUser"));
  52. WebElement pwdField = driver.findElement(By.id("txtPwd"));
  53. WebElement loginButton = driver.findElement(By.id("btnLogin"));
  54. //输入用户名和密码
  55. userField.sendKeys(account);
  56. pwdField.sendKeys(password);
  57. //点击登录按钮
  58. loginButton.click();
  59. //关闭提示遮罩层
  60. WebElement closeOverlay = driver.findElement(By.id("jpwClose"));
  61. if (closeOverlay.isDisplayed()) {
  62. closeOverlay.click();
  63. }
  64. //点击顶部菜单栏中的案件管理菜单
  65. WebElement caseManagementMenu = driver.findElement(By.name("71A7CC35-F597-40E1-9FEF-BE622A3A3B63"));
  66. caseManagementMenu.click();
  67. //点击左侧 查询 菜单
  68. WebElement searchMenu = driver.findElement(By.name("c3266ab3-521a-4815-8aaf-7dd0bc5a76af"));
  69. searchMenu.click();
  70. //切换到自定义报表Frame
  71. driver.switchTo().frame(1);
  72. WebElement inputSearch = driver.findElement(By.id("case_volume"));
  73. inputSearch.sendKeys(caseNo.trim());
  74. WebElement btnSearch = driver.findElement(By.id("btn_Search"));
  75. btnSearch.click();
  76. WebElement caseLink = driver.findElement(By.xpath("//a[contains(text()," + caseNo + ")]"));
  77. caseLink.click();
  78. driver.switchTo().parentFrame().switchTo().frame(2);
  79. WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(20));
  80. //等待页面上ID属性值为submitButton的元素加载完成
  81. WebElement myElement = wait.until((ExpectedCondition<WebElement>) wd -> {
  82. try {
  83. if (wd != null) {
  84. wd.switchTo().parentFrame().switchTo().frame(2);
  85. return wd.findElement(By.id("libase"));
  86. }
  87. } catch (Exception e) {
  88. return null;
  89. }
  90. return null;
  91. });
  92. myElement.click();
  93. // 案件号
  94. retObject.put("CaseNo", caseNo.trim());
  95. //案件名称
  96. retObject.put("CaseName", driver.findElement(By.id("p_case_info__case_name")).getAttribute("value"));
  97. //客户名称
  98. retObject.put("CustomerName", driver.findElement(By.id("p_case_info__customer_id")).getAttribute("value"));
  99. //业务类型
  100. retObject.put("BusinessType", driver.findElement(By.id("p_case_info__business_type_id")).getAttribute("value"));
  101. retObject.put("CaseState", getSelectText(driver.findElement(By.id("p_case_info__case_status_id"))));
  102. retObject.put("ApplicationType", getSelectText(driver.findElement(By.id("p_case_info__apply_type_id"))));
  103. //案件类型
  104. retObject.put("CaseType", driver.findElement(By.id("p_case_info__case_type_id")).getAttribute("value"));
  105. //委案日期
  106. retObject.put("EntrustingDate", driver.findElement(By.id("p_case_info__charge_date")).getAttribute("value"));
  107. //案件备注
  108. retObject.put("CaseMemo", driver.findElement(By.id("p_case_info__remark")).getAttribute("value"));
  109. WebElement expandInfoButton = driver.findElement(By.xpath("//span[contains(text(),'扩展信息')]"));
  110. expandInfoButton.click();
  111. retObject.put("CaseCoefficient", getSelectText(driver.findElement(By.id("p_case_info__case_coefficient_id"))));
  112. } catch (Exception e) {
  113. e.printStackTrace();
  114. throw new RuntimeException("An error occurred while retrieving case info", e);
  115. } finally {
  116. if (driver != null) {
  117. driver.quit();
  118. // killChromProcess() equivalent in Java can be achieved by ensuring the driver process is killed
  119. // This can be platform-specific and might not be necessary if driver.quit() is sufficient
  120. }
  121. }
  122. return retObject;
  123. }
  124. public static String getSelectText(WebElement element) {
  125. String strValue = element.getAttribute("value");
  126. if (strValue == null || strValue.isEmpty()) {
  127. return null;
  128. }
  129. List<WebElement> ops = element.findElements(By.tagName("option"));
  130. for (WebElement op : ops) {
  131. if (op.getAttribute("value").equals(strValue)) {
  132. return op.getText();
  133. }
  134. }
  135. return null;
  136. }
  137. //
  138. // public static void killChromeProcesses() {
  139. // try {
  140. // // 获取所有名为 "chrome" 的进程
  141. // List<ProcessHandle> chromeProcesses = ProcessHandle.allProcesses()
  142. // .filter(ph -> ph.info().command().orElse("").toLowerCase().contains("chrome"))
  143. // .collect(Collectors.toList());
  144. //
  145. // for (ProcessHandle chromeProcess : chromeProcesses) {
  146. // try {
  147. // chromeProcess.destroy(); // 尝试终止进程
  148. // chromeProcess.waitFor(10, TimeUnit.SECONDS); // 等待进程退出,超时为10秒
  149. // } catch (InterruptedException | TimeoutException e) {
  150. // // 处理异常,例如进程没有在指定时间内退出
  151. // e.printStackTrace();
  152. // }
  153. // }
  154. //
  155. // // 获取所有名为 "chromedriver" 的进程
  156. // List<ProcessHandle> chromedriverProcesses = ProcessHandle.allProcesses()
  157. // .filter(ph -> ph.info().command().orElse("").toLowerCase().contains("chromedriver"))
  158. // .collect(Collectors.toList());
  159. //
  160. // for (ProcessHandle chromedriverProcess : chromedriverProcesses) {
  161. // try {
  162. // chromedriverProcess.destroy(); // 尝试终止进程
  163. // chromedriverProcess.waitFor(10, TimeUnit.SECONDS); // 等待进程退出,超时为10秒
  164. // } catch (InterruptedException | TimeoutException e) {
  165. // // 处理异常,例如进程没有在指定时间内退出
  166. // e.printStackTrace();
  167. // }
  168. // }
  169. // } catch (IOException e) {
  170. // // 处理获取进程列表时可能发生的异常
  171. // e.printStackTrace();
  172. // }
  173. // }
  174. }