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 WebDriver2 实例介绍
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