GetReportInfoFromWDTest.java 9.7 KB

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