Sunday, July 31, 2016

Re: Testing if a view have an html element with at least one attribute

2016-07-31 12:38 GMT+02:00 ludovic coues <couesl@gmail.com>:
Hello,

I am trying to test if a view is displaying a form with an input
element with name=username.

Currently, I have tried a lot of variation around
`self.assertContains( response, "<input name=\"username\">",
html=True)` but none work.
I assume that doesn't work because the view have a field input with
name=username but also autofocus="", a class and a bunch of other
attribute.

I could extract the form from the context and check the field username
from the form is displayed in the view but I'm not ok with that
solution. I'm testing if there is a input with name=username, not a
bunch of unrelated attribute.

I could also use a LiveServerTestCase and selenium but that look like
a bit overkill for my need. lxml is another option but it would bring
more dependencies.

A bit of help would be welcome :)



--

Cordialement, Coues Ludovic
+336 148 743 42

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAEuG%2BTah74hdZMv%2BwdZPPq5PLaJ%3DhxOFMNXuVLfFYSw2Uz4N0w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Django usually creates the inputs the same way, so what I do in my tests is to first dump the content of the response body on the screen and then copy the input statement from there. What you probably need is '<input name="username"', because it doesn't matter for your test if the html tag is closed. So you should be fine with:
self.assertContains(response, '<input name="username"')

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CALXYUbmmotLwzjZY6ZZnAqy21xqZN1%3DiE7ah3g5JxFgEw-POZg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment