W3.org 对 WebDriver API 对做定义和规范。 WebDriver API 是独立于平台(Windows\Linux\MAC)和语言(Java/c#/Ruby/Python)的,它定义了 接口和相关的通讯协议。允许脚本或程序通过本规范实现对 web 浏览器行为的控制。 该 WebDriver API 通过通讯协议和一组接口来发现页面上的 DOM 元素中定义的操作,包括控制浏览器的行为。

我们可以这样来理解,比如,插板和插头,国标标准定义插板和插头标准,比如,插头与插版的规格,

材质,大小等。那么所有的电器的厂商生产的插头与所有插板厂商生产的插板都按照这套标准来设计。所

以,我们拿到任何一个合格的插头和插板都可以匹配得上。

WebDriver API 可以理解成对操作浏览器和页面元素的一套“国标”。那么不同的编程语言都可以按

照这套标准实现自己的语言的 WebDriver 模块。

下面看一下不同编程语言下实现百度搜索的例子。

在 java 中引入 Selenium WebDriver 实现自动化测试: baidu.java //添加 Selenium(webdriver)引用 import org.openqa.Selenium.By; import org.openqa.Selenium.WebDriver; import org.openqa.Selenium.WebElement; import org.openqa.Selenium.firefox.*; public class TestHelloWorld { public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); driver.get("http://www.baidu.com/"); WebElement txtbox = driver.findElement(By.name("wd")); txtbox.sendKeys("Glen"); WebElement btn = driver.findElement(By.id("su")); btn.click(); driver.close(); } } 在 C#中引入 Selenium WebDriver 实现自动化测试: baidu.cs //添加 Selenium(webdriver)的引用 using OpenQA.Selenium; using OpenQA.Selenium.Firefox;

using OpenQA.Selenium.Support.UI;

namespace SeleniumTests

{

class Baidu

{

static void Main(string[] args)

{

driver = new FirefoxDriver();

url = "http://www.baidu.com/";

driver.Navigate().GoToUrl(url)

var searchBox = driver.FindElementById("kw");

searchBox.SendKeys("Selenium");

ar btnClick = driver.FindElementById("su");

btnClick.Click();

driver.Quit();

}

}

}

在 Ruby 中引入 Selenium WebDriver 实现自动化测试:

baidu.rb

#导入 Selenium(webdriver)包

require 'Selenium-webdriver'

driver = Selenium::WebDriver.for:chrome

driver.get "http://www.baidu.com"

driver.find_element(:id, 'kw').send_keys "Hello WebDriver!"

driver.find_element(:id, 'su').click

driver.quit

在不同的编程语言中语法会有一定差异,我们抛去语法的差异性,在不同的语言中实现百度搜索的自

动化实例都完成了下面几个操作。

1、首先导入 Selenium(webdriver)相关模块

2、调用 Selenium 的浏览器驱动,获取浏览器句柄(driver)并启动浏览器。

3、通过句柄访问百度 URL。

4、通过句柄操作页面元素(百度输入框和“百度一下”按钮)。

5、通过句柄关闭浏览器。

输出与输入

一般编程语言的教程都从打印“Hello World!”开始,我们这里也不免俗套,从打印开始。

print 打印

Python 提供 print 方法来打印信息,下面打开 Python shell 来打印一些信息。

Python shell

ActivePython 2.7.6.9 (ActiveState Software Inc.) based on

Python 2.7.8 (default, Jun 30 2014, 16:08:48) [MSC v.1500 64 bit (AMD64)] on win32

Type "copyright", "credits" or "license()" for more information.

>>> print "hello"

hello

调用 print 方法,用户双引号(" ")把需要打印输出的信息引起来就被输出了。可是,我给你什么

信息,你就打印什么,这个很无聊!那我们就打印点有意思的。让打印信息向你问好。

Python shell

>>> name = "zhangsan"

>>> print "hello %s ,Nice to meet you!" %name

hello zhangsan ,Nice to meet you!

>>> name = "Lisi"

>>> print "hello %s ,Nice to meet you!" %name

hello Lisi ,Nice to meet you!

虽然,我们两次的打印语句一样,但由于定义的变量 name 两次赋值不同,那么打印出的结果也不完

全相同。%s(string)只能打印字符串,如果想打印数字,那么就要使用%d(data)。

Python shell

>>> age=27

>>> print "You are %d !" %age

You are 27 !

但是,有时候我们并不知道自己要打印的是什么类型的数据,那么可以用%r 来表示。

Python shell

>>> n = 100

>>> print "You print is %r ." %n

You print is 100 .

>>> n = "abc"

>>> print "You print is %r ." %n

You print is 'abc' .