mercredi 20 décembre 2017

Selenium test URL not found in Django

I'm new to Selenium and I'm getting "URL not found" error during a test. It's kinda strange since everything is working in my production evnviroment. I tried to replicate my production DB in my test enviroment but I still get the same error.

I was just improving the Tutorial project of Django documentation v. 1.11.

Here is my models.py:

class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')
    def __str__(self):
        return self.question_text
    def was_published_recently(self):
        now = timezone.now()
        return now - datetime.timedelta(days=1) <= self.pub_date <= now

class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.PositiveIntegerField(validators=[MinValueValidator(0)], default=0)

    def __str__(self):
        return self.choice_text

Here is the urls.py :

app_name = 'polls'
urlpatterns = [
    url(r'^$', views.IndexView.as_view(), name='index'),
    url(r'^specifics/(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
    url(r'^(?P<pk>[0-9]+)/results/$', views.ResultsView.as_view(), name='results'),
    url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),
]

And finally we have the tests.py part:

### SELENIUM ###
class VoteTest(StaticLiveServerTestCase):
    @classmethod
    def setUpClass(cls):
        super(VoteTest, cls).setUpClass()
        cls.selenium = webdriver.Firefox()
        cls.selenium.implicitly_wait(10)
        Question.objects.create(question_text="Sample_question", days=1)
        Choice.objects.create(choice_text="Sample_choice", question=q, votes=1)

    def test_vote(self):
        self.selenium.get('%s%s' % (self.live_server_url, '/polls/'))
        self.selenium.find_element_by_xpath('//a[@id="1"]').click()

        # Here I get the "URL not found error", the URL is '.../polls/specifics/1/

As I said the URL is reachable from the webserver. I also print the values of the test DB during the tests and everything seems fine with IDs. I think Selenum has some problem with he URLconf of Django or something. Thank you

Aucun commentaire:

Enregistrer un commentaire