microsoft / playwright-python

Python version of the Playwright testing and automation library.
https://playwright.dev/python/
Apache License 2.0
11.9k stars 911 forks source link

[Feature] Add soft assertions #1272

Open heathiviesyncro opened 2 years ago

heathiviesyncro commented 2 years ago

It would be nice to get the soft assertions that are available in node to be available in python.

https://playwright.dev/docs/test-assertions#soft-assertions

Thank you

vlado48 commented 2 years ago

Any idea how to implement this now? Try/Except with short timeout or is there a better way?

aspenboy commented 1 year ago

I am using pytest-check for soft assertions. Adds another dependency to the project, but it works great.

jfp1992 commented 1 year ago

I am using pytest-check for soft assertions. Adds another dependency to the project, but it works great.

I tried this one but couldn't get it working with expect(). Do you have an example of this?

Also +1 for soft assertions to be implemented in python as well as typescript.

aspenboy commented 1 year ago

I tried this one but couldn't get it working with expect(). Do you have an example of this?

It's a separate assertion mechanism, so you don't use it within expect(). Pretty nice examples are included on the github page: https://github.com/okken/pytest-check

Also - you can mix assertions in your tests, so it's fine to use assert, expect() and check().

mbroton commented 1 week ago

I agree, it would be really useful to have soft assertions in Python version of Playwright. Right now I'm using workarounds in some of my projects, similar to what was already mentioned above.

I spent some time exploring the code and came up with an approach that seems to work. Here’s an example of how it looks:

def test_foo(page: Page) -> None:
    # ...
    with soft_assertions() as expect:
        expect(page.locator("div")).to_have_text("foo")
        expect(page.locator("div")).to_have_text("bar")

expect in this example works just like a normal expect (works with pages, locators and api responses).

At first, I considered an expect.soft(...) approach like in Node, but I couldn’t find a solution that didn’t require a lot of changes.

Would love any feedback on this!