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>));

 

转载于:https://www.cnblogs.com/Tomliuxin/p/5710751.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值