`

selenium个人使用总结

 
阅读更多

开发中部分代码如附件

driver配置,windows 配置: TestBase.cs

url设置和浏览器参数设置:TestStarter.cs

异常后截图设置:RemoteWebDriverEx.cs TestRunner line 218

关于case中权限的设置管理类 PremissionManager.cs

关于测试中的资源文件读取写入管理类 CaseResourceManager.cs

关于测试中的case作者,模块,功能 注解类TestCaseMetadataAttribute.cs

case 涉及到的 属性有:名字,模块,脚本作者,文本作者,检查点,步奏集合,检查相,每一步的具体内容等。

 

1.查找元素:By.Name,By.Id, By.Tag, By.XPath, By.TagName, By.ClassName

    e.g.  By.XPath("//*[contains(@id,'" + this.screenId + "_streamlink_')]")

         By.XPath("./div[contains(@class,'dgrid-row')]")

2. doubleClick

   Actions builder = new Actions(this.driver);

            builder.DoubleClick(webElement);

 

            IAction doubleClick = builder.Build();

            doubleClick.Perform();

3.right click . new Actions(driver).ContextClick(webElement).Perform();

 

4. drag . 

       new Actions(driver).ClickAndHold(source).MoveByOffset(xOffset, yOffset).Release().Build().Perform();

 

5.select item in dropdown list

       protected IWebElement SelectDropDownItem(string selectId, string searchItemName)

        {

            IWebElement el=null;

            WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));

            wait.Until((d) =>

            {

                try

                {

                    int length = this.driver.FindElements(By.Id(selectId)).Count;

                    if (length > 1)

                    {

                        this.driver.FindElements(By.Id(selectId))[length - 1].FindElement(By.ClassName(HTMLConstants.selectContainer)).Click();

                    }

                    else

                    {

                        this.driver.FindElement(By.Id(selectId)).FindElement(By.ClassName(HTMLConstants.selectContainer)).Click();

                        SeleniumUtils.ThreadSleep(500);

                    }

 

                    IWebElement listElement = this.driver.FindElement(By.Id(selectId + HTMLConstants.dropdownTable_suffix));

 

 

                    el = SearchItemInListByName(listElement, By.ClassName(HTMLConstants.selectItemClass), searchItemName, true);

                }

                catch (Exception e)

                {

                    el = null;

                }

                return el;

          });

            return el;

 

        }

       protected IWebElement SearchItemInListByName(IWebElement listElement,By ItemBy,string searchItemName,bool needClick = false)

        { 

            IWebElement itemElement = null;

            IList<IWebElement> caseElementList = listElement.FindElements(ItemBy).ToList();

            int defaultCount = 0;

             int newCount = 0;

             do

             {

                 defaultCount = caseElementList.Count;

                 ScrollToView(caseElementList[caseElementList.Count - 1]);

                 caseElementList = listElement.FindElements(ItemBy).ToList();

                 newCount = caseElementList.Count;

             } while (defaultCount < newCount);

            

            foreach (IWebElement subElement in caseElementList)

            {

              //  if(!subElement.Displayed){

                    ScrollToView(subElement);

                //}

 

                if (!subElement.Text.Equals("") && subElement.Text.Trim().Equals(searchItemName.Trim()))

                {

                    itemElement = subElement;

                    if (needClick && itemElement != null) this.GetActions().MoveToElement(itemElement).Click(itemElement).Build().Perform();

                    return itemElement;

                }

            }

            return itemElement;

        }

 

        /// <summary>

        /// Search Item in the dropdown list by name.

        /// </summary>

        /// <param name="selectId"> drop down select id</param>

        /// <param name="searchItemName"> need select item.</param>

        /// <returns></returns>

        protected IWebElement SelectDropDownItem(string selectId, string searchItemName, Func<bool> func, bool expectedValue= true)

        {

            IWebElement el = null;

            WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(20));

            wait.Until((d) =>

            {

                SelectDropDownItem(selectId, searchItemName);

                return func.Invoke() == expectedValue;

            });

            return el;

 

        }

 

6. scroll to view 

        protected void ScrollToView(IWebElement viewElement) {

            if (viewElement == null) return;

            ExecuteScript(HTMLConstants.scrollJs, viewElement);

            //just using for page remance.

            SeleniumUtils.ThreadSleep(500);

 

        }

 

        protected Object ExecuteScript(string scriptCodes, params object[] args)

        {

            object obj = null;

            try

            {

                obj = ((IJavaScriptExecutor)driver).ExecuteScript(scriptCodes, args);

            }

            catch (Exception e) { }

            return obj;

 

        }

7. scroll to bottom

          protected void scrollToBottom(IWebElement webElement, By by)

        {

            try

            {

                List<IWebElement> subElement = webElement.FindElements(by).ToList();

                if (subElement == null) return;

                int currentPoint = 0;

                while (webElement.FindElements(by).ToList().Count > currentPoint)

                {

                    currentPoint = webElement.FindElements(by).ToList().Count;

                    subElement = webElement.FindElements(by).ToList();

                    ScrollToView(subElement.Last());

                    SeleniumUtils.ThreadSleep(200);

                }

            }catch(Exception e){

            }

 

        }

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics