2013-02-20 10 views
5

我已成功設置SeleniumGrid以跨多臺具有不同操作系統和瀏覽器的機器運行我的Python測試。 但是,我仍然必須爲每個節點編寫相同的測試用例3次,因爲對節點的引用位於測試用例的內部。如何設置Selenium Grid Python測試用例以跨多臺機器進行測試?

我已經看過Python的各種在線建議,例如。將節點分離到外部文件中並將其導入到測試用例中,但它們都不起作用,或者指令適用於Java。

有了這一個來自Mozilla的,我不知道我是如何設置這個文件與我的測試用例/如何運行:http://viewvc.svn.mozilla.org/vc/projects/sumo/tests/frontend/python_tests/suite_sumo.py?view=markup

如何設置我的Python測試案例,所以我只寫它一旦?

我中心命令提示符下的指令是:

java -jar selenium-server-standalone-2.29.0.jar -host http://localmachineipaddress -port 4444 -role hub 

我的節點命令提示符說明:

*FireFox PC, Chrome PC, Safari PC, and IE9 PC on local machine* 
java -jar selenium-server-standalone-2.29.0.jar -host localhost -role webdriver -hub http://theHubIP:4444/grid/register -port 5555 -browser browserName=firefox,maxInstances=5,platform=WINDOWS -browser browserName=chrome,maxInstances=5,platform=WINDOWS -Dwebdriver.chrome.driver=c:\SeleniumGrid\chromedriver.exe -browser browserName=iehta,maxInstances=5,platform=WINDOWS -Dwebdriver.ie.driver=c:\SeleniumGrid\IEDriverServer.exe -browser browserName=safari,maxInstances=5,platform=WINDOWS -Dwebdriver.safari.driver=c:\Python27\SafariDriver2.28.0.safariextz  

*FireFox MAC, Safari MAC, and Chrome MAC machine* 
java -jar selenium-server-standalone-2.29.0.jar -role webdriver -hub http://theHubIP:4444/grid/register -debug -port 5556 -browser browserName=firefox,maxInstances=5,platform=MAC -browser browserName=chrome,maxInstances=5,platform=MAC -browser browserName=safari,maxInstances=5,platform=MAC -Dwebdriver.safari.driver=c:\Python27\SafariDriver2.28.0.safariextz 

*IE8 PC machine* 
java -jar selenium-server-standalone-2.29.0.jar -role webdriver -hub http://theHubIP:4444/grid/register -port 5559 -browser browserName=iehta,maxInstances=5,platform=WINDOWS -Dwebdriver.ie.driver=c:\SeleniumGrid\IEDriverServer.exe 

我的測試案例命令提示符下的指令是:

python Python27/Test_Cases/SeleniumTest.py 5555 firefox WINDOWS 
python Python27/Test_Cases/SeleniumTest.py 5555 chrome WINDOWS 
python Python27/Test_Cases/SeleniumTest.py 5555 iehta WINDOWS 
python Python27/Test_Cases/SeleniumTest.py 5555 safari WINDOWS 
python Python27/Test_Cases/SeleniumTestIE8.py 5559 iehta WINDOWS 
python Python27/Test_Cases/SeleniumTestApple.py 5556 chrome MAC 
python Python27/Test_Cases/SeleniumTestApple.py 5556 firefox MAC 
python Python27/Test_Cases/SeleniumTestApple.py 5556 safari MAC 

我的測試案例是:

# coding: utf-8 
from selenium import webdriver 
from selenium.common.exceptions import TimeoutException 
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC 
from selenium.webdriver.common.keys import Keys 
import HTMLTestRunner 
import unittest, time 
import sys 

class SeleniumTest1(unittest.TestCase): 
    def setUp(self): 
    self.driver = webdriver.Remote(command_executor="http://theNodeIP:5555/wd/hub",desired_capabilities={ "browserName": browser, "platform": platform, "node":node }) 
    self.driver.implicitly_wait(2) 

def mytest(self): 
    self.driver.get("http://url.com") 
    self.driver.find_element_by_css_xpath("test_some_stuff").click() 

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

def suite(): 
    s1 = unittest.TestLoader().loadTestsFromTestCase(SeleniumTest1) 
    return unittest.TestSuite([s1]) 

def run(suite, report = "C:\\Python27\\Test_Cases\\Reports\\SeleniumTest1.html"): 
with open(report, "w") as f: 
    HTMLTestRunner.HTMLTestRunner(
       stream = f, 
       title = 'SeleniumTest1', 
       verbosity = 2, 
       description = 'SeleniumTest1' 
       ).run(suite) 

if __name__ == "__main__": 
args = sys.argv 

node=args[1] 

browser = args[2] 

platform = args[3] 

run(suite()) 
+0

爲什麼不把重複測試代碼放入函數中def someRepeatTest(webdriver):#你的代碼行到這裏... – 2017-07-16 09:29:10

回答

1

而是通過爲瀏覽器和平臺的參數,通過你的shell調用的,你可以有你的Python腳本讀取配置文件。實際上,您將擁有一個配置文件,其中列出了您想要運行的瀏覽器以及一系列平臺。

訣竅是你需要有一個更高級別的套件文件,它將調用每個組合的其他測試。因此,您將擁有一個套件文件,用於輪詢瀏覽器和平臺組合的此配置文件,並使用不同的組合套件執行套件。

如果Python中有多線程支持,甚至可以並行執行測試執行。

例如,在Ruby中,我將從.yml文件讀取配置,然後在每個瀏覽器平臺組合的多個線程中執行rake調用。

2

我能夠使用nose_parameterized模塊一次測試兩個瀏覽器。 (你不需要用鼻子測試運行使用nose_parameterized模塊。)

from django.test import LiveServerTestCase 
from nose_parameterized import parameterized 
from selenium import webdriver 


class UITest(LiveServerTestCase): 

    def setUp(self): 
     self.selenium = { 
      'chrome': webdriver.Chrome(), 
      'firefox': webdriver.Firefox(), 
     } 

    def tearDown(self): 
     for browser in self.selenium: 
      self.selenium[browser].quit() 

    testdata = [ 
     ('chrome',), 
     ('firefox',), 
    ] 

    @parameterized.expand(testdata) 
    def test_something(self, browser): 
     driver = self.selenium[browser] 
     # [...] 

使用Selenium格,你的問題問,只是改變webdrivers適合。

+0

正如你可以「成功設置SeleniumGrid來運行我的Python測試」,請幫我解決我的問題https://stackoverflow.com/q/45127482/ 248616。萬分感謝! – 2017-07-16 10:35:44

相關問題