private async void btn_Login_Selenium_Click(object sender, RoutedEventArgs e) { var userName = this.tbox_UserName_Selenium.Text.Trim(); var password = this.tbox_Password_Selenium.Text.Trim(); if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password)) { EMessageBox.Show("请输入用户名或密码"); return; } using (OpenQA.Selenium.IWebDriver driver = new OpenQA.Selenium.Edge.EdgeDriver()) { driver.Navigate().GoToUrl("http://i.360.cn"); //driver.Url = "http://i.360.cn"是一样的 var source = driver.PageSource; this.rtbox_BeforeLoginContent_Selenium.Document = new FlowDocument(new Paragraph(new Run(source))); //这个等待是无效的,只是测试代码,可以直接启用下载获取username的代码 QA.Support.UI.WebDriverWait wait = new QA.Support.UI.WebDriverWait(driver, TimeSpan.FromSeconds(2)); QA.IWebElement userNameEle = wait.Until <QA.IWebElement>(d => d.FindElement(QA.By.Name("userName"))); //QA.IWebElement userNameEle = driver.FindElement(QA.By.Name("userName")); QA.IWebElement passwordEle = driver.FindElement(QA.By.Name("password")); QA.IWebElement loginEle = driver.FindElement(QA.By.ClassName("quc-button-submit quc-button quc-button-primary")); //填写用户名 userNameEle.SendKeys(QA.Keys.Tab); userNameEle.Clear(); userNameEle.SendKeys(userName); //主动等待2秒 driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(2); //填写密码 passwordEle.SendKeys(QA.Keys.Tab); passwordEle.Clear(); passwordEle.SendKeys(password); driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(2); //点击登录按钮 loginEle.Click(); //主动等待5秒 //使用driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(2);这种方式等待无效 //登录需要时间,如果直接去获取Cookie,获取的还是未登录前的Cookie System.Threading.Thread.Sleep(5000); //Cookies var cookies = driver.Manage().Cookies.AllCookies; var cookieContainer = SeleniumUtil.CookieConvert(cookies); var html = await WebUtil.GetHtmlSource("http://i.360.cn", cookieContainer : cookieContainer); this.rtbox_AfterLoginContent_Selenium.Document = new FlowDocument(new Paragraph(new Run(html.Item1))); } }
public void Input(UI.UIInputAction action) { try { action.Status = TestResultType.Fail; OpenQA.Selenium.IWebElement element = SeleniumUIHelper.GetElement(this.Driver, SeleniumUIHelper.GetBy(action.Element)); element.Clear(); element.SendKeys(action.Value); action.Status = TestResultType.Success; } catch (System.Exception ex) { action.Status = TestResultType.Fail; string fileName = string.Format("{0}_{1}_{2}.png", action.TestCaseAction.TestCase.TestCaseRef.Key, action.TestCaseAction.ActionRef.Action, System.DateTime.Now.ToString("yyyyMMddHHmmssms")); SeleniumScreenshotHelper.CreateScreenshot(this.Driver, this.TestRunner.Environment, fileName); action.Error = new ValidationException("Common.ElementCanNotFound", action.Element, fileName); } }
/// <summary>Clears the text if it’s a text entry element.</summary> public WebElement clear() { _webElement.Clear(); return(this); }