分享一个selenium帮助类 -- seleniumHelper for .net

原创:分享一个selenium帮助类 -- seleniumHelper for .net

 

GUI自动化测试接触的不多,以前也就接触过QTP(现在叫UFT了),但都没深入过。

这次正好有个WEB测试项目,就试了下selenium.

 

废话不多说了,先上效果.

 视频看不到的戳这里

这里就拿百度来做个DEMO

先上测试代码,源码下周放出,DLL在这里

 static void testBaidu()
        {
            string i = string.Empty;
            RemoteWebDriver driver = new ChromeDriver();
            IOptions io = driver.Manage();
            io.Window.Maximize();
            

            string url = @"http://www.baidu.com";
            driver.Navigate().GoToUrl(url);

            IWebElement searchBox = driver.GetElementLazy(By.Id("kw"), 10);
            if (searchBox.Location.X < 200)
            {
                i = "1";
                searchBox = driver.GetElementLazy(By.Id("kw1"), 10);
            }
            driver.HighLight(searchBox,2);
            
            StepInfo si = new StepInfo();
            si.StepName = "step 1";
            si.StepAction = "Type HP in search box";
            si.StepExResult = "HP displayed in search box";
            si.StepStatus = StepStatus.process;

            driver.ShowStepAroundElement(si, searchBox, Align.auto);
            Thread.Sleep(2000);

            driver.RemoveHelper();
            
            while(searchBox.Displayed == false)
            {
                Thread.Sleep(1000);
            }
            searchBox.SendKeys("HP");

            si.StepStatus = StepStatus.fail;
            driver.ShowStepAroundElement(si, searchBox, Align.bottom);
            Thread.Sleep(2000);
            driver.RemoveHelper();

            IWebElement button = driver.GetElementLazy(By.Id("su"+i), 10);
            driver.HighLight(button);
            button.Click();

            driver.SwitchTo().DefaultContent();
            searchBox = driver.GetElementLazy(By.Id("kw"), 10);
            driver.HighLight(searchBox);
            searchBox.Clear();

            si.StepAction = "Type HP in search box";
            si.StepStatus = StepStatus.pass;

            driver.ShowStepAroundElement(si, searchBox, Align.right);
            Thread.Sleep(2000);
            driver.RemoveHelper();
            searchBox.SendNewKeys("Keys");


            button = driver.GetElementLazy(By.Id("su"), 10);
            driver.HighLight(button);
            string source = driver.Title;

            button.Click();

            //WebDriverWait wait = new WebDriverWait(driver,TimeSpan.FromSeconds(10));
            //IWebElement num = driver.FindElement(By.ClassName("nums"));
            //IWebElement num = wait.Until<IWebElement>((d) => { return d.FindElement(By.ClassName("nums")); });
            IWebElement num = driver.GetElementUntil(() => { return driver.Title != source; }, By.ClassName("nums"), 10);
            
           
            driver.HighLight(num, 3);
            si.StepName = "Step2";
            si.StepAction = "Check whether the record number";
            si.StepExResult = "The number should bigger than 20W";
            si.StepStatus = StepStatus.pass;
            driver.ShowStepAroundElement(si, num, Align.top);


            driver.SaveScreenUntil(() => { return driver.GetElementLazy(By.Id(si.StepId),2)!=null; }, @"E:\test.png", 2);
            driver.RemoveHelper();

            searchBox = driver.GetElementLazy(By.Id("kw"), 10);
            driver.HighLight(searchBox,2);
            searchBox.SendNewKeys("豆瓣音乐");


            button = driver.GetElementLazy(By.Id("su"), 10);
            driver.HighLight(button);

            button.Click();
            string xPath = "//span[text()=‘进入应用‘]";
            button = driver.GetElementLazy(By.XPath(xPath),10);
            driver.HighLight(button, 2);
            button.Click();
            Thread.Sleep(5000);
            driver.Quit();
        }
View Code

这个类主要扩展了IWebdriver的方法.

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。