mardi 21 août 2018

Django StaticLiveServerTestCase does not show data in grouped test

I am writing functional tests using Selenium for an app written in Django 1.9.

I have a function populate_test_db() that creates some test data using factory_boy:

def populate_test_db(self):

"""
Adds records to an empty test database
"""
self.coder = UserFactory.create()
self.country = CountryFactory.create()
self.assignment = AssignmentFactory.create()
self.assignment_progress = AssignmentProgressFactory.create(assignment = self.assignment)
self.article = ArticleFactory.create(assignments = (self.assignment,))
self.articles = ArticleFactory.create_batch(100, assignments = (self.assignment,))

return self

I call this function when setting up the functional tests for my app like this:

from functional_tests.testing_utilities import populate_test_db

class NewUserTest(StaticLiveServerTestCase):

    def setUp(self):
        self.browser = webdriver.Chrome()
        populate_test_db(self)

(I also have tried using setUpTestData() but that doesn't seem to work either with StaticLiveServerTestCase).

I then run the two following tests:

def test_can_login_to_site(self):

    self.browser.get(self.live_server_url)

    username = self.browser.find_element_by_css_selector('#login_form > table > tbody > tr:nth-child(1) > td:nth-child(2) > input[type="text"]') 
    username.send_keys('bobby')

    password = self.browser.find_element_by_css_selector('#login_form > table > tbody > tr:nth-child(2) > td:nth-child(2) > input[type="password"]') 
    password.send_keys('very_secure')
    password.send_keys(Keys.ENTER)
    time.sleep(1)

    assignment = self.browser.find_element_by_css_selector('#assignments > table > tbody > tr:nth-child(2) > td:nth-child(1) > a')
    self.assertIn(self.assignment.country.state_name, assignment.text)

def test_can_access_assignment(self):
    print(self.coder)
    print(self.assignment)
    self.browser.get(self.live_server_url)

    username = self.browser.find_element_by_css_selector('#login_form > table > tbody > tr:nth-child(1) > td:nth-child(2) > input[type="text"]') 
    username.send_keys('bobby')

    password = self.browser.find_element_by_css_selector('#login_form > table > tbody > tr:nth-child(2) > td:nth-child(2) > input[type="password"]') 
    password.send_keys('very_secure')
    password.send_keys(Keys.ENTER)
    time.sleep(1)

    assignment = self.browser.find_element_by_css_selector('#assignments > table > tbody > tr:nth-child(2) > td:nth-child(1) > a')
    assignment.click()
    time.sleep(1)

    headline = self.browser.find_element_by_css_selector('#article-info > h2')
    self.assertIn(self.article.headline, headline.text)

Both tests pass independently (i.e. when the other one is commented out), but not when both are active at the same time. When both are active, one of them fails with the following message:

File "path/to/app/functional_tests/test_functional_tests.py", line 64, in test_can_access_assignment
assignment = self.browser.find_element_by_css_selector('#assignments > table > tbody > tr:nth-child(2) > td:nth-child(1) > a')
....
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"#assignments > table > tbody > tr:nth-child(2) > td:nth-child(1) > a"}

Even though the same element just did appear in the previous test (so the first test passes, the second test doesn't). Why is that?

Aucun commentaire:

Enregistrer un commentaire