public void Login(string Url, string driverPath, string userName, string Password) { //If password is null replace empty value Password = Password != null ? Password : ""; //预上线环境账号 ChromeOptions CROptions = new ChromeOptions(); CROptions.AddArgument("-start-maximized"); CROptions.AddArgument("--disable-gpu"); if (!DataStatus.ProcessModel) { string ChromeXpathpath = Directory.GetCurrentDirectory().ToString() + "\\tools\\ChromeXpath.crx"; CROptions.AddExtension(ChromeXpathpath); } driver = new ChromeDriver(driverPath, CROptions); DataStatus.SetWebDriver(driver); driver.Url = Url; JWait.WaitForAjaxComplete(driver, 30); if (!JWait.WaitUntil(() => { element = driver.FindElement(By.XPath("//input[@placeholder='用户名']")); element.Clear(); element.SendKeys(userName); element = driver.FindElement(By.XPath("//input[@placeholder='密码']")); element.Clear(); element.SendKeys(Password); element = driver.FindElement(By.XPath("//button[contains(text(),'登录')]")); element.Click(); SwitchWebpage(driver.WindowHandles.Count); }, 30)) { throw new Exception("✘:【登录失败】"); } }
public void SwitchWebpage(int Lindex) { if (!JWait.WaitUntil(() => { ReadOnlyCollection <String> windowHandles = driver.WindowHandles; String Index = windowHandles[Lindex - 1]; driver.SwitchTo().Window(Index); }, 10)) { throw new Exception(string.Format("✘:【切换网页】至第{0}标签页失败", Lindex)); } }
public void UploadFile(string filePath) { //Name file path by Developer AutomationElement OpenDialog = null; AutomationElement EditElement = null; AutomationElement SubmitElement = null; //Get opendialog element JWait.WaitUntil(() => { AutomationElement Desktop = AutomationElement.RootElement; Condition OpenDialogConditon = new AndCondition( new PropertyCondition(AutomationElement.NameProperty, "打开"), new PropertyCondition(AutomationElement.LocalizedControlTypeProperty, "对话框") ); OpenDialog = Desktop.FindFirst(TreeScope.Descendants, OpenDialogConditon); if (OpenDialog == null) { throw new Exception(); } }, 30); //Get Edit element an set value to input Condition EditConditon = new AndCondition( new PropertyCondition(AutomationElement.NameProperty, "文件名(N):"), new PropertyCondition(AutomationElement.ClassNameProperty, "Edit"), new PropertyCondition(AutomationElement.IsValuePatternAvailableProperty, true) ); EditElement = OpenDialog.FindFirst(TreeScope.Descendants, EditConditon); ValuePattern EditAction = EditElement.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern; EditAction.SetValue(filePath); //Get submit element and click the button Condition SubmitConditon = new AndCondition( new PropertyCondition(AutomationElement.NameProperty, "打开(O)"), new PropertyCondition(AutomationElement.ClassNameProperty, "Button"), new PropertyCondition(AutomationElement.LocalizedControlTypeProperty, "按钮") ); SubmitElement = OpenDialog.FindFirst(TreeScope.Children, SubmitConditon); InvokePattern SubmitAction = SubmitElement.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern; SubmitAction.Invoke(); }