Selenium Web Driver

1 WebDriver

1. WebDriver介绍

Selenium 2.0最主要的新特性就是集成了精心设计的面向对象的WebDriver API。
WebDriver提供更加简单明了的接口来弥补Selenium-RCAPI的不足。
WebDriver更好的支持动态网页

2. WebDriver与Selenium-RC如何驱动浏览器

1) 对于所有支持的浏览器Selenium-RC都是使用的同一种方法: 当浏览器启动时,向其中注入javascript functions,从而使用这些js来驱动浏览器中的AUT(Application Under Test)
2) WebDriver使用对自动化的原生支持API,直接调用浏览器。不同类型的浏览器都会有原生的接口支持自动化操作。

3. WebDriver与Selenium-Server

是否需要是用Selenium Server取决于使用WebDriver的方式。
不需要使用Selenium-Server的两种情况:
        1) testcases仅仅使用了Webdriver的API;
        2) 浏览器和testcases在同一台PC上,而且testcases只使用了Webdriver API。WebDriver会直接运行浏览器。

需要使用Selenium-Server和Selenium-WebDriver:
        1) 使用Selenium-Grid分配testcases到集群环境(或者虚拟机)上;
        2) 需要调用非本机上的特定版本的浏览器;
        3) 未使用Java bindings,且想要使用HtmlUnitDriver。

4. 配置Selenium-WebDriver工程

   安装Selenium意味着在开发环境上配置一个工程,然后用Selenium编写程序。如何安装Selenium取决于编程语言和开发环境。 
    Python环境下安装Selenium:
pip install selenium

5. 迁移到Selenium2.0

   如何将Selenium1.0上构建的代码迁移到Selenium2.0。请参考Selenium2.0的首席开发工程师Simon Stewart文章: Magrating From Selenium RC to Selenium WebDriver

2 实例介绍

1 WebDriver可以用来实现Web应用程序的自动化测试,特别适合于验证实际结果是否符合预期结果的场景
2 WebDriver没有使用任何第三方测试框架,所以可以很好与单元测试工具或者最古老的main函数结合使用。
3 当你创建完Selenium工程后,你会发现WebDriver和普通的第三方库一样是完全独立的,在你使用之前不需要启动任何额外的进程或者安装程序,相反如果你使用Selenium-RC需要先启动代理服务器。

  注意:当你使用如下WebDriver时需要额外的步骤:Chrome Driver,Opera Driver,Android Driver,IPhone Driver。

实例1:

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0

# Create a new instance of the Firefox driver
driver = webdriver.Firefox()

# go to the google home page
driver.get("http://www.google.com")

# the page is ajaxy so the title is originally this:
print driver.title

# find the element that's name attribute is q (the google search box)
inputElement = driver.find_element_by_name("q")

# type in the search
inputElement.send_keys("cheese!")

# submit the form (although google automatically searches now without submitting)
inputElement.submit()

try:
    # we have to wait for the page to refresh, the last thing that seems to be updated is the title
    WebDriverWait(driver, 10).until(EC.title_contains("cheese!"))

    # You should see "cheese! - Google Search"
    print driver.title

finally:
    driver.quit()

实例2:

结合Django中的LiveServerTestCase来使用。

在Django的app下面的测试模块(myapp/tests.py)中输入:

from django.test import LiveServerTestCase
from selenium.webdriver.firefox.webdriver import WebDriver

class MySeleniumTests(LiveServerTestCase):
    fixtures = ['user-data.json']

    @classmethod
    def setUpClass(cls):
        super(MySeleniumTests, cls).setUpClass()
        cls.selenium = WebDriver()

    @classmethod
    def tearDownClass(cls):
        cls.selenium.quit()
        super(MySeleniumTests, cls).tearDownClass()

    def test_login(self):
        self.selenium.get('%s%s' % (self.live_server_url, '/login/'))
        username_input = self.selenium.find_element_by_name("username")
        username_input.send_keys('myuser')
        password_input = self.selenium.find_element_by_name("password")
        password_input.send_keys('secret')
        self.selenium.find_element_by_xpath('//input[@value="Log in"]').click()
运行测试:

$ ./manage.py test myapp.tests.MySeleniumTests.test_login
本实例自动打开Firefox,到登录页面,自动输入用户名和密码,再点击click。
Window下的运行结果:

C:\mysite>manage.py test polls.tests.MySeleniumTests.test_login
Creating test database for alias 'default'...
C:\Python27\lib\site-packages\django\core\management\commands\loaddata.py:225: U
serWarning: No fixture named 'user-data' found.
  warnings.warn("No fixture named '%s' found." % fixture_name)

Traceback (most recent call last):
  File "C:\Python27\lib\wsgiref\handlers.py", line 85, in run
    self.result = application(self.environ, self.start_response)
  File "C:\Python27\lib\site-packages\django\test\testcases.py", line 1108, in _
_call__
    return super(FSFilesHandler, self).__call__(environ, start_response)
  File "C:\Python27\lib\site-packages\django\core\handlers\wsgi.py", line 189, i
n __call__
    response = self.get_response(request)
  File "C:\Python27\lib\site-packages\django\test\testcases.py", line 1091, in g
et_response
    return self.serve(request)
  File "C:\Python27\lib\site-packages\django\test\testcases.py", line 1103, in s
erve
    return serve(request, final_rel_path, document_root=self.get_base_dir())
  File "C:\Python27\lib\site-packages\django\views\static.py", line 54, in serve

    fullpath = os.path.join(document_root, newpath)
  File "C:\Python27\lib\ntpath.py", line 65, in join
    result_drive, result_path = splitdrive(path)
  File "C:\Python27\lib\ntpath.py", line 115, in splitdrive
    if len(p) > 1:
TypeError: object of type 'NoneType' has no len()
Traceback (most recent call last):
  File "C:\Python27\lib\wsgiref\handlers.py", line 85, in run
    self.result = application(self.environ, self.start_response)
  File "C:\Python27\lib\site-packages\django\test\testcases.py", line 1108, in _
_call__
    return super(FSFilesHandler, self).__call__(environ, start_response)
  File "C:\Python27\lib\site-packages\django\core\handlers\wsgi.py", line 189, i
n __call__
    response = self.get_response(request)
  File "C:\Python27\lib\site-packages\django\test\testcases.py", line 1091, in g
et_response
    return self.serve(request)
  File "C:\Python27\lib\site-packages\django\test\testcases.py", line 1103, in s
erve
    return serve(request, final_rel_path, document_root=self.get_base_dir())
  File "C:\Python27\lib\site-packages\django\views\static.py", line 54, in serve

    fullpath = os.path.join(document_root, newpath)
  File "C:\Python27\lib\ntpath.py", line 65, in join
    result_drive, result_path = splitdrive(path)
  File "C:\Python27\lib\ntpath.py", line 115, in splitdrive
    if len(p) > 1:
TypeError: object of type 'NoneType' has no len()
Traceback (most recent call last):
  File "C:\Python27\lib\wsgiref\handlers.py", line 85, in run
    self.result = application(self.environ, self.start_response)
  File "C:\Python27\lib\site-packages\django\test\testcases.py", line 1108, in _
_call__
    return super(FSFilesHandler, self).__call__(environ, start_response)
  File "C:\Python27\lib\site-packages\django\core\handlers\wsgi.py", line 189, i
n __call__
    response = self.get_response(request)
  File "C:\Python27\lib\site-packages\django\test\testcases.py", line 1091, in g
et_response
    return self.serve(request)
  File "C:\Python27\lib\site-packages\django\test\testcases.py", line 1103, in s
erve
    return serve(request, final_rel_path, document_root=self.get_base_dir())
  File "C:\Python27\lib\site-packages\django\views\static.py", line 54, in serve

    fullpath = os.path.join(document_root, newpath)
  File "C:\Python27\lib\ntpath.py", line 65, in join
    result_drive, result_path = splitdrive(path)
  File "C:\Python27\lib\ntpath.py", line 115, in splitdrive
    if len(p) > 1:
TypeError: object of type 'NoneType' has no len()
Traceback (most recent call last):
  File "C:\Python27\lib\wsgiref\handlers.py", line 85, in run
    self.result = application(self.environ, self.start_response)
  File "C:\Python27\lib\site-packages\django\test\testcases.py", line 1108, in _
_call__
    return super(FSFilesHandler, self).__call__(environ, start_response)
  File "C:\Python27\lib\site-packages\django\core\handlers\wsgi.py", line 189, i
n __call__
    response = self.get_response(request)
  File "C:\Python27\lib\site-packages\django\test\testcases.py", line 1091, in g
et_response
    return self.serve(request)
  File "C:\Python27\lib\site-packages\django\test\testcases.py", line 1103, in s
erve
    return serve(request, final_rel_path, document_root=self.get_base_dir())
  File "C:\Python27\lib\site-packages\django\views\static.py", line 54, in serve

    fullpath = os.path.join(document_root, newpath)
  File "C:\Python27\lib\ntpath.py", line 65, in join
    result_drive, result_path = splitdrive(path)
  File "C:\Python27\lib\ntpath.py", line 115, in splitdrive
    if len(p) > 1:
TypeError: object of type 'NoneType' has no len()
.
----------------------------------------------------------------------
Ran 1 test in 8.232s

OK
Destroying test database for alias 'default'...

3 Selenium-WebDriver API详解

参考Selenium-WebDriver API Commands and Operations:
http://docs.seleniumhq.org/docs/03_webdriver.jsp

4 Selenium-WebDriver的Drivers

WebDriver是测试的关键接口的名字,但有几个实现:
1 HtmlUnit Driver
2 Firefox Driver

driver = webdriver.Firefox()

3 Internet Explorer Driver

driver = webdriver.Ie()

4 ChromeDriver

driver = webdriver.Chrome()
5 Opera, iOS, Android Driver

5 WebDriver高级用法

http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp



参考:

1 官方英文:http://docs.seleniumhq.org/docs/03_webdriver.jsp

2 中文:http://www.ltesting.net/ceshi/open/kygncsgj/selenium/2014/0404/207231.html

1.1 selenium webdriver学习(一)------------快速开始 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.2 selenium webdriver学习(二)————对浏览器的简单操作 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 1.3 selenium webdriver学习(三)------------执行js脚本 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 1.4 selenium webdriver学习(四)------------定位页面元素 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 1.5 selenium webdriver学习(五)------------iframe的处理 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 1.6 selenium webdriver学习(六)------------如何得到弹出窗口 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 1.7 selenium webdriver学习(七)------------如何处理alert、confirm、prompt对话框 . . . . . . . . .24 1.8 selenium webdriver学习(八)------------如何操作select下拉框 . . . . . . . . . . . . . . . . . . . . . . . . .27 1.9 selenium webdriver学习(九)------------如何操作cookies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 1.10 selenium webdriver学习(十)------------如何把一个元素拖放到另一个元素里面 . . . . . . . . . . .31 1.11 selenium webdriver学习(十一)------------如何等待页面元素加载完成 . . . . . . . . . . . . . . . . . .33 1.12 selenium webdriver学习(十二)------------如何利用selenium-webdriver截图 . . . . . . . . . . . .38
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值