소스 검색

重构创建ChromeDriver代码为统一的方法

luocaiyang 11 달 전
부모
커밋
4ce6e6b83b
1개의 변경된 파일43개의 추가작업 그리고 116개의 파일을 삭제
  1. 43 116
      wispro.sp.utility/IPEasyUtility.cs

+ 43 - 116
wispro.sp.utility/IPEasyUtility.cs

@@ -16,25 +16,15 @@ namespace wispro.sp.utility
 {
     public class IPEasyUtility
     {
-        public static void DownloadReport(string strId,string filename)
-        {
-            string strFileSavePath = ConfigHelper.GetSectionValue("IPEasySetting:DownloadFileSavePath");
-            bool isheadless = (ConfigHelper.GetSectionValue("IPEasySetting:isHeadless") == "true");
-            string Account = ConfigHelper.GetSectionValue("IPEasySetting:Account");
-            string Password = ConfigHelper.GetSectionValue("IPEasySetting:Password");
-
-            OpenQA.Selenium.Chrome.ChromeOptions options = new OpenQA.Selenium.Chrome.ChromeOptions();
+        static string strFileSavePath = ConfigHelper.GetSectionValue("IPEasySetting:DownloadFileSavePath");
+        static string Account = ConfigHelper.GetSectionValue("IPEasySetting:Account");
+        static string Password = ConfigHelper.GetSectionValue("IPEasySetting:Password");
 
 
-            options.AddUserProfilePreference("download.default_directory", strFileSavePath);
-            options.AddUserProfilePreference("intl.accept_languages", "nl");
-            options.AddUserProfilePreference("disable-popup-blocking", "true");
-            if (isheadless)
-            {
-                options.AddArgument("headless");
-            }
+        public static void DownloadReport(string strId,string filename)
+        {
 
-            using (IWebDriver driver = new OpenQA.Selenium.Chrome.ChromeDriver(ConfigHelper.GetSectionValue("IPEasySetting:ChormeDriverPath"), options))
+            using (IWebDriver driver = CreateChromeDriver())
             {
                 try
                 {
@@ -45,6 +35,7 @@ namespace wispro.sp.utility
                     driver.Navigate().GoToUrl(ConfigHelper.GetSectionValue("IPEasySetting:IPEasyWeb"));
 
                     //输入用户名和密码
+                    
                     driver.FindElement(By.Id("txtUser")).SendKeys(Account);
                     driver.FindElement(By.Id("txtPwd")).SendKeys(Password);
 
@@ -112,34 +103,36 @@ namespace wispro.sp.utility
             sw.Dispose();
         }
 
-        /// <summary>
-        /// 根据报表名称导出报表
-        /// </summary>
-        /// <param name="ReportName">报告名称</param>
-        /// <param name="isModifyDate">是否手动修改日期范围</param>
-        public static DataTable DownloadReport(string ReportName,bool isModifyDate)
+        private static OpenQA.Selenium.Chrome.ChromeDriver CreateChromeDriver()
         {
-            Log($"{DateTime.Now}\t开始下载:{ReportName}");
-            DataTable retDatatable;
-            string strFileSavePath = ConfigHelper.GetSectionValue("IPEasySetting:DownloadFileSavePath");
-            bool isheadless =  (ConfigHelper.GetSectionValue("IPEasySetting:isHeadless") == "true");
-            string Account = ConfigHelper.GetSectionValue("IPEasySetting:Account");
-            string Password = ConfigHelper.GetSectionValue("IPEasySetting:Password");
-
-            OpenQA.Selenium.Chrome.ChromeOptions options = new OpenQA.Selenium.Chrome.ChromeOptions();
-
+            bool isheadless = (ConfigHelper.GetSectionValue("IPEasySetting:isHeadless") == "true");
             
+            var options = new OpenQA.Selenium.Chrome.ChromeOptions();
+
             options.AddUserProfilePreference("download.default_directory", strFileSavePath);
             options.AddUserProfilePreference("intl.accept_languages", "nl");
             options.AddUserProfilePreference("disable-popup-blocking", "true");
             options.AddUserProfilePreference("safebrowsing.enabled", true);
-            options.AddArgument($"--unsafely-treat-insecure-origin-as-secure={ConfigHelper.GetSectionValue("IPEasySetting:IPEasyWeb").Replace("/Login.aspx","")}");//替换自己的域名,多域名可用逗号分隔。
+            options.AddArgument($"--unsafely-treat-insecure-origin-as-secure={ConfigHelper.GetSectionValue("IPEasySetting:IPEasyWeb").Replace("/Login.aspx", "")}");//替换自己的域名,多域名可用逗号分隔。
             if (isheadless)
             {
                 options.AddArgument("headless");
             }
+
+            return new OpenQA.Selenium.Chrome.ChromeDriver(ConfigHelper.GetSectionValue("IPEasySetting:ChormeDriverPath"), options);
+        }
+
+        /// <summary>
+        /// 根据报表名称导出报表
+        /// </summary>
+        /// <param name="ReportName">报告名称</param>
+        /// <param name="isModifyDate">是否手动修改日期范围</param>
+        public static DataTable DownloadReport(string ReportName,bool isModifyDate)
+        {
+            Log($"{DateTime.Now}\t开始下载:{ReportName}");
+            DataTable retDatatable;
             Log($"{DateTime.Now}\t开始启动Chrome");
-            using (IWebDriver driver = new OpenQA.Selenium.Chrome.ChromeDriver(ConfigHelper.GetSectionValue("IPEasySetting:ChormeDriverPath"), options))
+            using (IWebDriver driver = CreateChromeDriver())
             {
                 try
                 {
@@ -158,6 +151,7 @@ namespace wispro.sp.utility
                     driver.Navigate().GoToUrl(ConfigHelper.GetSectionValue("IPEasySetting:IPEasyWeb"));
 
                     //输入用户名和密码
+                    
                     driver.FindElement(By.Id("txtUser")).SendKeys(Account);
                     driver.FindElement(By.Id("txtPwd")).SendKeys(Password);
 
@@ -178,7 +172,7 @@ namespace wispro.sp.utility
                     Log($"{DateTime.Now}\t点击左侧 自定义报表 菜单");
                     var customerReportMenu = driver.FindElement(By.Name("642fa96f-1e1f-46fd-aaa4-cb461ee8df5b"));
                     driver.ExecuteJavaScript("arguments[0].click();", customerReportMenu);
-                    
+
                     //切换到自定义报表Frame
                     Log($"{DateTime.Now}\t切换到自定义报表Frame");
                     driver.SwitchTo().Frame(1);
@@ -280,7 +274,7 @@ namespace wispro.sp.utility
                     string strStatus = tdStatus.Text;
                     while (strStatus.Trim() != "导出成功!")
                     {
-                        if(strStatus.Trim() == "导出失败,请稍后重试!")
+                        if (strStatus.Trim() == "导出失败,请稍后重试!")
                         {
                             return null;
                         }
@@ -296,13 +290,13 @@ namespace wispro.sp.utility
                     firstTr.FindElement(By.XPath("//td/a[@title='下载']")).Click();
                     //System.Threading.Thread.Sleep(5000);
                     //btnDownload.Click();
-
-                    string strFilePath = System.IO.Path.Combine(strFileSavePath, $"{ReportName.Trim()}.xlsx");
                     
+                    string strFilePath = System.IO.Path.Combine(strFileSavePath, $"{ReportName.Trim()}.xlsx");
+
                     int iwaiting = 0;
                     while (true)
                     {
-                        
+
                         System.IO.FileInfo file = new System.IO.FileInfo(strFilePath.Replace("~", "_"));
                         if (!file.Exists || file.Length == 0)
                         {
@@ -327,7 +321,7 @@ namespace wispro.sp.utility
                     firstTr.FindElement(By.XPath("//td/a[@title='删除']")).Click();
 
                     //System.IO.FileInfo file = new System.IO.FileInfo(strFilePath.Replace("~", "_"));
-                    retDatatable = NPOIExcel.ExcelToDataTable(strFilePath.Replace("~", "_"),true);
+                    retDatatable = NPOIExcel.ExcelToDataTable(strFilePath.Replace("~", "_"), true);
                     System.IO.File.Delete(strFilePath.Replace("~", "_"));
                 }
                 catch (Exception ex)
@@ -348,6 +342,8 @@ namespace wispro.sp.utility
 
             Log($"{DateTime.Now}\t返回数据");
             return retDatatable;
+
+            
         }
 
         /// <summary>
@@ -357,29 +353,11 @@ namespace wispro.sp.utility
         /// <returns></returns>
         public static dynamic GetCaseInfo(string caseNo)
         {
-            string strFileSavePath = ConfigHelper.GetSectionValue("IPEasySetting:DownloadFileSavePath");
-            bool isheadless = (ConfigHelper.GetSectionValue("IPEasySetting:isHeadless") == "true");
-            string Account = ConfigHelper.GetSectionValue("IPEasySetting:Account");
-            string Password = ConfigHelper.GetSectionValue("IPEasySetting:Password");
-
-            OpenQA.Selenium.Chrome.ChromeOptions options = new OpenQA.Selenium.Chrome.ChromeOptions();
-
-
-            options.AddUserProfilePreference("download.default_directory", strFileSavePath);
-            options.AddUserProfilePreference("intl.accept_languages", "nl");
-            options.AddUserProfilePreference("disable-popup-blocking", "true");
-            options.AddUserProfilePreference("profile", new { default_content_setting_values = new { images = 2 } });
-
-            if (isheadless)
-            {
-                options.AddArgument("headless");
-            }
-
             dynamic retObject = new ExpandoObject();
 
-             retObject.CaseNo = caseNo.Trim();
+            retObject.CaseNo = caseNo.Trim();
 
-            using (IWebDriver driver = new OpenQA.Selenium.Chrome.ChromeDriver(ConfigHelper.GetSectionValue("IPEasySetting:ChormeDriverPath"), options))
+            using (IWebDriver driver = CreateChromeDriver())
             {
                 try
                 {
@@ -390,6 +368,7 @@ namespace wispro.sp.utility
                     driver.Navigate().GoToUrl(ConfigHelper.GetSectionValue("IPEasySetting:IPEasyWeb"));
 
                     //输入用户名和密码
+                    
                     driver.FindElement(By.Id("txtUser")).SendKeys(Account);
                     driver.FindElement(By.Id("txtPwd")).SendKeys(Password);
 
@@ -480,24 +459,6 @@ namespace wispro.sp.utility
 
         public static dynamic GetPerformanceRecord(string caseNo, List<string> doItemNames)
         {
-            string strFileSavePath = ConfigHelper.GetSectionValue("IPEasySetting:DownloadFileSavePath");
-            bool isheadless = (ConfigHelper.GetSectionValue("IPEasySetting:isHeadless") == "true");
-            string Account = ConfigHelper.GetSectionValue("IPEasySetting:Account");
-            string Password = ConfigHelper.GetSectionValue("IPEasySetting:Password");
-
-            OpenQA.Selenium.Chrome.ChromeOptions options = new OpenQA.Selenium.Chrome.ChromeOptions();
-
-
-            options.AddUserProfilePreference("download.default_directory", strFileSavePath);
-            options.AddUserProfilePreference("intl.accept_languages", "nl");
-            options.AddUserProfilePreference("disable-popup-blocking", "true");
-            options.AddUserProfilePreference("profile", new { default_content_setting_values = new { images = 2 } });
-
-            if (isheadless)
-            {
-                options.AddArgument("headless");
-            }
-
             dynamic retObject = new ExpandoObject();
 
             retObject.CaseNo = caseNo.Trim();
@@ -507,7 +468,7 @@ namespace wispro.sp.utility
             {
 
                 Service.Start();
-                using (IWebDriver driver = new OpenQA.Selenium.Chrome.ChromeDriver(Service, options))
+                using (IWebDriver driver = CreateChromeDriver())
                 {
                     try
                     {
@@ -520,6 +481,7 @@ namespace wispro.sp.utility
                         driver.Navigate().GoToUrl(ConfigHelper.GetSectionValue("IPEasySetting:IPEasyWeb"));
 
                         //输入用户名和密码
+                       
                         driver.FindElement(By.Id("txtUser")).SendKeys(Account);
                         driver.FindElement(By.Id("txtPwd")).SendKeys(Password);
 
@@ -730,24 +692,7 @@ namespace wispro.sp.utility
             {
                 return GetAPCNPerformanceRecord(caseNo, doItemName, caseStage);
             }
-            string strFileSavePath = ConfigHelper.GetSectionValue("IPEasySetting:DownloadFileSavePath");
-            bool isheadless = (ConfigHelper.GetSectionValue("IPEasySetting:isHeadless") == "true");
-            string Account = ConfigHelper.GetSectionValue("IPEasySetting:Account");
-            string Password = ConfigHelper.GetSectionValue("IPEasySetting:Password");
-
-            OpenQA.Selenium.Chrome.ChromeOptions options = new OpenQA.Selenium.Chrome.ChromeOptions();
-
-
-            options.AddUserProfilePreference("download.default_directory", strFileSavePath);
-            options.AddUserProfilePreference("intl.accept_languages", "nl");
-            options.AddUserProfilePreference("disable-popup-blocking", "true");
-            options.AddUserProfilePreference("profile", new { default_content_setting_values = new { images = 2 } });
-
-            if (isheadless)
-            {
-                options.AddArgument("headless");
-            }
-
+            
             dynamic retObject = new ExpandoObject();
 
             retObject.CaseNo = caseNo.Trim();
@@ -757,7 +702,7 @@ namespace wispro.sp.utility
             {
 
                 Service.Start();
-                using (IWebDriver driver = new OpenQA.Selenium.Chrome.ChromeDriver(Service, options))
+                using (IWebDriver driver = CreateChromeDriver())
                 {
                     try
                     {
@@ -962,24 +907,6 @@ namespace wispro.sp.utility
 
         private static dynamic GetAPCNPerformanceRecord(string caseNo, string doItemName, string caseStage = null)
         {
-            string strFileSavePath = ConfigHelper.GetSectionValue("IPEasySetting:DownloadFileSavePath");
-            bool isheadless = (ConfigHelper.GetSectionValue("IPEasySetting:isHeadless") == "true");
-            string Account = ConfigHelper.GetSectionValue("IPEasySetting:Account");
-            string Password = ConfigHelper.GetSectionValue("IPEasySetting:Password");
-
-            OpenQA.Selenium.Chrome.ChromeOptions options = new OpenQA.Selenium.Chrome.ChromeOptions();
-
-
-            options.AddUserProfilePreference("download.default_directory", strFileSavePath);
-            options.AddUserProfilePreference("intl.accept_languages", "nl");
-            options.AddUserProfilePreference("disable-popup-blocking", "true");
-            options.AddUserProfilePreference("profile", new { default_content_setting_values = new { images = 2 } });
-
-            if (isheadless)
-            {
-                options.AddArgument("headless");
-            }
-
             dynamic retObject = new ExpandoObject();
 
             retObject.CaseNo = caseNo.Trim();
@@ -989,7 +916,7 @@ namespace wispro.sp.utility
             {
 
                 Service.Start();
-                using (IWebDriver driver = new OpenQA.Selenium.Chrome.ChromeDriver(Service, options))
+                using (IWebDriver driver = CreateChromeDriver())
                 {
                     try
                     {