保持Firefox配置文件在多个Selenium测试中保持不变,而无需指定配置文件

tip*_*ipu 5 python selenium

试图实现:

  • 整个测试中同样的firefox配置文件

问题:

  • 测试分布在30个不同的文件中,实例化一个selenium对象,从而创建一个firefox配置文件,在第一次测试中不会持续到下面的测试,因为一旦脚本结束,对象就会死掉IIRC

  • 无法指定配置文件,因为我正在编写一个应该在不同机器上运行的测试套件

可能的解决方案:

  • 在一些公共代码中创建一个selenium对象,在整个测试过程中保留在内存中.我通过生成一个新的python进程并等待它结束来运行每个测试.我不确定如何将内存中的对象发送到新的python对象.

任何帮助表示赞赏,谢谢.

编辑:只是想到而不是生成一个子python进程来运行测试,我只是实例化selenium IDE生成的测试类,在所有30个测试中删除setUp和tearDown方法,在开始时实例化一个selenium对象,然后传递说selenium对象实例化的每个测试.

mpd*_*rty 2

我遇到了同样的问题,并且还注意到,在测试中保留单个 Firefox 会话可以大大加快测试套件的性能。

我所做的是为我的 Selenium 测试创建一个基类,该基类仅在 Firefox 尚未启动时才激活它。在拆卸过程中,此类不会关闭 Firefox。然后,我在导入所有测试的单独文件中创建了一个测试套件。当我想一起运行所有测试时,我只执行测试套件。测试套件结束时,Firefox 自动关闭。

这是基本测试类的代码:

from selenium.selenium import selenium
import unittest, time, re
import BRConfig

class BRTestCase(unittest.TestCase):
    selenium = None

    @classmethod
    def getSelenium(cls):
        if (None == cls.selenium):
            cls.selenium = selenium("localhost", 4444, "*chrome", BRConfig.WEBROOT)
            cls.selenium.start()
        return cls.selenium

    @classmethod
    def restartSelenium(cls):
        cls.selenium.stop()
        cls.selenium.start()

    @classmethod
    def stopSelenium(cls):
        cls.selenium.stop()

    def setUp(self):
        self.verificationErrors = []
        self.selenium = BRTestCase.getSelenium()

    def tearDown(self):
        self.assertEqual([], self.verificationErrors)
Run Code Online (Sandbox Code Playgroud)

这是测试套件:

import unittest, sys
import BRConfig, BRTestCase

# The following imports are my test cases
import exception_on_signup
import timezone_error_on_checkout
import ...

def suite():
    return unittest.TestSuite((\
        unittest.makeSuite(exception_on_signup.ExceptionOnSignup),
        unittest.makeSuite(timezone_error_on_checkout.TimezoneErrorOnCheckout),
        ...
    ))

if __name__ == "__main__":
    result = unittest.TextTestRunner(verbosity=2).run(suite())
    BRTestCase.BRTestCase.stopSelenium()
    sys.exit(not result.wasSuccessful())
Run Code Online (Sandbox Code Playgroud)

这样做的一个缺点是,如果您仅从命令行运行单个测试,Firefox 将不会自动关闭。然而,我通常将所有测试一起运行,作为将代码推送到 Github 的一部分,因此解决这个问题并不是我的首要任务。

以下是在此系统中运行的单个测试的示例:

from selenium.selenium import selenium
import unittest, time, re
import BRConfig
from BRTestCase import BRTestCase

class Signin(BRTestCase):
    def test_signin(self):
        sel = self.selenium
        sel.open("/signout")
        sel.open("/")
        sel.open("signin")
        sel.type("email", "test@test.com")
        sel.type("password", "test")
        sel.click("//div[@id='signInControl']/form/input[@type='submit']")
        sel.wait_for_page_to_load("30000")
        self.assertEqual(BRConfig.WEBROOT, sel.get_location())

if __name__ == "__main__":
    unittest.main()
Run Code Online (Sandbox Code Playgroud)