51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 6593|回复: 8

Selenium Webdriver开发入门

[复制链接]

该用户从未签到

发表于 2013-5-4 11:03:58 | 显示全部楼层 |阅读模式
Selenium Webdriver开发入门

1        文档目的
  编写此文档的目的在于帮助更多想学习Selenium webdriver的同好们,最近发现好多人想学习Selenium做自动化,又不知道怎么入门,因此写下这篇入门的文档供各位使用,希望大家能更快的领会到Selenium webdriver 的强大之处。本文只对Selenium webdriver入门作介绍,Selenium rc由于本人认为已经过时,不做介绍。另外本文是一个初级入门文档,想要深入学习的话,并不合适。本文假定你有一定的编程水平,因此对于以下基本编程问题不在这做介绍。

2        Selenium 是什么
        Selenium是一组工具的集合。包括Selenium Rc,Selenium Grid,Selenium IDE,Selenium Webdriver.
        目前大家都在说的Selenium通常是指Selenium Webdriver也就是Selnium2.0.既然说到了Selenium2.0,在这也提一下什么是selenium1.0好了,1.0版本的selenium1.0主要是指Selenium RC(一个基于Javascript注入的工具),Selenium Grid,SeleniumID等工具的集合。在讲Webdriver集成到Selenium之后发布了Selenium 2.0版本。
        Selenium Rc和Selenium webdriver的区别,这里简单说一下。Selenium rc对于多个浏览器窗口并不支持,所以有些情况处理不了,Webdriver能干Selenium rc能干的所有事,而且支持多浏览器窗口。当然了,Webdriver的缺点也在于支持的浏览器没有Selenium rc多。但是不代表不够用。下图是官网上Webdriver对浏览器支持的信息
        Google Chrome 12.0.712.0+
        Internet Explorer 6, 7, 8, 9 - 32 and 64-bit where applicable
        Firefox 3.0, 3.5, 3.6, 4.0, 5.0, 6, 7
        Opera 11.5+
        HtmlUnit 2.9
        Android – 2.3+ for phones and tablets (devices & emulators)
        iOS 3+ for phones (devices & emulators) and 3.2+ for tablets (devices & emulators)
        注意以上对于移动设备的支持是说对移动设备上的浏览器的支持,支持的浏览器版本是特定的。

3        怎样开始开发自己的第一个Webdriver脚本。
   本文以C#语言作为示例开发语言,开发工具选用VS2010。题外话,语言只是工具使用什么语言开发脚本都无所谓。在阅读本小节的前提是假定你已经会创建C# 控制台或者测试项目。

1)        前期准备
        在官网的下载页面找到C#版本的weddriver下载下来(Selenium Client & WebDriver Language Bindings)
        对于IE浏览器你还需要下载The Internet Explorer Driver Server
        下载上述包文件后,解压缩。
2)        创建C# 控制台项目并添加对Webdriver.dll的引用。开始编写第一个脚本吧。下面是官网给出的示例。
3)        using OpenQA.Selenium;
4)        using OpenQA.Selenium.Firefox;
5)       
6)        // Requires reference to WebDriver.Support.dll
7)        using OpenQA.Selenium.Support.UI;
8)       
9)        class GoogleSuggest
10)        {
11)            static void Main(string[] args)
12)            {
13)                // Create a new instance of the Firefox driver.
14)       
15)                // Notice that the remainder of the code relies on the interface,
16)                // not the implementation.
17)       
18)                // Further note that other drivers (InternetExplorerDriver,
19)                // ChromeDriver, etc.) will require further configuration
20)                // before this example will work. See the wiki pages for the
21)                // individual drivers at http://code.google.com/p/selenium/wiki
22)                // for further information.
23)                IWebDriver driver = new FirefoxDriver();
24)       
25)                //Notice navigation is slightly different than the Java version
26)                //This is because 'get' is a keyword in C#
27)                driver.Navigate().GoToUrl("http://www.google.com/");
28)       
29)                // Find the text input element by its name
30)                IWebElement query = driver.FindElement(By.Name("q"));
31)       
32)                // Enter something to search for
33)                query.SendKeys("Cheese");
34)       
35)                // Now submit the form. WebDriver will find the form for us from the element
36)                query.Submit();
37)       
38)                // Google's search is rendered dynamically with JavaScript.
39)                // Wait for the page to load, timeout after 10 seconds
40)                WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
41)                wait.Until((d) => { return d.Title.ToLower().StartsWith("cheese"); });
42)       
43)                // Should see: "Cheese - Google Search"
44)                System.Console.WriteLine("Page title is: " + driver.Title);
45)       
46)                //Close the browser
47)                driver.Quit();
48)            }
49)        }
回复

使用道具 举报

该用户从未签到

发表于 2013-5-9 17:21:27 | 显示全部楼层
好帖,学习了
回复 支持 反对

使用道具 举报

该用户从未签到

 楼主| 发表于 2013-5-17 16:36:08 | 显示全部楼层
有什么问题,可以提问,尽量回答啊
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2013-5-29 12:35:36 | 显示全部楼层
回复 3# langhai5212


    请问哪里有用vs2010+selenium2的教程呢?
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2013-6-5 15:10:58 | 显示全部楼层
好贴
回复 支持 反对

使用道具 举报

该用户从未签到

 楼主| 发表于 2013-6-8 15:55:16 | 显示全部楼层
回复 4# martin_lc

你下载slenium 2.0的 C#版的包,解压缩以后,创建测试项目,添加webdriver.dll引用就可以写了
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2013-7-2 23:33:45 | 显示全部楼层
本帖最后由 502testing 于 2013-7-4 01:00 编辑

{:4_83:}果断收藏
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2013-8-28 16:18:48 | 显示全部楼层
好贴~对初学者很受用

弱弱的问一句,是不是只有slenium IDE可以支持脚本录制呢?
回复 支持 反对

使用道具 举报

该用户从未签到

 楼主| 发表于 2014-2-13 18:02:09 | 显示全部楼层
回复 8# yttself
selenium IDE可以录制,不建议录制
回复 支持 反对

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-4-17 01:45 , Processed in 0.076920 second(s), 28 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表