第一次测试后,硒测试失败

时间:2012-08-17 19:52:38

标签: python django selenium

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

我正在尝试运行3个测试,在第一次测试后,它们会因此错误而失败: NoSuchElementException:消息:u'无法找到元素:{"method":"xpath","selector":"//a[@href=\\"#detail\\"]"}'

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

这是我的测试跑步者:

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'),
        )

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

1 个答案:

答案 0 :(得分:3)

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

相关问题