zoukankan      html  css  js  c++  java
  • 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>));

  • 相关阅读:
    20200630(A--E)题解 by 王文硕
    20200629(A--E)题解 by 章思航
    GC垃圾回收
    Codeforces Round #629 (Div. 3) A、B、C
    AtomicInteger的Increment方法的自己实现。
    两个线程,一个输出字母一个输出数字,输出A1B2C3....Z26
    NIO记录
    mysql优化相关
    一些Nginx的Linux命令和conf配置文件
    docker记录
  • 原文地址:https://www.cnblogs.com/Tomliuxin/p/5710751.html
Copyright © 2011-2022 走看看