using System;
using System.Linq;
using OpenQA.Selenium;
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();
options.AddUserProfilePreference("download.default_directory", strFileSavePath);
options.AddUserProfilePreference("intl.accept_languages", "nl");
options.AddUserProfilePreference("disable-popup-blocking", "true");
if (isheadless)
{
options.AddArgument("headless");
}
using (IWebDriver driver = new OpenQA.Selenium.Chrome.ChromeDriver(ConfigHelper.GetSectionValue("IPEasySetting:ChormeDriverPath"), options))
{
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(50);
driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(500);
//进入登录界面
driver.Navigate().GoToUrl("http://47.106.221.167/Login.aspx");
//输入用户名和密码
driver.FindElement(By.Id("txtUser")).SendKeys(Account);
driver.FindElement(By.Id("txtPwd")).SendKeys(Password);
//点击登录按钮
driver.FindElement(By.Id("btnLogin")).Click();
//关闭提示遮罩层
driver.FindElement(By.Id("jpwClose")).Click();
//点击顶部菜单栏中的报表管理菜单
driver.FindElement(By.Name("970d33d5-c728-41b8-a060-4330610706b9")).Click();
//点击左侧 自定义报表 菜单
driver.FindElement(By.Name("642fa96f-1e1f-46fd-aaa4-cb461ee8df5b")).Click();
//切换到自定义报表Frame
driver.SwitchTo().Frame(1);
//调用报表导出JS
((IJavaScriptExecutor)driver).ExecuteScript($"Report.Export('{strId}');");
//切换到弹出的导出报表窗口,点击导出按钮
driver.SwitchTo().DefaultContent();
var ihg_export = driver.FindElement(By.Name("ihg_export"));
driver.SwitchTo().Frame(ihg_export);
driver.FindElement(By.Id("btnSubmit")).Click();
string strFilePath = System.IO.Path.Combine(strFileSavePath, filename);
while (true)
{
System.IO.FileInfo file = new System.IO.FileInfo(strFilePath);
if (!file.Exists || file.Length == 0) {
System.Threading.Thread.Sleep(5000);
}
else
{
break;
}
}
}
}
///
/// 根据报表名称导出报表
///
/// 报告名称
/// 是否手动修改日期范围
public static void DownloadReport(string ReportName,bool isModifyDate)
{
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");
if (isheadless)
{
options.AddArgument("headless");
}
using (IWebDriver driver = new OpenQA.Selenium.Chrome.ChromeDriver(ConfigHelper.GetSectionValue("IPEasySetting:ChormeDriverPath"), options))
{
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(50);
driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(500);
//进入登录界面
driver.Navigate().GoToUrl("http://47.106.221.167/Login.aspx");
//输入用户名和密码
driver.FindElement(By.Id("txtUser")).SendKeys(Account);
driver.FindElement(By.Id("txtPwd")).SendKeys(Password);
//点击登录按钮
driver.FindElement(By.Id("btnLogin")).Click();
//关闭提示遮罩层
driver.FindElement(By.Id("jpwClose")).Click();
//点击顶部菜单栏中的报表管理菜单
driver.FindElement(By.Name("970d33d5-c728-41b8-a060-4330610706b9")).Click();
//点击左侧 自定义报表 菜单
driver.FindElement(By.Name("642fa96f-1e1f-46fd-aaa4-cb461ee8df5b")).Click();
//切换到自定义报表Frame
driver.SwitchTo().Frame(1);
var inputSearch = driver.FindElement(By.Id("customizedList_TxtSheetSearchKey"));
inputSearch.SendKeys(ReportName);
var btnSearch = driver.FindElement(By.ClassName("btn-search"));
btnSearch.Click();
var reportRecord = driver.FindElement(By.XPath($"//td[contains(text(),'{ReportName}')]"));
reportRecord.Click();
System.Threading.Thread.Sleep(1000);
if (isModifyDate)
{
var btnEdit = driver.FindElement(By.ClassName("edit"));
btnEdit.Click();
System.Threading.Thread.Sleep(500);
var doItemFinished = driver.FindElement(By.XPath("//span[contains(@objcvalue,'_date') and contains(@id,'ST;')]"));
doItemFinished.Click();
System.Threading.Thread.Sleep(500);
var startDate = driver.FindElement(By.XPath("//input[contains(@id,'dt_s_')]"));
startDate.Click();
System.Threading.Thread.Sleep(500);
startDate.SendKeys($"{DateTime.Now.AddMonths(-1).Year }-{DateTime.Now.AddMonths(-1).Month}-01");
var endDate = driver.FindElement(By.XPath("//input[contains(@id,'dt_e_')]"));//.Id("dt_e_4F8FE88D-9040-45F1-9723-45699BCD4CAF"));
endDate.Click();
System.Threading.Thread.Sleep(500);
endDate.SendKeys(new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddDays(-1).ToString("yyyy-MM-dd"));
var addDate = driver.FindElement(By.XPath("//a[contains(@onclick,'ReportSub.ConditionDTSelect')]"));
addDate.Click();
System.Threading.Thread.Sleep(500);
var btnSave = driver.FindElement(By.Id("save"));
btnSave.Click();
System.Threading.Thread.Sleep(3000);
}
var linkExport = driver.FindElement(By.XPath("//a[contains(@onclick,'-') and @class='tbexcel']"));
linkExport.Click();
//切换到弹出的导出报表窗口,点击导出按钮
driver.SwitchTo().DefaultContent();
var ihg_export = driver.FindElement(By.Name("ihg_export"));
driver.SwitchTo().Frame(ihg_export);
driver.FindElement(By.Id("btnSubmit")).Click();
string strFilePath = System.IO.Path.Combine(strFileSavePath, $"{ReportName.Trim()}.xlsx");
while (true)
{
System.IO.FileInfo file = new System.IO.FileInfo(strFilePath);
if (!file.Exists || file.Length == 0)
{
System.Threading.Thread.Sleep(5000);
}
else
{
break;
}
}
}
}
}
}