示例#1
0
        public virtual TPage GetPageValues <TModel>(int propertyShift = 0) where TModel : new()
        {
            var properties = typeof(TModel).GetProperties();

            if (properties.Length < _selector.GetFields.Length)
            {
                throw new SelectorException($"The number of properties ({properties.Length}) in the model '{typeof(TModel).Name}' must be greater than or equal to the number of 'GetFields' selectors: {_selector.GetFields.Length}");
            }

            try
            {
                if (SeleniumUtil.WaitElement(_driver, GetBySelector(_selector.GetFields[0])).Displayed)
                {
                    PageValues = new TModel();

                    for (int i = 0; i < _selector.GetFields.Length; i++)
                    {
                        var element   = _driver.FindElement(GetBySelector(_selector.GetFields[i]));
                        var textValue = (_driver as IJavaScriptExecutor).ExecuteScript($"return arguments[0].value", element) as string;

                        if (textValue is null)
                        {
                            textValue = _driver.FindElement(GetBySelector(_selector.GetFields[i])).Text;
                        }

                        properties[i + propertyShift].SetValue(PageValues, textValue);
                    }
                }
            }
            catch (System.Exception)
            {
                throw;
            }
            return(ChangeType(this, typeof(TPage)) as TPage);
        }
示例#2
0
        public override TestStepResult Do()
        {
            var driver       = (IWebDriver)TestState.GetInstanceWithKey("driver");
            var elementQuery = GetTestData(1);
            var selectorType = GetTestData(2);

            var element = driver.FindElement(SeleniumUtil.GetSelector(selectorType, elementQuery));

            if (null == element)
            {
                return(TestStepResult.Failed($"Could not find Element {elementQuery}"));
            }
            var dsn = element.GetAttribute("data-service-name");

            driver.SwitchTo().Window(driver.CurrentWindowHandle);
            driver.FindElement(By.XPath("//body")).Click();

            if (element.GetAttribute("href") != null)
            {
                element.Click();
                Thread.Sleep(5000);
            }
            else
            {
                element.Click();
            }

            return(TestStepResult.Successful());
        }
示例#3
0
        public virtual TPage GetPageValues(int seconds = 5)
        {
            try
            {
                if (SeleniumUtil.WaitElement(_driver, GetBySelector(_selector.GetFields[0]), seconds).Displayed)
                {
                    PageValues = new List <string>();
                    for (int i = 0; i < _selector.GetFields.Length; i++)
                    {
                        var element   = _driver.FindElement(GetBySelector(_selector.GetFields[i]));
                        var textValue = (_driver as IJavaScriptExecutor).ExecuteScript($"return arguments[0].value", element) as string;

                        if (textValue is null)
                        {
                            textValue = _driver.FindElement(GetBySelector(_selector.GetFields[i])).Text;
                        }

                        PageValues.Add(textValue);
                    }
                }
            }
            catch (System.Exception)
            {
                throw;
            }
            return(ChangeType(this, typeof(TPage)) as TPage);
        }
示例#4
0
        public override TestStepResult Do()
        {
            var elementQuery = GetTestData(1);
            var selectorType = GetTestData(2);

            var elementExists = IsElementPresent(SeleniumUtil.GetSelector(selectorType, elementQuery));

            var expectedResult = GetExpectedResult();

            Log.Info($"Expected result: '{expectedResult}'");

            //Expects that you want it to Pass if no Expected result is submitted
            if (expectedResult == "")
            {
                expectedResult = "Pass";
            }
            if (expectedResult == expectedResultPass)
            {
                if (elementExists)
                {
                    return(TestStepResult.Failed($"Could not find Element {elementQuery}"));
                }
            }
            else
            {
                if (!elementExists)
                {
                    return(TestStepResult.Failed($"Could find Element {elementQuery} but it was not expected"));
                }
            }

            return(TestStepResult.Successful());
        }
示例#5
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)));
            }
        }
        public override TestStepResult Do()
        {
            var driver = (IWebDriver)TestState.GetInstanceWithKey("driver");

            var text         = GetTestData(1);
            var elementQuery = GetTestData(2);
            var selectorType = GetTestData(3);

            var select = new SelectElement(driver.FindElement(SeleniumUtil.GetSelector(selectorType, elementQuery)));

            select.SelectByText(text);

            return(TestStepResult.Successful());
        }
示例#7
0
 public virtual bool HasTableResults(int seconds = 5)
 {
     try
     {
         if (SeleniumUtil.WaitElement(_driver, GetBySelector(_selector.Table), seconds).Displayed)
         {
             return(_driver.FindElements(GetBySelector(_selector.Table)).Any());
         }
     }
     catch (System.Exception)
     {
         return(false);
     }
     return(false);
 }
        public bool VerifyAdvanceBuyOrderTab(string instrument, string side, double size, string limitPrice, string buyMarketOrderTime)
        {
            bool flag = false;

            UserSetFunctions.Click(driver.FindElement(openOrderTabButton));
            string buyAmountValue = SeleniumUtil.ConvertToDoubleFormat(size);
            string expectedRow    = instrument + " || " + side + " || " + size + " || " + limitPrice + " || " + buyMarketOrderTime;

            if (GetListOfOpenOrders().Contains(expectedRow))
            {
                output.WriteLine("Matched Expected -> " + expectedRow + " Actual -> ");
                flag = true;
            }
            return(flag);
        }
        public override TestStepResult Do()
        {
            var driver       = (IWebDriver)TestState.GetInstanceWithKey("driver");
            var elementQuery = GetTestData(1);
            var selectorType = GetTestData(2);

            var element = driver.FindElement(SeleniumUtil.GetSelector(selectorType, elementQuery));

            if (null == element)
            {
                return(TestStepResult.Failed($"Could not find Element {elementQuery}"));
            }
            element.Click();

            return(TestStepResult.Successful());
        }
示例#10
0
        public virtual TPage WaitLoad(int seconds = 2)
        {
            if (string.IsNullOrEmpty(_selector.LoadElement))
            {
                throw new SelectorException("The selector for 'LoadElement' property hasn't been configured.");
            }
            try
            {
                while (SeleniumUtil.WaitElement(_driver, GetBySelector(_selector.LoadElement), 1).Displayed)
                {
                    System.Threading.Thread.Sleep(1000 * seconds);
                }
            } catch {}

            return(ChangeType(this, typeof(TPage)) as TPage);
        }
示例#11
0
        public override TestStepResult Do()
        {
            var driver       = (IWebDriver)TestState.GetInstanceWithKey("driver");
            var elementQuery = GetTestData(1);
            var selectorType = GetTestData(2);
            var text         = GetTestData(3);

            var element = driver.FindElement(SeleniumUtil.GetSelector(selectorType, elementQuery));

            if (!element.Text.Contains(text))
            {
                return(TestStepResult.Failed($"Element did not contain the expected text. Expected: '{text}' Got: '{element.Text}'"));
            }

            return(TestStepResult.Successful());
        }
示例#12
0
        public virtual TPage PerformClick(int buttonIndex, int seconds = 5)
        {
            if (buttonIndex >= _selector.PageButtons.Length)
            {
                throw new SelectorException($"The button index ({buttonIndex}) is greater than the number of 'PageButtons' selectors: {_selector.PageButtons.Length}.");
            }

            if (SeleniumUtil.WaitElement(_driver, GetBySelector(_selector.PageButtons[buttonIndex]), seconds).Displayed)
            {
                var element = _driver.FindElement(GetBySelector(_selector.PageButtons[buttonIndex]));

                try{ element.Click(); }
                catch { (_driver as IJavaScriptExecutor).ExecuteScript($@"arguments[0].click()", element); }

                System.Threading.Thread.Sleep(500);
            }
            return(ChangeType(this, typeof(TPage)) as TPage);
        }
示例#13
0
        public virtual TPage SetPageValues(string value, int selectorIndex)
        {
            try
            {
                if (selectorIndex > _selector.SetFields.Length)
                {
                    throw new SelectorException($"The informed selector index ({selectorIndex}) must be less than or equal to the number of 'SetFields' selectors count: {_selector.SetFields.Length}");
                }

                if (SeleniumUtil.WaitElement(_driver, GetBySelector(_selector.SetFields[selectorIndex])).Displayed)
                {
                    HandleInputType(_selector.SetFields[selectorIndex], value);
                }
            }
            catch (System.Exception)
            {
                throw;
            }
            return(ChangeType(this, typeof(TPage)) as TPage);
        }
示例#14
0
        public virtual TPage SetPageValues(int selectOption, int selectorIndex)
        {
            try
            {
                if (selectorIndex > _selector.SetFields.Length)
                {
                    throw new SelectorException($"The informed selector index ({selectorIndex}) must be less than or equal to the number of 'SetFields' selectors count: {_selector.SetFields.Length}");
                }

                if (SeleniumUtil.WaitElement(_driver, GetBySelector(_selector.SetFields[selectorIndex])).Displayed)
                {
                    var element = _driver.FindElement(GetBySelector(_selector.SetFields[selectorIndex]));
                    (_driver as IJavaScriptExecutor).ExecuteScript($@"arguments[0].selectedIndex={selectOption}", element);
                }
            }
            catch (System.Exception)
            {
                throw;
            }
            return(ChangeType(this, typeof(TPage)) as TPage);
        }
示例#15
0
        public virtual TPage SetPageValues(params string[] pageValues)
        {
            try
            {
                if (pageValues.Length > _selector.SetFields.Length)
                {
                    throw new SelectorException($"The entry values ({pageValues.Length}) must be less than or equal to the number of 'SetFields' selectors count: {_selector.SetFields.Length}");
                }

                if (SeleniumUtil.WaitElement(_driver, GetBySelector(_selector.SetFields[0])).Displayed)
                {
                    for (int i = 0; i < pageValues.Length; i++)
                    {
                        HandleInputType(_selector.SetFields[i], pageValues[i]);
                    }
                }
            }
            catch (System.Exception)
            {
                throw;
            }
            return(ChangeType(this, typeof(TPage)) as TPage);
        }
示例#16
0
        public virtual TPage GetTableInstances(params string[] tableValues)
        {
            if (tableValues is null || tableValues.All(el => string.IsNullOrEmpty(el)))
            {
                throw new ArgumentException("Can't perform the action due to the lack of entry values.");
            }

            if (tableValues.Length > _selector.TableFields.Length)
            {
                throw new SelectorException($"The number of entry values ({tableValues.Length}) doesn't match the length of 'TableFields' selectors: {_selector.TableFields.Length}.");
            }

            if (SeleniumUtil.WaitElement(_driver, GetBySelector(_selector.Table)).Displayed)
            {
                var table = _driver.FindElements(GetBySelector(_selector.Table));
                TableInstances = new List <IWebElement>();

                foreach (var element in table)
                {
                    var assertCount = 0;
                    for (int i = 0; i < tableValues.Length; i++)
                    {
                        if (element.FindElement(GetBySelector(_selector.TableFields[i])).Text.Equals(tableValues[i]))
                        {
                            assertCount++;
                        }
                    }

                    if (assertCount == tableValues.Length)
                    {
                        TableInstances.Add(element);
                    }
                }
            }
            return(ChangeType(this, typeof(TPage)) as TPage);
        }