English 中文(简体)
在不指定概要文件的情况下,在多个Selenium测试中保持Firefox概要文件的持久性
原标题:Keeping Firefox profile persistent in multiple Selenium tests without specifying a profile

努力实现:

  • same firefox profile throughout tests

问题:

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

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

可能的解决方案:

  • Creating a selenium object in some common code that stays in memory throughout the tests. I am running each test by spawning a new python process and waiting for it to end. I am unsure how to send objects in memory to a new python object.

感谢您的帮助。

edit:我只是想实例化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()
问题回答

您可以在运行服务器本身的同时指定firefox配置文件。命令看起来像

java-jar-seleniumserver.jar-ffirefoxProfileTemplate“C:SeleniumProfiles”,其中“C:SelleniumProfile”将是存储firefox模板文件的路径。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签