2012-11-26 63 views
0

我是一名硒自动化的新手。我创建了一个Selenium测试用例&测试套件。 我将测试套件作为Python webdriver导出。来自Selenium的Python webdriver

我该如何执行这个python代码? 我尝试这样做:

./pythonwebdriver <selenium test case.html>

我得到这个错误:

Traceback (most recent call last): 
File "./pythondriver.py", line 52, in <module> 
unittest.main() 
File "/usr/lib/python2.7/unittest/main.py", line 94, in __init__ 
self.parseArgs(argv) 
File "/usr/lib/python2.7/unittest/main.py", line 149, in parseArgs 
self.createTests() 
File "/usr/lib/python2.7/unittest/main.py", line 158, in createTests 
self.module) 
File "/usr/lib/python2.7/unittest/loader.py", line 128, in loadTestsFromNames 
suites = [self.loadTestsFromName(name, module) for name in names] 
File "/usr/lib/python2.7/unittest/loader.py", line 100, in loadTestsFromName 
parent, obj = obj, getattr(obj, part) 
AttributeError: 'module' object has no attribute '<testcasename>' 

回答

6

有没有这样的事情的Python的webdriver。 Webdriver是驱动网页的组件。它已被集成到Selenium 2中。本地它在Java中工作,但有很多语言的绑定可用,包括Python

下面是来自webdriver文档的一个带注释的示例,稍加修改。为了创建单元测试,创建一个测试类,它继承了unittest模块提供的类TestCase。有关的webdriver

#!/usr/bin/python 

from selenium import webdriver 
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0 
import unittest 

class GoogleTest(unittest.TestCase): 
    def test_basic_search(self): 
     # Create a new instance of the Firefox driver 
     driver = webdriver.Firefox() 
     driver.implicitly_wait(10) 

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

     # 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() 

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

     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(lambda driver : 
            driver.title != original_title) 
      self.assertIn("cheese!", driver.title.lower()) 

      # You should see "cheese! - Google Search" 
      print driver.title 
     finally: 
      driver.quit() 

if __name__ == '__main__': 
    unittest.main() 

一个好处是,你可以改变该驱动线是

driver = webdriver.Chrome() 
driver = webdriver.Firefox() 
driver = webdriver.Ie() 

取决于什么浏览器,你需要测试。除了ChromeDriver,FirefoxDriverInternetExplorerDriver还有HtmlUnitDriver这是最轻量级的,可以运行无头(但可能运行一些JavaScript不同于浏览器),RemoteWebDriver它允许在远程机器上并行运行测试,以及许多其他(iPhone,Android, Safari,Opera)。

运行它可以像运行任何python脚本一样完成。要么只是:

python <script_name.py> 

或者包括像上述!#/usr/bin/python的第一行解释器的名字。最后两行

if __name__ == '__main__': 
    unittest.main() 

当该文件直接运行时,使脚本运行测试,如./selenium_test.py。也可以从多个文件自动收集测试用例并将它们一起运行(请参阅unittest文档)。运行在某些模块或个别测试测试另一种方式是

python -m unittest selenium_test.GoogleTest 
+0

如何运行导出的python脚本?我的问题中提到了错误。 – cppcoder

+0

对不起,我忘了解决运行的部分。我现在已经更新了答案,包括设置单元测试框架并包含运行测试的一些方法。由于您没有提供给出错误的脚本,因此很难说错误究竟是什么,但希望这会有所帮助。 – Edu

1

你的脚本调用unittest.main()处理该命令行参数:<selenium test case.html>unittest.main()需要测试模块,测试类或测试方法的名称作为命令行参数,而不是<selenium test case.html>