Skip to content

✨ Add additional snapshot command exec helpers#922

Merged
wwilsman merged 7 commits intomasterfrom
ww/add-exec-helpers
May 13, 2022
Merged

✨ Add additional snapshot command exec helpers#922
wwilsman merged 7 commits intomasterfrom
ww/add-exec-helpers

Conversation

@wwilsman
Copy link
Copy Markdown
Contributor

@wwilsman wwilsman commented May 12, 2022

What is this?

This PR adds additional helpers provided with scripts evaluated on pages using the core browser.

In practice, these helpers are available to any execute option when using the percy snapshot command. For our own internal use, we can utilize these helpers when given their associated snapshot option (like waitForSelector). We could also potential use them while scraping for snapshots in other SDKs (like Storybook).

While addressing these helpers and in making them publicly accessible, I also decided to address how the current generatePromise function works. Currently, it returns a promise-like generator that can be "canceled" and have registered handlers called. However, only one place (besides tests) actually uses this feature: the queue. After updating the util, the queue was updated to utilize replacement utils instead.

Will close #857

Helpers:

  • generatePromise(generator[, signal][, callback]) - Updated to accept an AbortSignal, available in browser environments and later Node versions. However for some current versions of Node, a polyfill is required to support the minimum features needed to cancel a generator loop. The generatePromise util can now also receive an optional Node-style callback argument, which is utilized by the queue.

  • waitFor(predicate[, options]) - Split into two separate functions. A new util, yieldFor, which is an async generator function that behaves exactly the same as waitFor. The main difference is instead of running automatically, it runs the predicate check only when iterated. The existing waitFor util is now just a wrapper for generatePromise(yieldFor(...)).

  • waitForTimeout(timeout[, value]) - A promisified version of setTimeout (without a callback). The first argument is the timeout and the second optional argument is the value to resolve.

  • waitForSelector(selector[, timeout]) - Alias for waitFor(() => document.querySelector(selector), timeout).

  • waitForXPath(selector[, timeout]) - Like waitForSelector(...), but uses document.evaulate(...) instead.

  • scrollToBottom([options][, onScroll]) - A browser util to scroll to the bottom of the page, one window at a time. Any provided options are passed along to window.scrollTo(...), and any provided onScroll function will be awaited on after each window segment is scrolled.

  • config - An object containing global Percy config options. Also available as window.__PERCY__.config.

  • snapshot - An object containing the current snapshot options. Only available when execute options are provided. Also available as window.__PERCY__.snapshot.

Also changed in this PR:

The change from "cancelable" generators to AbortController highlighted that our intent was always to abort (cancel implies it has not yet started). With this, I also updated @percy/cli-command to use an AbortController for dealing with process signals. In combination with the new generatePromise util, the code for process signal handling was actually cut back significantly. However, any code that previously relied on error.canceled needed to be updated to check the error name, which is the recommended way for checking for an AbortError (emitted by an AbortSignal).

In order to get a global config reference in the right place, the browser class constructor was refactored slightly to accept a core percy instance. Other constructor code was moved into the launch method, and some process code was moved into a spawn method.

To Do:

These helpers should be publicly documented. The snapshot command itself also lacks good documentation, so this could be an opportunity to write better documentation around that in general.

wwilsman added 3 commits May 12, 2022 14:14
The generatePromise function now accepts an abort signal and node-style callback. Rather than
returning a promise-like generator, it now always returns a promise.

A new AbortController polyfill was added for Node environments to support providing the
generatorPromise function with an abort signal.

A yieldFor function replaces the previous waitFor function with the same basic functionality;
yielding on every iteration to the provided predicate function.

The waitFor function is now a shortcut for generatePromise(yieldFor(...args)).

A new waitForTimeout function was added in preperation of more exec utils.
@wwilsman wwilsman added the ✨ enhancement New feature or request label May 12, 2022
@wwilsman wwilsman requested a review from Robdel12 May 12, 2022 21:14
@wwilsman wwilsman force-pushed the ww/add-exec-helpers branch from 5a05edf to c78d820 Compare May 12, 2022 21:32
These are not reset during hooks, so they can leak between tests when redefined
@wwilsman wwilsman force-pushed the ww/add-exec-helpers branch from 7462235 to cc7bdb1 Compare May 12, 2022 21:46
Copy link
Copy Markdown
Contributor

@Robdel12 Robdel12 left a comment

Choose a reason for hiding this comment

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

🏁 Slick stuff! Documenting this will be really nice and give folks using snapshot methods (the CLI, storybook, etc) will love these helpers 🔥

@wwilsman wwilsman merged commit 0f4575e into master May 13, 2022
@wwilsman wwilsman deleted the ww/add-exec-helpers branch May 13, 2022 16:51
kodiakhq bot referenced this pull request in carbon-design-system/carbon-for-ibm-dotcom May 24, 2022
[![WhiteSource Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [@percy/cli](https://togithub.com/percy/cli) | [`~1.1.0` -> `~1.2.0`](https://renovatebot.com/diffs/npm/@percy%2fcli/1.1.0/1.2.1) | [![age](https://badges.renovateapi.com/packages/npm/@percy%2fcli/1.2.1/age-slim)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://badges.renovateapi.com/packages/npm/@percy%2fcli/1.2.1/adoption-slim)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://badges.renovateapi.com/packages/npm/@percy%2fcli/1.2.1/compatibility-slim/1.1.0)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://badges.renovateapi.com/packages/npm/@percy%2fcli/1.2.1/confidence-slim/1.1.0)](https://docs.renovatebot.com/merge-confidence/) |
| [@percy/dom](https://togithub.com/percy/cli) | [`~1.1.0` -> `~1.2.0`](https://renovatebot.com/diffs/npm/@percy%2fdom/1.1.0/1.2.1) | [![age](https://badges.renovateapi.com/packages/npm/@percy%2fdom/1.2.1/age-slim)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://badges.renovateapi.com/packages/npm/@percy%2fdom/1.2.1/adoption-slim)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://badges.renovateapi.com/packages/npm/@percy%2fdom/1.2.1/compatibility-slim/1.1.0)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://badges.renovateapi.com/packages/npm/@percy%2fdom/1.2.1/confidence-slim/1.1.0)](https://docs.renovatebot.com/merge-confidence/) |
| [@percy/sdk-utils](https://togithub.com/percy/cli) | [`~1.1.0` -> `~1.2.0`](https://renovatebot.com/diffs/npm/@percy%2fsdk-utils/1.1.0/1.2.1) | [![age](https://badges.renovateapi.com/packages/npm/@percy%2fsdk-utils/1.2.1/age-slim)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://badges.renovateapi.com/packages/npm/@percy%2fsdk-utils/1.2.1/adoption-slim)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://badges.renovateapi.com/packages/npm/@percy%2fsdk-utils/1.2.1/compatibility-slim/1.1.0)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://badges.renovateapi.com/packages/npm/@percy%2fsdk-utils/1.2.1/confidence-slim/1.1.0)](https://docs.renovatebot.com/merge-confidence/) |

---

### Release Notes

<details>
<summary>percy/cli</summary>

### [`v1.2.1`](https://togithub.com/percy/cli/releases/tag/v1.2.1)

[Compare Source](https://togithub.com/percy/cli/compare/v1.2.0...v1.2.1)

<!-- Release notes generated using configuration in .github/release.yml at master -->

#### What's Changed

##### 🐛 Bug Fixes

-   🐛 Fix sitemap xml parsing regex (stop when the lookahead matches) by [@&#8203;Robdel12](https://togithub.com/Robdel12) in [https://github.com/percy/cli/pull/928](https://togithub.com/percy/cli/pull/928)

**Full Changelog**: percy/cli@v1.2.0...v1.2.1

### [`v1.2.0`](https://togithub.com/percy/cli/releases/tag/v1.2.0)

[Compare Source](https://togithub.com/percy/cli/compare/v1.1.4...v1.2.0)

<!-- Release notes generated using configuration in .github/release.yml at master -->

#### What's Changed

##### ✨ Enhancements

-   ✨ Add additional snapshot command exec helpers by [@&#8203;wwilsman](https://togithub.com/wwilsman) in [https://github.com/percy/cli/pull/922](https://togithub.com/percy/cli/pull/922)

##### 🐛 Bug Fixes

-   🐛 Fix regex matching accepting non-regex patterns by [@&#8203;wwilsman](https://togithub.com/wwilsman) in [https://github.com/percy/cli/pull/923](https://togithub.com/percy/cli/pull/923)

##### ⬆️⬇️ Dependency Updates

-   ⬆️ Bump karma from 6.3.19 to 6.3.20 by [@&#8203;dependabot](https://togithub.com/dependabot) in [https://github.com/percy/cli/pull/927](https://togithub.com/percy/cli/pull/927)
-   ⬆️ Bump yaml from 2.0.1 to 2.1.0 by [@&#8203;dependabot](https://togithub.com/dependabot) in [https://github.com/percy/cli/pull/925](https://togithub.com/percy/cli/pull/925)
-   ⬆️ Bump karma-jasmine from 5.0.0 to 5.0.1 by [@&#8203;dependabot](https://togithub.com/dependabot) in [https://github.com/percy/cli/pull/926](https://togithub.com/percy/cli/pull/926)

**Full Changelog**: percy/cli@v1.1.4...v1.2.0

### [`v1.1.4`](https://togithub.com/percy/cli/releases/tag/v1.1.4)

[Compare Source](https://togithub.com/percy/cli/compare/v1.1.3...v1.1.4)

<!-- Release notes generated using configuration in .github/release.yml at master -->

#### What's Changed

##### 🐛 Bug Fixes

-   🐛 Request fonts as binary via Node by [@&#8203;Robdel12](https://togithub.com/Robdel12) in [https://github.com/percy/cli/pull/921](https://togithub.com/percy/cli/pull/921)

**Full Changelog**: percy/cli@v1.1.3...v1.1.4

### [`v1.1.3`](https://togithub.com/percy/cli/releases/tag/v1.1.3)

[Compare Source](https://togithub.com/percy/cli/compare/v1.1.2...v1.1.3)

<!-- Release notes generated using configuration in .github/release.yml at master -->

#### What's Changed

##### 🐛 Bug Fixes

-   🐛 Fix sdk-utils test helper log capture by [@&#8203;wwilsman](https://togithub.com/wwilsman) in [https://github.com/percy/cli/pull/920](https://togithub.com/percy/cli/pull/920)

##### ⬆️⬇️ Dependency Updates

-   ⬆️ Bump path-to-regexp from 6.2.0 to 6.2.1 by [@&#8203;dependabot](https://togithub.com/dependabot) in [https://github.com/percy/cli/pull/919](https://togithub.com/percy/cli/pull/919)
-   ⬆️ Bump rollup from 2.71.1 to 2.72.1 by [@&#8203;dependabot](https://togithub.com/dependabot) in [https://github.com/percy/cli/pull/918](https://togithub.com/percy/cli/pull/918)

**Full Changelog**: percy/cli@v1.1.2...v1.1.3

### [`v1.1.2`](https://togithub.com/percy/cli/releases/tag/v1.1.2)

[Compare Source](https://togithub.com/percy/cli/compare/v1.1.1...v1.1.2)

<!-- Release notes generated using configuration in .github/release.yml at master -->

#### What's Changed

##### 🐛 Bug Fixes

-   🐛 Fix logger TTY logic for logging progress by [@&#8203;wwilsman](https://togithub.com/wwilsman) in [https://github.com/percy/cli/pull/917](https://togithub.com/percy/cli/pull/917)

**Full Changelog**: percy/cli@v1.1.1...v1.1.2

### [`v1.1.1`](https://togithub.com/percy/cli/releases/tag/v1.1.1)

[Compare Source](https://togithub.com/percy/cli/compare/v1.1.0...v1.1.1)

<!-- Release notes generated using configuration in .github/release.yml at master -->

#### What's Changed

##### 🐛 Bug Fixes

-   🐛 Rollback sdk-utils to commonjs for broader compatibility by [@&#8203;wwilsman](https://togithub.com/wwilsman) in [https://github.com/percy/cli/pull/916](https://togithub.com/percy/cli/pull/916)

##### ⬆️⬇️ Dependency Updates

-   ⬆️ Bump karma from 6.3.18 to 6.3.19 by [@&#8203;dependabot](https://togithub.com/dependabot) in [https://github.com/percy/cli/pull/900](https://togithub.com/percy/cli/pull/900)
-   ⬆️ Bump [@&#8203;babel/preset-env](https://togithub.com/babel/preset-env) from 7.16.11 to 7.17.10 by [@&#8203;dependabot](https://togithub.com/dependabot) in [https://github.com/percy/cli/pull/913](https://togithub.com/percy/cli/pull/913)
-   ⬆️ Bump rollup from 2.70.2 to 2.71.1 by [@&#8203;dependabot](https://togithub.com/dependabot) in [https://github.com/percy/cli/pull/912](https://togithub.com/percy/cli/pull/912)
-   ⬆️ Bump [@&#8203;babel/cli](https://togithub.com/babel/cli) from 7.17.6 to 7.17.10 by [@&#8203;dependabot](https://togithub.com/dependabot) in [https://github.com/percy/cli/pull/911](https://togithub.com/percy/cli/pull/911)
-   ⬆️ Bump ws from 8.5.0 to 8.6.0 by [@&#8203;dependabot](https://togithub.com/dependabot) in [https://github.com/percy/cli/pull/914](https://togithub.com/percy/cli/pull/914)
-   ⬆️ Bump [@&#8203;babel/core](https://togithub.com/babel/core) from 7.17.9 to 7.17.10 by [@&#8203;dependabot](https://togithub.com/dependabot) in [https://github.com/percy/cli/pull/910](https://togithub.com/percy/cli/pull/910)
-   ⬆️ Bump [@&#8203;rollup/plugin-node-resolve](https://togithub.com/rollup/plugin-node-resolve) from 13.2.1 to 13.3.0 by [@&#8203;dependabot](https://togithub.com/dependabot) in [https://github.com/percy/cli/pull/915](https://togithub.com/percy/cli/pull/915)

**Full Changelog**: percy/cli@v1.1.0...v1.1.1

</details>

---

### Configuration

📅 **Schedule**: "every weekend" (UTC).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Never, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about these updates again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, click this checkbox.

---

This PR has been generated by [WhiteSource Renovate](https://renovate.whitesourcesoftware.com). View repository job log [here](https://app.renovatebot.com/dashboard#github/carbon-design-system/carbon-for-ibm-dotcom).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

✨ enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add additional execute helpers

2 participants