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

(feat): add visual matcher #141

Merged
merged 13 commits into from
Jan 11, 2024
Merged

(feat): add visual matcher #141

merged 13 commits into from
Jan 11, 2024

Conversation

christian-bromann
Copy link
Member

@christian-bromann christian-bromann commented Jan 7, 2024

First draft of visual matchers. Needs some testing and documentation.

I decided for this interface:

it(`should match an element successful with a baseline for '${browserName}'`, async () => {
    await expect($('.hero__title-logo')).toMatchElementSnapshot('wdioLogo', 0, {
        removeElements: [await $('nav.navbar')]
    })
})

it(`should match a full page screenshot successful with a baseline for '${browserName}'`, async () => {
    await expect(browser).toMatchFullPageSnapshot('fullPage', 0, {
        fullPageScrollTimeout: 1500,
        hideAfterFirstScroll: [
            await $('nav.navbar'),
        ],
    })
})

it(`should match a tabbable screenshot successful with a baseline for '${browserName}'`, async () => {
    await expect(browser).toMatchTabbablePageSnapshot('tabbable', 0, {
        hideAfterFirstScroll: [
            await $('nav.navbar'),
        ]
    })
})

implements #140

Copy link

changeset-bot bot commented Jan 7, 2024

🦋 Changeset detected

Latest commit: 540311a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@wdio/visual-service Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@christian-bromann christian-bromann linked an issue Jan 7, 2024 that may be closed by this pull request
@christian-bromann christian-bromann marked this pull request as ready for review January 8, 2024 04:47
@christian-bromann
Copy link
Member Author

@wswebcreation let me know what you think!

@christian-bromann
Copy link
Member Author

Add docs for them in webdriverio/expect-webdriverio#1409

@wswebcreation
Copy link
Member

Add docs for them in webdriverio/expect-webdriverio#1409

Done this one, added comments to it

Copy link
Member

@wswebcreation wswebcreation left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@erwinheitzman
Copy link
Member

I would like the ability the expectation in the service settings so that I do not have to pass 0 every time I do a comparison, so making the number an optional instead of required argument. What do you think?

@christian-bromann
Copy link
Member Author

What do you think?

Great idea, will add the feature.

@christian-bromann
Copy link
Member Author

christian-bromann commented Jan 11, 2024

Improved the matcher failing message:

 "spec" Reporter:
------------------------------------------------------------------
[Safari 16.6 macOS #0-0] Running: Safari (v16.6) on macOS
[Safari 16.6 macOS #0-0] Session ID: 14b39b5f154444faa0970b43c6e36565
[Safari 16.6 macOS #0-0]
[Safari 16.6 macOS #0-0] » /tests/specs/desktop.spec.ts
[Safari 16.6 macOS #0-0] @wdio/visual-service desktop
[Safari 16.6 macOS #0-0]    ✖ should compare an element successful with a baseline for 'safari-16'
[Safari 16.6 macOS #0-0]    ✓ should compare a full page screenshot successful with a baseline for 'safari-16'
[Safari 16.6 macOS #0-0]    ✓ should compare a tabbable screenshot successful with a baseline for 'safari-16'
[Safari 16.6 macOS #0-0]
[Safari 16.6 macOS #0-0] 2 passing (56.5s)
[Safari 16.6 macOS #0-0] 1 failing
[Safari 16.6 macOS #0-0]
[Safari 16.6 macOS #0-0] 1) @wdio/visual-service desktop should compare an element successful with a baseline for 'safari-16'
[Safari 16.6 macOS #0-0] Expected image to have a mismatch percentage of 0%, but was 46.878%
Please compare the images manually and update the baseline if the new screenshot is correct.

Baseline: /Users/christian.bromann/Sites/WebdriverIO/projects/visual-testing/tests/sauceLabsBaseline/desktop_safari/wdioLogo-macOS12-16-1366x768.png
Actual Screenshot: /Users/christian.bromann/Sites/WebdriverIO/projects/visual-testing/.tmp/actual/desktop_safari/wdioLogo-macOS12-16-1366x768.png
Difference: /Users/christian.bromann/Sites/WebdriverIO/projects/visual-testing/.tmp/diff/desktop_safari/wdioLogo-macOS12-16-1366x768.png

See https://webdriver.io/docs/api/visual-regression.html for more information.

@christian-bromann
Copy link
Member Author

christian-bromann commented Jan 11, 2024

Assertions can now be written without an expected result, it defaults to 0:

await expect($('.hero__title-logo')).toMatchElementSnapshot('wdioLogo', {
    removeElements: [await $('nav.navbar')]
})
// or just
await expect($('.hero__title-logo')).toMatchElementSnapshot('wdioLogo')

@christian-bromann
Copy link
Member Author

Let me know what you all think!

/**
* overwrite `returnAllCompareData` to allow us to provide a better assertion message
*/
options.returnAllCompareData = true
Copy link
Member

Choose a reason for hiding this comment

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

👍

if (typeof expected === 'number') {
return {
pass: result.misMatchPercentage === expected,
message: () => (
Copy link
Member

Choose a reason for hiding this comment

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

❤️

Copy link
Member

@wswebcreation wswebcreation left a comment

Choose a reason for hiding this comment

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

Thanks

@christian-bromann christian-bromann merged commit 474c90f into main Jan 11, 2024
4 checks passed
@christian-bromann christian-bromann deleted the cb/visual-matcher branch January 11, 2024 06:53
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.

[💡 Feature]: create custom matchers for visual testing
3 participants