Пример #1
0
        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)));
            }
        }
Пример #2
0
 /// <summary>Simulates typing into the element.</summary>
 /// <param name="keysOrModifier">Sequence of keys or a modifier key(Control,Shift...) if the sequence is in keysToSendEx</param>
 /// <param name="keys">Optional - Sequence of keys if keysToSend contains modifier key(Control,Shift...)</param>
 /// <example>
 /// To send mobile to an element :
 /// <code lang="vbs">
 ///     driver.findElementsById("id").sendKeys "mobile"
 /// </code>
 /// To send ctrl+a to an element :
 /// <code lang="vbs">
 ///     driver.findElementsById("id").sendKeys Keys.Control, "a"
 /// </code>
 /// </example>
 public void sendKeys(string keysOrModifier, [Optional][DefaultParameterValue("")] string keys)
 {
     if (string.IsNullOrEmpty(keys))
     {
         _webElement.SendKeys(keysOrModifier);
     }
     else
     {
         new OpenQA.Selenium.Interactions.Actions(_webDriver).KeyDown(keysOrModifier).SendKeys(keys).KeyUp(keysOrModifier).Build().Perform();
     }
 }
 /// <summary>
 /// 模拟用户输入指定内容到页面指定输入框
 /// </summary>
 /// <param name="element">输入框页面元素</param>
 /// <param name="text">输入内容</param>
 protected bool SendKeysToElement(QA.IWebElement element, string text)
 {
     try
     {
         _WebDriver.Manage().Timeouts().SetScriptTimeout(TimeSpan.FromSeconds(5));
         _WebDriver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(5));
         element.SendKeys(text);
         return(true);
     }
     catch (Exception ex)
     {
         _SystemLog.writeLog2Console(LOG_LEVEL.ERR, string.Format("Input keywords faild, keywords<{0}>,error message<{1}>.", text, ex.Message));
         return(false);
     }
 }
Пример #4
0
        static void Main(string[] args)
        {
            OpenQA.Selenium.IWebDriver driver = new ChromeDriver();

            driver.Navigate().GoToUrl("http://google.pl");
            //ChromeWebElement element = driver.FindElement(DivideByZeroException.
            //OpenQA.Selenium.IWebDriver element = driver.FindElement

            OpenQA.Selenium.IWebElement elementName = driver.FindElement(by: nameof("q"));

            OpenQA.Selenium.IWebElement element = driver.FindElement(by: class("g"));



            elementName.SendKeys("executeautomation");

            driver.Close();
        }
        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);
            }
        }