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

SoftAssertions Implementation #1340

Merged
merged 4 commits into from
Aug 17, 2023
Merged

Conversation

uchagani
Copy link
Contributor

@uchagani uchagani commented Jul 22, 2023

Feedback requested:

I took a stab at implementing soft assertions (issue #819). Before I continue, I'd love to get some feedback on if this is an acceptable approach.

I have implemented PageAssertions so far. Please take a look at TestSoftPageAssertions.java for what the usage would be. I used most of the existing test cases to exercise the method via soft assertions.

I believe this approach takes the least intrusive route (No existing playwright code was modified).

@uchagani uchagani changed the title Initial commit of SoftAssertions SoftAssertions Implementation Jul 22, 2023
@uchagani uchagani marked this pull request as ready for review July 25, 2023 18:34
@yury-s
Copy link
Member

yury-s commented Aug 11, 2023

Hi @uchagani, sorry for long delay. Your approach looks sensible and we can accept your implementation of soft assertions into this repo.

A few notes about the implementation:

  1. The API interfaces for Java (and all other language ports) are generated from the documentation sources that live upstream, e.g. PageAssertions. PageAssertions needs to be generated in a similar fashion. This way it will "automatically" appear on playwright.dev. For the latter, you may need to tweak format_java.js to make sure the documentation looks nice. Feel free to ask any questions as that code is quite convoluted.
  2. All implementation classes should go under src/main/java/com/microsoft/playwright/impl.

Starting with PageAssertions is a good idea, let's worked out it first and then proceed with the locator assertions.


@Override
public PageAssertions not() {
return new PageAssertionsImplProxy(super.results, (PageAssertionsImpl) pageAssertions.not());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just change the return type of PageAssertionsImpl.not() to PageAssertionsImpl.

import java.util.List;
import java.util.regex.Pattern;

class PageAssertionsProxy extends SoftAssertionsBase implements PageAssertions {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this class is subsumed by PageAssertionsImplProxy and can be deleted.

import java.util.List;

class SoftAssertionsImpl implements SoftAssertions {
final List<Throwable> results;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would use RuntimeException here and in PageAssertionsImplProxy and SoftAssertionsBase. We don't want to catch any Errors anyway and only catch unchecked exceptions in SoftAssertionsBase anyway.

Copy link
Contributor Author

@uchagani uchagani Aug 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yury-s, we catch AssertionFailedError which is an Error

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, didn't know that. Then Throwable is justified.

@uchagani
Copy link
Contributor Author

@yury-s I created a class-softassertions.md file but when i run roll I get the following error:

Error: Unexpected assertion class SoftAssertions
    at assertionArgument (/Users/umairchagani/dev/playwright.dev/src/generator.js:753:9)
    at JavaFormatter.formatMember (/Users/umairchagani/dev/playwright.dev/src/format_java.js:42:30)
    at Generator.createMemberLink (/Users/umairchagani/dev/playwright.dev/src/generator.js:506:27)
    at Generator.visitClassToc (/Users/umairchagani/dev/playwright.dev/src/generator.js:514:12)
    at Generator.generateClassDoc (/Users/umairchagani/dev/playwright.dev/src/generator.js:174:10)
    at new Generator (/Users/umairchagani/dev/playwright.dev/src/generator.js:140:12)
    at generateDocsForLanguages (/Users/umairchagani/dev/playwright.dev/src/generate.js:103:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async /Users/umairchagani/dev/playwright.dev/src/generate.js:156:5

How do i get it playwright to run with my new additions?

@yury-s
Copy link
Member

yury-s commented Aug 14, 2023

@yury-s I created a class-softassertions.md file but when i run roll I get the following error:

Error: Unexpected assertion class SoftAssertions
    at assertionArgument (/Users/umairchagani/dev/playwright.dev/src/generator.js:753:9)
    at JavaFormatter.formatMember (/Users/umairchagani/dev/playwright.dev/src/format_java.js:42:30)
    at Generator.createMemberLink (/Users/umairchagani/dev/playwright.dev/src/generator.js:506:27)
    at Generator.visitClassToc (/Users/umairchagani/dev/playwright.dev/src/generator.js:514:12)
    at Generator.generateClassDoc (/Users/umairchagani/dev/playwright.dev/src/generator.js:174:10)
    at new Generator (/Users/umairchagani/dev/playwright.dev/src/generator.js:140:12)
    at generateDocsForLanguages (/Users/umairchagani/dev/playwright.dev/src/generate.js:103:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async /Users/umairchagani/dev/playwright.dev/src/generate.js:156:5

How do i get it playwright to run with my new additions?

Do you have the change uploaded somewhere? It might be that you need to update some of the generator scripts, I can try running it locally and help you.

Update: yeah, it looks like you need to update the generator around format_java.js:42 to support SoftAssertion class. Probably you can set empty prefix similar to if (member.clazz.varName === 'playwrightAssertions') { code a few lines above.

@uchagani
Copy link
Contributor Author

uchagani commented Aug 14, 2023

@yury-s here is my branch with the markdown (https://github.com/uchagani/playwright/tree/java-soft-assertions). I'll take a look at your suggestion about changing format_java

@uchagani
Copy link
Contributor Author

uchagani commented Aug 15, 2023

@yury-s so i added a || member.clazz.varName === 'softAssertions' to line 39 and now I can see that it created a class-softassertions.mdx file but i can't seem to see it when i run the dev server via npm run start-java is there something else I need to do?

image

image

@yury-s
Copy link
Member

yury-s commented Aug 15, 2023

but i can't seem to see it when i run the dev server via npm run start-java is there something else I need to do?

I usually just follow the instructions and that's enough. Note that the new page will not show up in the sidebar automatically, you need to manually edit https://github.com/microsoft/playwright.dev/blob/main/java/sidebars.js, but you should be able to navigate to the page by typing its url. I can try to apply your patch locally later today if it still doesn't work.

@uchagani
Copy link
Contributor Author

Ah ok I didn't know it wouldn't show up in the sidebar and I had to navigate to it. I'll try that in a bit.

@uchagani
Copy link
Contributor Author

@yury-s so i can't navigate to it either. I think i'm doing something wrong because when I add it to the sidebar under assertions, none of the assertion sidebar items show up.

image

@uchagani
Copy link
Contributor Author

Here is my playwright.dev branch: https://github.com/uchagani/playwright.dev/tree/java-soft-assertions

@yury-s
Copy link
Member

yury-s commented Aug 16, 2023

@yury-s so i can't navigate to it either. I think i'm doing something wrong because when I add it to the sidebar under assertions, none of the assertion sidebar items show up.

By default it shows stable docs which don't contain the new page. You need to navigate to http://localhost:3000/java/docs/next/api/class-softassertions (note /next/ in the URL, it points to the dev version of the docs). You can press Shift 5 times to stick with the next docs on that page.

I ran your patches locally and I can see the page.

@uchagani
Copy link
Contributor Author

that worked! thanks!

@uchagani
Copy link
Contributor Author

@yury-s PRs created for both the main playwright repo and playwright.dev. Is there anything else I need to do before the new API interface will be available for this PR?

@uchagani
Copy link
Contributor Author

@yury-s how does the api generator know to make a method static?

@uchagani
Copy link
Contributor Author

https://github.com/microsoft/playwright-java/blob/main/tools/api-generator/src/main/java/com/microsoft/playwright/tools/ApiGenerator.java#L685 found it. i guess we'll have to add something here too to handle the static create method. I can add it once the other PRs are merged.

@yury-s
Copy link
Member

yury-s commented Aug 17, 2023

I'll commit this right-away to unblock you. Feel free to address any comments in a follow-up PR.

@yury-s yury-s merged commit 632fba5 into microsoft:main Aug 17, 2023
@yury-s
Copy link
Member

yury-s commented Aug 17, 2023

Oops, wrong PR, I meant the one upstream, sorry 😄

yury-s pushed a commit to microsoft/playwright that referenced this pull request Aug 17, 2023
This PR adds the `SoftAssertions` API for Java. Related PR:
microsoft/playwright-java#1340

Note: microsoft/playwright.dev#1135 needs to be
merged in order for the markdown in this PR to be rendered without
errors
yury-s added a commit to yury-s/playwright-java that referenced this pull request Aug 17, 2023
yury-s added a commit that referenced this pull request Aug 17, 2023
Germandrummer92 pushed a commit to OctoMind-dev/playwright that referenced this pull request Oct 27, 2023
This PR adds the `SoftAssertions` API for Java. Related PR:
microsoft/playwright-java#1340

Note: microsoft/playwright.dev#1135 needs to be
merged in order for the markdown in this PR to be rendered without
errors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants