努力实现:
在整个测试中使用相同的firefox配置文件
问题:
测试分布在30个不同的文件中,实例化一个selenium对象,从而创建一个firefox配置文件,在第一个测试中不会持续到下面的测试中,因为一旦脚本结束iirc,对象就会死掉
无法指定配置文件,因为我正在编写一个应该在不同计算机上运行的测试套件
可能的解决方案:
在一些公共代码中创建selenium对象,这些代码在整个测试过程中都保留在内存中。我通过生成一个新的python进程并等待它结束来运行每个测试。我不确定如何将内存中的对象发送到新的python对象。
感谢您的帮助。
编辑:我只想实例化Selenium IDE生成的测试类,删除所有30个测试中的setup和teardown方法,在开始时实例化一个Selenium对象,然后将所述Selenium对象传递给实例化的每个测试,而不是派生一个子Python进程来运行测试。

最佳答案

我遇到了同样的问题,还注意到在测试中持久化一个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)

这是测试套件:
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())

这样做的一个缺点是,如果只从命令行运行一个测试,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()

08-05 06:12
查看更多