2016-08-31 89 views
3

我正在使用python編寫一個程序,它將從Selenium測試中生成更強大的數據和報告。我希望能夠從Firefox從Selenium IDE中導出測試用例,並且不需要修改該特定的導出python文件,將其注入到我的腳本中,並允許我的腳本進行修改,例如更改webdriver(部分實現是針對Nagios服務器,它將使用PhantomJS作爲驅動程序),並提取請求信息和屏幕截圖以進行報告(順便說一句,我知道我可以在IDE模板中更改驅動程序,但這只是一個示例)。將Selenium IDE腳本導入爲unittest.TestCase並進行動態修改

可以說我已經從IDE導出的文件路徑/到/ mytests/search.py​​

import... 

class Search(unittest.TestCase): 
    def setUp(self): 
     self.driver = webdriver.Firefox() 
     self.base_url = 'http://www.example.com' 

    def test_search(self): 
     driver = self.driver 
     driver.get(self.base_url) 
     elem = self.driver.find_element_by_name('q') 
     elem.send_keys('nagios') 
     elem.send_keys(Keys.RETURN) 
     assert 'No results found.' not in self.driver.page_source 
     driver.get(self.base_url + '/status') 
     assert 'OK' in self.driver.page_source 

    def tearDown(self): 
     self.driver.quit() 

我希望我的腳本能夠做到這一點:

python -m my_wrapper mytests.search --url 'http://www.google.com' 

這將以某種方式導入 search.py​​,從中創建一個新的測試用例,並允許我重寫setUp/tearDown方法(將驅動程序更改爲PhantomJS,從sys.argv修改基本URL),並允許捕獲埃羅rs和寫入有關失敗測試可能已停止的信息(因爲有兩個不同的URL調用,該頁面的屏幕截圖將導出到驅動程序當前的任何位置)。

我想這樣 my_wrapper .__ main__

# get 'mytests.search' from argparse 
dirname, module = os.path.split('mytests.search'.replace('.', '/')) 
suite = unittest.TestLoader().discover(dirname, pattern='%s.py' % module) 

# Modify testcases here <-- THIS IS THE HARD PART 
# 
# Is there a way to inject the tests into another testCase class? 
# 
# class BaseClass(unittest.TestCase): 
# def setUp(self): 
#  self.driver = webdriver.PhantomJS() 
#  ... 
# 
# def runTest(self): pass 
# 
# def tearDown(self): ... 
# 
# then... 
new_suite = unittest.TestSuite() 
for sub_suite in master_suite: #<-- iterating over the above suite here 
    for tests in sub_suite._tests: #<-- why do you have to do this twice? 
     for test_name in tests._tests: 
      # This doesn't work but show's my thought process 
      mytest = BaseClass(methodName=test_name) 
      setattr(mytest, test_func, getattr(tests, test_name)) 
      new_suite.addTest(mytest) 

try: 
    result = unittest.TextTestRunner(verbosity=2).run(new_suite) 
except (SOME_SELENIUM_ERROR): 
    # get the screenshot, save to file <-- don't know how to do this either 

無論如何,任何幫助這個將不勝感激。我真的希望儘可能使用盡可能簡單的代碼儘可能簡化代碼。

注意:除了硒和PhantomJS之外,出於多種政策原因,我還擔心使用其他依賴項,如非標準庫等。 ...

回答

1

嗨,我的朋友,我一直在搜索這樣的事情幾天。我發現這個問題在計算器

Python unittest passing arguments

,所以你的代碼應該是這樣的:

import sys 
import unittest 

class Search(unittest.TestCase): 
    URL = "" 
    def setUp(self): 
     self.driver = webdriver.Firefox() 
     self.base_url = 'http://www.example.com' 

    def test_search(self): 
     driver = self.driver 
     driver.get(self.base_url) 
     elem = self.driver.find_element_by_name('q') 
     elem.send_keys('nagios') 
     elem.send_keys(Keys.RETURN) 
     assert 'No results found.' not in self.driver.page_source 
     driver.get(self.base_url + '/status') 
     assert 'OK' in self.driver.page_source 

    def tearDown(self): 
     self.driver.quit() 
if __name__ == "__main__": 
    Search.URL=sys.argv.pop() 
    unittest.main() 

我希望這是你一直在尋找的東西。

執行你應該做的:

python mySearchTest.py "http://example.com"