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

2016-08-03 Thread Fred Stluka

  
  
Ludovic,

On my project, we have lots of test cases that do a GET or POST and

then check the returned HTML.  We use the BeautifulSoup HTML 
parser from our Django tests to avoid the types of errors you're 
getting with simple string comparisons.  I'm not sure if there any
pros/cons vs lxml, but it works great for us!

--Fred
  
  
  Fred Stluka -- mailto:f...@bristle.com --
  http://bristle.com/~fred/
  
  Bristle Software, Inc -- http://bristle.com -- Glad to be of
  service!
  
  Open Source: Without walls and fences, we need no Windows or
  Gates.
  
  

On 7/31/16 5:08 PM, Andreas Kuhne
  wrote:


  Jupp, thats about what I meant :-)


Probably the best way, if you don't want to check text.


Regards,


Andréas

  
  2016-07-31 18:01 GMT+02:00 ludovic
coues :
Currently,
  I am using lxml. More dependencies but it's the cleanest
  method I've found currently.
  I use it like that:
  
      from django.test import TestCase
      from lxml import etree
  
      class FormTest(TestCase):
          def test_input(self):
              response = self.client.get('/accounts/login')
              self.assertEqual(response.status_code, 200)
              doc = etree.HTML(response.content)
             
  self.assertEqual(len(doc.findall('.//input[@name="username"]')),
  1)
  

  
  
  2016-07-31 17:46 GMT+02:00 Andreas Kuhne :
  > 2016-07-31 15:59 GMT+02:00 ludovic coues :
  >>
  >> Oh, sorry. A bit of misunderstanding and
  miscommunication on my part.
  >>
  >> The exemple I gave is just a quick way to
  reproduce my problem. The
  >> real test use self.client, reverse, cast
  response.content to a string.
  >> What I gave is a minimal exemple.
  >>
  >> Also, I assumed you talked about comparing
  bit of html element, not
  >> the raw content returned by the view. I'm
  quite uneasy to have the
  >> test failing if a class is added or removed
  from the element. But yes,
  >> your solution work for the current state of
  the application.
  >>
  >> 2016-07-31 14:46 GMT+02:00 Andreas Kuhne :
  >> > 2016-07-31 13:56 GMT+02:00 ludovic coues
  :
  >> >>
  >> >> First, thanks for the suggestion.
  >> >>
  >> >> I just tried that, didn't work.
  >> >> Here is the test file I used:
  >> >>
  >> >>
  >> >>     from django.test import TestCase
  >> >>
  >> >>     class HTMLTestCase(TestCase):
  >> >>
  >> >>         def
  test_input_in_fieldset(self):
  >> >>             fieldset = """
  >> >>         
  >> >>             
  >> >>             
  >> >> maxlength="254" name="username"
  rows="3" type="text" required />
  >> >>         
  >> >>     """
  >> >>           
   self.assertInHTML('',
  fieldset)
  >> >>           
   self.assertInHTML('',
  fieldset)
  >> >>
  >> >>
  >> >> First input is to have a working
  exemple, second is taken as is from
  >> >> my view. Not closing the input in
  assertInHTML give an error `Couldn't
  >> >> find '
  >> >> errors with assertContains(*args,
  html=True)
  >> >>
  >> >> I could copy/past the input directly
  in my test but if the maxlength
  >> >> or class attribute change, the test
  will break. Taking the input
  >> >> directly from the django form will
  test if the django form is rendered
  >> >> in the view, not if the view is
  displaying a suitable form.
  >> >>
  >> >> 

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

2016-07-31 Thread Andreas Kuhne
Jupp, thats about what I meant :-)

Probably the best way, if you don't want to check text.

Regards,

Andréas

2016-07-31 18:01 GMT+02:00 ludovic coues :

> Currently, I am using lxml. More dependencies but it's the cleanest
> method I've found currently.
> I use it like that:
>
> from django.test import TestCase
> from lxml import etree
>
> class FormTest(TestCase):
> def test_input(self):
> response = self.client.get('/accounts/login')
> self.assertEqual(response.status_code, 200)
> doc = etree.HTML(response.content)
>
> self.assertEqual(len(doc.findall('.//input[@name="username"]')), 1)
>
>
>
> 2016-07-31 17:46 GMT+02:00 Andreas Kuhne :
> > 2016-07-31 15:59 GMT+02:00 ludovic coues :
> >>
> >> Oh, sorry. A bit of misunderstanding and miscommunication on my part.
> >>
> >> The exemple I gave is just a quick way to reproduce my problem. The
> >> real test use self.client, reverse, cast response.content to a string.
> >> What I gave is a minimal exemple.
> >>
> >> Also, I assumed you talked about comparing bit of html element, not
> >> the raw content returned by the view. I'm quite uneasy to have the
> >> test failing if a class is added or removed from the element. But yes,
> >> your solution work for the current state of the application.
> >>
> >> 2016-07-31 14:46 GMT+02:00 Andreas Kuhne :
> >> > 2016-07-31 13:56 GMT+02:00 ludovic coues :
> >> >>
> >> >> First, thanks for the suggestion.
> >> >>
> >> >> I just tried that, didn't work.
> >> >> Here is the test file I used:
> >> >>
> >> >>
> >> >> from django.test import TestCase
> >> >>
> >> >> class HTMLTestCase(TestCase):
> >> >>
> >> >> def test_input_in_fieldset(self):
> >> >> fieldset = """
> >> >> 
> >> >> 
> >> >>  >> >> maxlength="254" name="username" rows="3" type="text" required />
> >> >> 
> >> >> """
> >> >> self.assertInHTML('', fieldset)
> >> >> self.assertInHTML('', fieldset)
> >> >>
> >> >>
> >> >> First input is to have a working exemple, second is taken as is from
> >> >> my view. Not closing the input in assertInHTML give an error
> `Couldn't
> >> >> find ' same
> >> >> errors with assertContains(*args, html=True)
> >> >>
> >> >> I could copy/past the input directly in my test but if the maxlength
> >> >> or class attribute change, the test will break. Taking the input
> >> >> directly from the django form will test if the django form is
> rendered
> >> >> in the view, not if the view is displaying a suitable form.
> >> >>
> >> >> 2016-07-31 13:22 GMT+02:00 Andreas Kuhne  >:
> >> >> > 2016-07-31 12:38 GMT+02:00 ludovic coues :
> >> >> >>
> >> >> >> 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, "",
> >> >> >> 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+unsubscr...@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 

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

2016-07-31 Thread ludovic coues
Currently, I am using lxml. More dependencies but it's the cleanest
method I've found currently.
I use it like that:

from django.test import TestCase
from lxml import etree

class FormTest(TestCase):
def test_input(self):
response = self.client.get('/accounts/login')
self.assertEqual(response.status_code, 200)
doc = etree.HTML(response.content)
self.assertEqual(len(doc.findall('.//input[@name="username"]')), 1)



2016-07-31 17:46 GMT+02:00 Andreas Kuhne :
> 2016-07-31 15:59 GMT+02:00 ludovic coues :
>>
>> Oh, sorry. A bit of misunderstanding and miscommunication on my part.
>>
>> The exemple I gave is just a quick way to reproduce my problem. The
>> real test use self.client, reverse, cast response.content to a string.
>> What I gave is a minimal exemple.
>>
>> Also, I assumed you talked about comparing bit of html element, not
>> the raw content returned by the view. I'm quite uneasy to have the
>> test failing if a class is added or removed from the element. But yes,
>> your solution work for the current state of the application.
>>
>> 2016-07-31 14:46 GMT+02:00 Andreas Kuhne :
>> > 2016-07-31 13:56 GMT+02:00 ludovic coues :
>> >>
>> >> First, thanks for the suggestion.
>> >>
>> >> I just tried that, didn't work.
>> >> Here is the test file I used:
>> >>
>> >>
>> >> from django.test import TestCase
>> >>
>> >> class HTMLTestCase(TestCase):
>> >>
>> >> def test_input_in_fieldset(self):
>> >> fieldset = """
>> >> 
>> >> 
>> >> > >> maxlength="254" name="username" rows="3" type="text" required />
>> >> 
>> >> """
>> >> self.assertInHTML('', fieldset)
>> >> self.assertInHTML('', fieldset)
>> >>
>> >>
>> >> First input is to have a working exemple, second is taken as is from
>> >> my view. Not closing the input in assertInHTML give an error `Couldn't
>> >> find '> >> errors with assertContains(*args, html=True)
>> >>
>> >> I could copy/past the input directly in my test but if the maxlength
>> >> or class attribute change, the test will break. Taking the input
>> >> directly from the django form will test if the django form is rendered
>> >> in the view, not if the view is displaying a suitable form.
>> >>
>> >> 2016-07-31 13:22 GMT+02:00 Andreas Kuhne :
>> >> > 2016-07-31 12:38 GMT+02:00 ludovic coues :
>> >> >>
>> >> >> 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, "",
>> >> >> 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+unsubscr...@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 '> >> > 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, '> >> >
>> >> > --
>> >> > You received this message because you are subscribed to the Google
>> >> > Groups
>> >> > "Django users" group.
>> >> > To 

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

2016-07-31 Thread Andreas Kuhne
2016-07-31 15:59 GMT+02:00 ludovic coues :

> Oh, sorry. A bit of misunderstanding and miscommunication on my part.
>
> The exemple I gave is just a quick way to reproduce my problem. The
> real test use self.client, reverse, cast response.content to a string.
> What I gave is a minimal exemple.
>
> Also, I assumed you talked about comparing bit of html element, not
> the raw content returned by the view. I'm quite uneasy to have the
> test failing if a class is added or removed from the element. But yes,
> your solution work for the current state of the application.
>
> 2016-07-31 14:46 GMT+02:00 Andreas Kuhne :
> > 2016-07-31 13:56 GMT+02:00 ludovic coues :
> >>
> >> First, thanks for the suggestion.
> >>
> >> I just tried that, didn't work.
> >> Here is the test file I used:
> >>
> >>
> >> from django.test import TestCase
> >>
> >> class HTMLTestCase(TestCase):
> >>
> >> def test_input_in_fieldset(self):
> >> fieldset = """
> >> 
> >> 
> >>  >> maxlength="254" name="username" rows="3" type="text" required />
> >> 
> >> """
> >> self.assertInHTML('', fieldset)
> >> self.assertInHTML('', fieldset)
> >>
> >>
> >> First input is to have a working exemple, second is taken as is from
> >> my view. Not closing the input in assertInHTML give an error `Couldn't
> >> find ' >> errors with assertContains(*args, html=True)
> >>
> >> I could copy/past the input directly in my test but if the maxlength
> >> or class attribute change, the test will break. Taking the input
> >> directly from the django form will test if the django form is rendered
> >> in the view, not if the view is displaying a suitable form.
> >>
> >> 2016-07-31 13:22 GMT+02:00 Andreas Kuhne :
> >> > 2016-07-31 12:38 GMT+02:00 ludovic coues :
> >> >>
> >> >> 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, "",
> >> >> 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+unsubscr...@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 ' >> > 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, ' >> >
> >> > --
> >> > 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+unsubscr...@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.
> >>
> >>
> >>
> >> --
> >>
> >> Cordialement, Coues Ludovic
> >> +336 148 743 42
> >>
> >> --
> >> You received this message because you are subscribed to the Google
> Groups

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

2016-07-31 Thread ludovic coues
Oh, sorry. A bit of misunderstanding and miscommunication on my part.

The exemple I gave is just a quick way to reproduce my problem. The
real test use self.client, reverse, cast response.content to a string.
What I gave is a minimal exemple.

Also, I assumed you talked about comparing bit of html element, not
the raw content returned by the view. I'm quite uneasy to have the
test failing if a class is added or removed from the element. But yes,
your solution work for the current state of the application.

2016-07-31 14:46 GMT+02:00 Andreas Kuhne :
> 2016-07-31 13:56 GMT+02:00 ludovic coues :
>>
>> First, thanks for the suggestion.
>>
>> I just tried that, didn't work.
>> Here is the test file I used:
>>
>>
>> from django.test import TestCase
>>
>> class HTMLTestCase(TestCase):
>>
>> def test_input_in_fieldset(self):
>> fieldset = """
>> 
>> 
>> > maxlength="254" name="username" rows="3" type="text" required />
>> 
>> """
>> self.assertInHTML('', fieldset)
>> self.assertInHTML('', fieldset)
>>
>>
>> First input is to have a working exemple, second is taken as is from
>> my view. Not closing the input in assertInHTML give an error `Couldn't
>> find '> errors with assertContains(*args, html=True)
>>
>> I could copy/past the input directly in my test but if the maxlength
>> or class attribute change, the test will break. Taking the input
>> directly from the django form will test if the django form is rendered
>> in the view, not if the view is displaying a suitable form.
>>
>> 2016-07-31 13:22 GMT+02:00 Andreas Kuhne :
>> > 2016-07-31 12:38 GMT+02:00 ludovic coues :
>> >>
>> >> 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, "",
>> >> 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+unsubscr...@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 '> > 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, '> >
>> > --
>> > 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+unsubscr...@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.
>>
>>
>>
>> --
>>
>> 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+unsubscr...@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 

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

2016-07-31 Thread Andreas Kuhne
2016-07-31 13:56 GMT+02:00 ludovic coues :

> First, thanks for the suggestion.
>
> I just tried that, didn't work.
> Here is the test file I used:
>
>
> from django.test import TestCase
>
> class HTMLTestCase(TestCase):
>
> def test_input_in_fieldset(self):
> fieldset = """
> 
> 
>  maxlength="254" name="username" rows="3" type="text" required />
> 
> """
> self.assertInHTML('', fieldset)
> self.assertInHTML('', fieldset)
>
>
> First input is to have a working exemple, second is taken as is from
> my view. Not closing the input in assertInHTML give an error `Couldn't
> find ' errors with assertContains(*args, html=True)
>
> I could copy/past the input directly in my test but if the maxlength
> or class attribute change, the test will break. Taking the input
> directly from the django form will test if the django form is rendered
> in the view, not if the view is displaying a suitable form.
>
> 2016-07-31 13:22 GMT+02:00 Andreas Kuhne :
> > 2016-07-31 12:38 GMT+02:00 ludovic coues :
> >>
> >> 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, "",
> >> 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+unsubscr...@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 ' > 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, ' >
> > --
> > 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+unsubscr...@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.
>
>
>
> --
>
> 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+unsubscr...@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%2BTZCWDkkrZuP2N168pth1hL-gc8FrVjMwZnb3xS%2BK1X%2Bxw%40mail.gmail.com
> .
> For more options, visit https://groups.google.com/d/optout.
>

Hi,

I think you have misunderstood me.

First you need to check against the real response object, otherwise your
test will only test if the item is present in your string, which is not
what I meant. I meant that you should check what HTML django is generating
to then get the correct information.

In your case, I am now guessing that the output of django is the
information you put into the fieldset variable?

If so, you need to write
self.assertContains(response,'https://groups.google.com/group/django-users.
To view 

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

2016-07-31 Thread ludovic coues
First, thanks for the suggestion.

I just tried that, didn't work.
Here is the test file I used:


from django.test import TestCase

class HTMLTestCase(TestCase):

def test_input_in_fieldset(self):
fieldset = """




"""
self.assertInHTML('', fieldset)
self.assertInHTML('', fieldset)


First input is to have a working exemple, second is taken as is from
my view. Not closing the input in assertInHTML give an error `Couldn't
find ':
> 2016-07-31 12:38 GMT+02:00 ludovic coues :
>>
>> 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, "",
>> 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+unsubscr...@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 ' 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, '
> --
> 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+unsubscr...@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.



-- 

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+unsubscr...@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%2BTZCWDkkrZuP2N168pth1hL-gc8FrVjMwZnb3xS%2BK1X%2Bxw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


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

2016-07-31 Thread Andreas Kuhne
2016-07-31 12:38 GMT+02:00 ludovic coues :

> 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, "",
> 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+unsubscr...@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 '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.