I am trying to run selenium tests I’ve written for a Django project on a Debian server, using xvfb.
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\\"]"}'
I have run export DISPLAY=:99 and am using Django LiveServerTestCase with django-selenium.
SELENIUM_DISPLAY = ':99' is set in my settings.py.
Here is my test runner:
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'),
)
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?
You need to use
setUp()andtearDown(), notsetUpClass()andtearDownClass(). The Class versions are run globally for the entire fixture, so all 3 tests are using the sameWebDriverinstance, and thus the browser isn’t in the state you expect for your second and third tests.