Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Add soft assertions #1272

Open
heathiviesyncro opened this issue Apr 25, 2022 · 5 comments
Open

[Feature] Add soft assertions #1272

heathiviesyncro opened this issue Apr 25, 2022 · 5 comments

Comments

@heathiviesyncro
Copy link

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

@mxschmitt mxschmitt changed the title Can we please get soft assertions added to the python tools [Feature] Add soft assertions Jun 1, 2022
@vlado48
Copy link

vlado48 commented Jun 2, 2022

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

@aspenboy
Copy link

aspenboy commented Dec 7, 2022

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

@jfp1992
Copy link

jfp1992 commented Sep 27, 2023

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
Copy link

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
Copy link

mbroton commented Nov 10, 2024

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants