2

我正在尝试使用 xvfb 在 Debian 服务器上运行我为 Django 项目编写的硒测试。

我尝试运行 3 个测试,在第一次测试后,它们因以下错误而失败:NoSuchElementException: Message: u'Unable to locate element: {"method":"xpath","selector":"//a[@href=\\"#detail\\"]"}'

我已经运行export DISPLAY=:99并正在使用带有 django-selenium 的 Django LiveServerTestCase。 SELENIUM_DISPLAY = ':99'在我的 settings.py 中设置。

这是我的测试运行器:

class BaseLiveTest(LiveServerTestCase):
    @classmethod
    def setUpClass(cls):
        cls.selenium = WebDriver()
        super(BaseLiveTest, cls).setUpClass()

    @classmethod
    def tearDownClass(cls):
        super(BaseLiveTest, cls).tearDownClass()
        cls.selenium.quit()

    def login(self, user):
        #helper function, to log in users
        #go to login page
        self.selenium.get("%s%s" % (self.live_server_url, reverse('userena_signin')))
        #wait for page to display
        WebDriverWait(self.selenium, 10).until(
            lambda x: self.selenium.find_element_by_id('id_identification'),
        )
        #fill in form and submit
        identifictation_input = self.selenium.find_element_by_id('id_identification')
        identifictation_input.send_keys(user.email)
        password_input = self.selenium.find_element_by_id("id_password")
        password_input.send_keys('password')
        self.selenium.find_element_by_xpath('//form/descendant::button[@type="submit"]').click()

        #wait for dashboard to load
        WebDriverWait(self.selenium, 10).until(
            lambda x: self.selenium.find_element_by_id('container'),
        )

当我自己运行每个测试时,它们都通过了,但是如果我尝试一个接一个地运行它们,最后两个会失败。有任何想法吗?

4

1 回答 1

3

您需要使用setUp()and tearDown(),而不是setUpClass()and tearDownClass()。Class 版本针对整个fixture 全局运行,因此所有3 个测试都使用同一个WebDriver实例,因此浏览器不在您对第二个和第三个测试的预期状态。

于 2012-08-17T20:43:39.377 回答