如何设置Selenium Grid的Python测试用例以在多台机器上测试?
我成功地搭建了SeleniumGrid,可以在不同操作系统和浏览器的多台机器上运行我的Python测试。不过,我还是得为每个节点写三遍相同的测试用例,因为测试用例里有节点的引用。
我查阅了很多网上的建议,比如把节点的IP地址放到外部文件里,然后导入到测试用例中,但这些方法都不管用,或者说明都是针对Java的。
我看到Mozilla的一个例子,但我不太清楚该如何把这个文件和我的测试用例结合起来,或者该怎么运行它: http://viewvc.svn.mozilla.org/vc/projects/sumo/tests/frontend/python_tests/suite_sumo.py?view=markup
我该如何设置我的Python测试用例,这样我只需要写一次就行了?
我的Hub命令提示符指令是:
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())
2 个回答
与其通过命令行传递浏览器和平台的参数,不如让你的Python脚本读取一个配置文件。简单来说,你可以准备一个配置文件,里面列出你想要运行的浏览器和平台。
关键在于,你需要一个更高级的套件文件,这个文件会调用其他测试,尝试所有可能的组合。也就是说,你会有一个套件文件,它会查看这个配置文件,获取浏览器和平台的组合,然后执行不同组合的测试。
如果Python支持多线程,你甚至可以让测试同时执行,这样可以节省时间。
举个例子,在Ruby中,我会从一个.yml文件中读取配置,然后用多个线程同时执行每个浏览器和平台的组合。
我用 nose_parameterized
这个模块同时测试了两个浏览器。(你并不需要使用nose测试工具来使用 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 Grid,正如你问的那样,只需要把网页驱动程序换成合适的就可以了。