简体   繁体   中英

Django Unit Testing: CSS selectors to test HTML?

There's something I discovered about the Rails framework which I really like, the ability to test the output of templates really easily with css selector, for example test how many "li" elements are in a page, if an element with a certain ID is present, ecc.

AssertContains feels really limited compared to these instruments, check them out: http://guides.rubyonrails.org/testing.html#testing-views

How to achieve something similar with django? Is there a python library that implements CSS selectors?

django-with-asserts provides a TestCase mixin which allows you to test HTML like:

with self.assertHTML(resp, 'input[name="email"]') as (elem,):
    self.assertEqual(elem.value, 'bob@example.com')

You can use Selenium to find elements using CSS selectors, xpath, etc. The driver is specific to a browser, like the Firefox driver, Chrome driver, etc, but it's about the closest you'll get in Django.

See: https://docs.djangoproject.com/en/1.4/topics/testing/#django.test.LiveServerTestCase for more information on the LiveServerTestCase.

and: http://selenium-python.readthedocs.org/en/latest/locating-elements.html for the selectors available in Selenium.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM