2012-11-26 62 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>