• Selenium 学习总结


    新的项目需要, 要用 selenium+webdriver 做 Web UI 的 automation测试, 总结下学习成果,欢迎大家讨论。

    要用 Selenium前需要准备如下:(我用的是 .NET C#语言)

    1. Download Selenium:http://seleniumhq.org/download
    2. Download server:它是一个 jar文件  2.53.1
    3. IE driver:64 bit Windows IE
    4. Download .net dll: Download

    由于 selenium是基于java 的,需要安装java,下载并安装java:

    http://www.oracle.com/technetwork/java/javase/downloads/index-jsp-138363.html

    在运行 selenium 的 automation case前需要开启 selenium server

    运行 CMD:java -jar selenium-server.jar –interactive   (selenium-server.jar 在上面第2步下载得到)

    好了, 以上所有的准备工作都做好,准备好以后,我们就可以开始写 automation test cases 了。

    我用的是 VS, C# code 写的 test cases。

    打开 VS, 然后创建一个 test project,然后添加如下引用:

    WebDriver.dll,

    WebDriver.Support.dll,

    ThoughWorks.Selenium.Core.dll    (这些dll在上面第4步中下载得到)

    然后添加 using:

    using OpenQA.Selenium.Firefox;    (如果用其他的driver,可以添加 类似 IE等的 driver)

    using OpenQA.Selenium;

    using Selenium;

    using OpenQA.Selenium.Support.UI;

    然后就可以创建test cases 了

           [TestMethod]

            public void TestDriver()

            {

                OpenQA.Selenium.Firefox.FirefoxDriver webDriver = new OpenQA.Selenium.Firefox.FirefoxDriver();

                //OpenQA.Selenium.IE.InternetExplorerDriver webDriver = new OpenQA.Selenium.IE.InternetExplorerDriver(@"E:\Selenimu");    启动 IE 浏览器

                webDriver.Navigate().GoToUrl("http://www.google.com");

                IWebElement element = webDriver.FindElement(By.Id("lst-ib"));

                element.SendKeys("Tom Liu");

                IWebElement searchButton = webDriver.FindElement(By.Name("btnK"));

                searchButton.Click();

                System.Threading.Thread.Sleep(1000);

                string xpath = "//div[@id='imagebox_bigimages']/div/a";

                IWebElement imageLink = webDriver.FindElement(By.XPath(xpath));

                imageLink.Click();

                webDriver.Quit();

            }

    总结下, webdriver可以有几种找 UI 元素的方法,例如: By.Id, By.Name, By.CssSelector, By.XPath

    //By id
    WebElement ele = driver.findElement(By.id(<element id>));
    //By Name
    WebElement ele = driver.findElement(By.id(<element name>));
    //By className
    WebElement ele = driver.findElement(By.className(<element ClassName>));
    //By tabName
    WebElement ele = driver.findElement(By.tagName(<html tagName>));
    //By linkText
    WebElement ele = driver.findElement(By.linkText(<link text>));
    //By partialLinkText
    WebElement ele = driver.findElement(By.partialLinkText(<link text>));//通过部分文本定位连接
    //By cssSelector
    WebElement ele = driver.findElement(By.cssSelector(<css>));
    //By XPATH
    WebElement ele = driver.findElement(By.xpath(<element xpath>));

  • 相关阅读:
    多态
    163VIP邮箱哪个好?为什么要使用邮箱客户端?
    163邮箱的格式什么样的?常见的电子邮箱品牌有哪些?
    申请企业邮箱需要准备什么材料?外贸邮箱怎么注册?
    《10秒挑战》h5游戏案例分析
    H5反应类爆款游戏分享
    企业邮箱多少钱,哪家企业邮箱更具性价比?
    注册申请企业邮箱,哪家最优惠 #万元礼包来袭#
    购买一个163VIP邮箱,3位超短靓号更惊喜~
    小程序对H5游戏的技术分析
  • 原文地址:https://www.cnblogs.com/Tomliuxin/p/5710751.html
Copyright © 2020-2023  润新知