【问题标题】:How do I set up a Selenium Grid Python test case to test across multiple machines?如何设置 Selenium Grid Python 测试用例以跨多台机器进行测试?
【发布时间】:2016-12-06 04:05:27
【问题描述】:

我已成功设置 SeleniumGrid 以在具有不同操作系统和浏览器的多台机器上运行我的 Python 测试。 但是,我仍然需要编写 3 次相同的测试用例,每个节点一次,因为对节点的引用在测试用例内部。

我查看了各种有关 Python 的在线建议,例如。将节点 ips 分离到外部文件中并将其导入测试用例,但它们似乎都不起作用,或者说明适用于 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())

【问题讨论】:

  • 为什么不把重复的测试代码放到函数中,例如def someRepeatTest(webdriver): #your code lines go here...

标签: python selenium


【解决方案1】:

我能够使用nose_parameterized 模块同时测试两个浏览器。 (使用nose_parameterized模块不需要使用nose test runner。)

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,只需更改网络驱动程序以适应。

【讨论】:

【解决方案2】:

您可以让 Python 脚本读取配置文件,而不是通过 shell 调用传递浏览器和平台的参数。本质上,您将拥有一个配置文件,其中列出了您希望在其上运行的浏览器以及平台列表。

诀窍是您需要有一个更高级别的套件文件,该文件将使用每个组合调用其他测试。因此,您将有一个套件文件轮询此配置文件以获取浏览器和平台组合,执行具有不同组合的套件。

如果 Python 支持多线程,您甚至可以并行化测试执行。

例如,在 Ruby 中,我会从 .yml 文件中读取我的配置,然后使用每个浏览器平台组合在多个线程中执行 rake 调用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-03
    • 2020-02-26
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 2018-12-02
    相关资源
    最近更新 更多