硒测试在第一次测试后失败 [英] Selenium tests fail after first test

查看:126
本文介绍了硒测试在第一次测试后失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to run selenium tests I've written for a Django project on a Debian server, using xvfb.

我有3个测试,我试图运行,第一次测试后,他们失败与这个错误:
NoSuchElementException:消息:u'Unable定位元素: {method:xpath,selector:// a [@ href = \\#detail\\]}'

I have 3 tests I am trying to run, after the first test, they fail with this error: NoSuchElementException: Message: u'Unable to locate element: {"method":"xpath","selector":"//a[@href=\\"#detail\\"]"}'

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

I have run export DISPLAY=:99 and am using Django LiveServerTestCase with django-selenium. SELENIUM_DISPLAY = ':99' is set in my 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'),
        )

当我运行每个测试本身它们都通过,但如果我尝试运行他们一个接一个最后2失败。任何想法?

When I run each test by itself they all pass, but if I try to run them one after another the last 2 fail. Any ideas?

推荐答案

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

You need to use setUp() and tearDown(), not setUpClass() and tearDownClass(). The Class versions are run globally for the entire fixture, so all 3 tests are using the same WebDriver instance, and thus the browser isn't in the state you expect for your second and third tests.

这篇关于硒测试在第一次测试后失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆