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

Use async check instead of the sync one #1746

Merged
merged 15 commits into from
Sep 27, 2024
Merged

Conversation

inancgumus
Copy link
Member

@inancgumus inancgumus commented Sep 25, 2024

What?

We're replacing sync check of k6 with the async check utility function.

Along the way, I've also fixed some small issues I saw.

Why

Writing concise code can be difficult when using the k6 check function with async code since it doesn't support async APIs. A solution that we have suggested so far is to declare a temporary variable, wait for the value that is to be checked, and then check the result later. However, this approach can clutter the code with single-use declarations and unnecessary variable names, for example:

const checked = await p.locator('.checked').isChecked();

check(checked, {
    'checked': c => c,
});

To address this limitation, we've added a version of the check function to jslib.k6.io that makes working with async/await simpler. The check function is a drop-in replacement for the built-in check, with added support for async code. Any Promises will be awaited, and the result is reported once the operation has been completed:

// Import the new check function from jslib.k6.io/k6-utils
import { check } from 'https://jslib.k6.io/k6-utils/1.5.0/index.js';

// ...

// Use the new check function with async code
check(page, {
    'checked': async p => p.locator('.checked').isChecked(),
});

Related PR(s)/Issue(s)

Closes #1725

Checklist

  • I have used a meaningful title for the PR.
  • I have described the changes I've made in the "What?" section above.
  • I have performed a self-review of my changes.
  • I have run the npm start command locally and verified that the changes look good.
  • I have made my changes in the docs/sources/next folder of the documentation.
  • I have reflected my changes in the docs/sources/v{most_recent_release} folder of the documentation.

@heitortsergent
Copy link
Collaborator

The changes look good to me, I'll wait until someone else on the development team takes a look. 🤓

Also, good catch on the k6/x/browser import. I did a search and found a few more instances of it (throttlenetwork.md, on.md, waitforevent.md). Do you mind if I make a commit to fix those as well?

@inancgumus
Copy link
Member Author

inancgumus commented Sep 26, 2024

@heitortsergent Thanks for your review!

The changes look good to me, I'll wait until someone else on the development team takes a look. 🤓

❤️ Of course!

Also, good catch on the k6/x/browser import. I did a search and found a few more instances of it (throttlenetwork.md, on.md, waitforevent.md). Do you mind if I make a commit to fix those as well?

Sure, please feel free :) Just for heads-up, anything above k6 0.52 must have k6/browser, whereas anything below must have k6/experimental/browser.

@inancgumus
Copy link
Member Author

@heitortsergent In addition to your commit, I've also added another that fixes the ones in v51 and below.

Copy link
Contributor

@ankur22 ankur22 left a comment

Choose a reason for hiding this comment

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

Thanks for this change 😄

Left some suggestions that might need to be looked at.

@inancgumus
Copy link
Member Author

@ankur22 Thanks for your review! I've made the updates 👍

@inancgumus inancgumus merged commit 9bc6d7c into main Sep 27, 2024
5 checks passed
@inancgumus inancgumus deleted the update/browser-async-check branch September 27, 2024 15:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: browser The browser module
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Docs: Replace sync check usage with async check
4 participants