gazpacho icon indicating copy to clipboard operation
gazpacho copied to clipboard

"in" support

Open adamchainz opened this issue 5 years ago • 2 comments

Is your feature request related to a problem? Please describe.

I'd like to use gazpacho for testing the HTML output of my Django application. A natural way to do this would be to search for a given HTML chunk. Therefore I'd like to be able to use the in operator on a Soup to check for that HTML chunk. This probably has utility outside of tests.

Describe the solution you'd like

I imagine this working something like:

response = self.client.get('/')  # django's test client
assert response.status_code == HTTPStatus.OK
body = response.content.decode()
assert '<h1><a href="/">Home page</a></h1>' in Soup(body)

Describe alternatives you've considered

It's possible to reproduce this with find() and making assertions on the contents of the found node, but much more complicated since it requires assertions for each node in the tree.

Additional information

Django already supports a similar assertion called assertInHTML. However this relies on normalizing the HTML text and making text assertions, so it's clunky around matching the actual elements.

adamchainz avatar Jan 12 '21 14:01 adamchainz

Interesting idea!

I don't think I fully understand, though, why would:

assert '<h1><a href="/">Home page</a></h1>' in str(Soup(body))

Not work?

maxhumber avatar Jan 13 '21 17:01 maxhumber

If there are two attributes in the HTML, what order will they come out of Soup ? What about whitespace, etc.?

adamchainz avatar Jan 13 '21 21:01 adamchainz