From ee46a5f0c16d1df846adb85e2304f6d061129c0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 27 Sep 2024 12:33:53 -0400 Subject: [PATCH 01/22] Update examples/colorscheme to async check --- examples/colorscheme.js | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/examples/colorscheme.js b/examples/colorscheme.js index 42f3725c6..1d13df987 100644 --- a/examples/colorscheme.js +++ b/examples/colorscheme.js @@ -1,5 +1,5 @@ -import { check } from 'k6'; import { browser } from 'k6/x/browser/async'; +import { check } from 'https://jslib.k6.io/k6-utils/1.5.0/index.js'; export const options = { scenarios: { @@ -18,26 +18,20 @@ export const options = { } export default async function() { - const preferredColorScheme = 'dark'; - const context = await browser.newContext({ // valid values are "light", "dark" or "no-preference" - colorScheme: preferredColorScheme, + colorScheme: 'dark', }); const page = await context.newPage(); try { await page.goto( - 'https://googlechromelabs.github.io/dark-mode-toggle/demo/', + 'https://test.k6.io', { waitUntil: 'load' }, ) - const colorScheme = await page.evaluate(() => { - return { - isDarkColorScheme: window.matchMedia('(prefers-color-scheme: dark)').matches - }; - }); - check(colorScheme, { - 'isDarkColorScheme': cs => cs.isDarkColorScheme + await check(page, { + 'isDarkColorScheme': + p => p.evaluate(() => window.matchMedia('(prefers-color-scheme: dark)').matches) }); } finally { await page.close(); From 3167545254f9aa1c31ee384f57ed1a781bd447f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 27 Sep 2024 12:41:37 -0400 Subject: [PATCH 02/22] Update examples/cookies to async check --- examples/cookies.js | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/examples/cookies.js b/examples/cookies.js index c545e28b3..c64701ee9 100644 --- a/examples/cookies.js +++ b/examples/cookies.js @@ -1,5 +1,5 @@ -import { check } from 'k6'; import { browser } from 'k6/x/browser/async'; +import { check } from 'https://jslib.k6.io/k6-utils/1.5.0/index.js'; export const options = { scenarios: { @@ -23,8 +23,8 @@ export default async function () { try { // get cookies from the browser context - check((await context.cookies()).length, { - 'initial number of cookies should be zero': n => n === 0, + await check(await context.cookies(), { + 'initial number of cookies should be zero': c => c.length === 0, }); // add some cookies to the browser context @@ -63,10 +63,10 @@ export default async function () { } ]); let cookies = await context.cookies(); - check(cookies.length, { + await check(cookies.length, { 'number of cookies should be 2': n => n === 2, }); - check(cookies[0], { + await check(cookies[0], { 'cookie 1 name should be testcookie': c => c.name === 'testcookie', 'cookie 1 value should be 1': c => c.value === '1', 'cookie 1 should be session cookie': c => c.expires === -1, @@ -76,7 +76,7 @@ export default async function () { 'cookie 1 should be httpOnly': c => c.httpOnly === true, 'cookie 1 should be secure': c => c.secure === true, }); - check(cookies[1], { + await check(cookies[1], { 'cookie 2 name should be testcookie2': c => c.name === 'testcookie2', 'cookie 2 value should be 2': c => c.value === '2', }); @@ -103,23 +103,22 @@ export default async function () { }, ]); cookies = await context.cookies("http://foo.com", "https://baz.com"); - check(cookies.length, { + await check(cookies.length, { 'number of filtered cookies should be 2': n => n === 2, }); - check(cookies[0], { + await check(cookies[0], { 'the first filtered cookie name should be foo': c => c.name === 'foo', 'the first filtered cookie value should be 42': c => c.value === '42', }); - check(cookies[1], { + await check(cookies[1], { 'the second filtered cookie name should be baz': c => c.name === 'baz', 'the second filtered cookie value should be 44': c => c.value === '44', }); // clear cookies await context.clearCookies(); - cookies = await context.cookies(); - check(cookies.length, { - 'number of cookies should be zero': n => n === 0, + await check(await context.cookies(), { + 'number of cookies should be zero': c => c.length === 0, }); } finally { await page.close(); From 4ef8c0acd00c529aaf961529f2635f79a66dd510 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 27 Sep 2024 12:50:18 -0400 Subject: [PATCH 03/22] Update examples/device_emulation to async check --- examples/device_emulation.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/device_emulation.js b/examples/device_emulation.js index a3f1a3066..cbb114f78 100644 --- a/examples/device_emulation.js +++ b/examples/device_emulation.js @@ -1,5 +1,5 @@ -import { check } from 'k6'; import { browser, devices } from 'k6/x/browser/async'; +import { check } from 'https://jslib.k6.io/k6-utils/1.5.0/index.js'; export const options = { scenarios: { @@ -27,7 +27,7 @@ export default async function() { const page = await context.newPage(); try { - await page.goto('https://k6.io/', { waitUntil: 'networkidle' }); + await page.goto('https://test.k6.io/', { waitUntil: 'networkidle' }); const dimensions = await page.evaluate(() => { return { width: document.documentElement.clientWidth, @@ -36,7 +36,7 @@ export default async function() { }; }); - check(dimensions, { + await check(dimensions, { 'width': d => d.width === device.viewport.width, 'height': d => d.height === device.viewport.height, 'scale': d => d.deviceScaleFactor === device.deviceScaleFactor, From 9c49e6fcc9ad10b29b61d1d598ab30093b6bba5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 27 Sep 2024 13:10:05 -0400 Subject: [PATCH 04/22] Update examples/dispatch to async check --- examples/dispatch.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/examples/dispatch.js b/examples/dispatch.js index 057b8932d..dc31bfb15 100644 --- a/examples/dispatch.js +++ b/examples/dispatch.js @@ -1,5 +1,5 @@ -import { check } from 'k6'; import { browser } from 'k6/x/browser/async'; +import { check } from 'https://jslib.k6.io/k6-utils/1.5.0/index.js'; export const options = { scenarios: { @@ -27,9 +27,10 @@ export default async function() { const contacts = page.locator('a[href="/contacts.php"]'); await contacts.dispatchEvent("click"); - const h3 = page.locator("h3"); - const ok = await h3.textContent() == "Contact us"; - check(ok, { "header": ok }); + await check(page.locator('h3'), { + 'header': async lo => lo.textContent() + .then(text => text == 'Contact us') + }); } finally { await page.close(); } From 89999c912005337a63e9b017caf66394bfc2e99f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 27 Sep 2024 13:14:19 -0400 Subject: [PATCH 05/22] Update examples/elementstate to async check --- examples/elementstate.js | 55 +++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/examples/elementstate.js b/examples/elementstate.js index 47eb9f9bd..ee3f651c8 100644 --- a/examples/elementstate.js +++ b/examples/elementstate.js @@ -1,5 +1,5 @@ -import { check } from 'k6'; import { browser } from 'k6/x/browser/async'; +import { check } from 'https://jslib.k6.io/k6-utils/1.5.0/index.js'; export const options = { scenarios: { @@ -33,34 +33,37 @@ export default async function() { `); // Check state - let isVisible = await page.$('.visible').then(e => e.isVisible()); - let isHidden = await page.$('.hidden').then(e => e.isHidden()); - let isEditable = await page.$('.editable').then(e => e.isEditable()); - let isEnabled = await page.$('.enabled').then(e => e.isEnabled()); - let isDisabled = await page.$('.disabled').then(e => e.isDisabled()); - let isChecked = await page.$('.checked').then(e => e.isChecked()); - let isUnchecked = !await page.$('.unchecked').then(e => e.isChecked()); - - check(page, { - 'visible': isVisible, - 'hidden': isHidden, - 'editable': isEditable, - 'enabled': isEnabled, - 'disabled': isDisabled, - 'checked': isChecked, - 'unchecked': isUnchecked, + await check(page, { + 'is visible': + async p => p.$('.visible').then(e => e.isVisible()), + 'is hidden': + async p => p.$('.hidden').then(e => e.isHidden()), + 'is editable': + async p => p.$('.editable').then(e => e.isEditable()), + 'is enabled': + async p => p.$('.enabled').then(e => e.isEnabled()), + 'is disabled': + async p => p.$('.disabled').then(e => e.isDisabled()), + 'is checked': + async p => p.$('.checked').then(e => e.isChecked()), + 'is unchecked': + async p => p.$('.unchecked') + .then(async e => await e.isChecked() === false), }); // Change state and check again - await page.$(".unchecked").then(e => e.setChecked(true)) - await page.$(".checked").then(e => e.setChecked(false)) - - let isUncheckedChecked = await page.$(".unchecked").then((e) => e.isChecked()); - let isCheckedUnchecked = !await page.$(".checked").then((e) => e.isChecked()); - - check(page, { - isUncheckedChecked: isUncheckedChecked, - isCheckedUnchecked: isCheckedUnchecked, + await check(page, { + 'is unchecked checked': + async p => p.$(".unchecked") + .then(e => e.setChecked(true)) + .then(() => p.$(".unchecked")) + .then(e => e.isChecked()), + 'is checked unchecked': + async p => p.$(".checked") + .then(e => e.setChecked(false)) + .then(() => p.$(".checked")) + .then(e => e.isChecked()) + .then(checked => !checked), }); await page.close(); From bf09afb0b444570d71bb2e8fb4810b80870cdbcd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 27 Sep 2024 13:44:23 -0400 Subject: [PATCH 06/22] Update examples/evaluate to async check --- examples/evaluate.js | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/examples/evaluate.js b/examples/evaluate.js index 1dc1ad097..034e5b020 100644 --- a/examples/evaluate.js +++ b/examples/evaluate.js @@ -1,5 +1,5 @@ -import { check } from 'k6'; import { browser } from 'k6/x/browser/async'; +import { check } from 'https://jslib.k6.io/k6-utils/1.5.0/index.js'; export const options = { scenarios: { @@ -25,20 +25,21 @@ export default async function() { await page.goto("https://test.k6.io/", { waitUntil: "load" }); // calling evaluate without arguments - let result = await page.evaluate(() => { - return Promise.resolve(5 * 42); - }); - check(result, { - "result should be 210": (result) => result == 210, - }); + await check(page, { + 'result should be 210': + async p => p.evaluate( + () => 5 * 42 + ) + .then(r => r == 210) + }); // calling evaluate with arguments - result = await page.evaluate(([x, y]) => { - return Promise.resolve(x * y); - }, [5, 5] - ); - check(result, { - "result should be 25": (result) => result == 25, + await check(page, { + 'result should be 25': + async p => p.evaluate( + ([x, y]) => x * y, [5, 5], + ) + .then(r => r == 25), }); } finally { await page.close(); From 686b21e56a8719985e5b388637ab943e15c3c981 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 27 Sep 2024 13:52:08 -0400 Subject: [PATCH 07/22] Update examples/fillform to async check --- examples/fillform.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/examples/fillform.js b/examples/fillform.js index 90b94dde2..3ddd3d0a1 100644 --- a/examples/fillform.js +++ b/examples/fillform.js @@ -1,5 +1,5 @@ -import { check } from 'k6'; import { browser } from 'k6/x/browser/async'; +import { check } from 'https://jslib.k6.io/k6-utils/1.5.0/index.js'; export const options = { scenarios: { @@ -41,17 +41,17 @@ export default async function() { page.locator('input[type="submit"]').click(), ]); - const h2 = page.locator('h2'); - const headerOK = await h2.textContent() == 'Welcome, admin!'; - check(headerOK, { 'header': headerOK }); + await check(page.locator('h2'), { + 'header': async lo => lo.textContent() + .then(t => t == 'Welcome, admin!') + }); // Check whether we receive cookies from the logged site. - check(await context.cookies(), { - 'session cookie is set': cookies => { - const sessionID = cookies.find(c => c.name == 'sid') - return typeof sessionID !== 'undefined' - } - }) + await check(context, { + 'session cookie is set': async ctx => ctx.cookies() + .then(cookies => cookies.find(c => c.name == 'sid')) + .then(sessionID => typeof sessionID !== 'undefined') + }); } finally { await page.close(); } From f1277a5119b2cdd5fa8315d828be6965114b8401 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 27 Sep 2024 13:56:26 -0400 Subject: [PATCH 08/22] Update examples/getattribute to async check --- examples/getattribute.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/examples/getattribute.js b/examples/getattribute.js index 9584159b9..2615c506e 100644 --- a/examples/getattribute.js +++ b/examples/getattribute.js @@ -1,5 +1,5 @@ -import { check } from 'k6'; import { browser } from 'k6/x/browser/async'; +import { check } from 'https://jslib.k6.io/k6-utils/1.5.0/index.js'; export const options = { scenarios: { @@ -25,10 +25,11 @@ export default async function() { await page.goto('https://googlechromelabs.github.io/dark-mode-toggle/demo/', { waitUntil: 'load', }); - let el = await page.$('#dark-mode-toggle-3'); - const mode = await el.getAttribute('mode'); - check(mode, { - "GetAttribute('mode')": mode === 'light', + await check(page, { + "GetAttribute('mode')": + async p => p.$('#dark-mode-toggle-3') + .then(e => e.getAttribute('mode')) + .then(m => m === 'light'), }); } finally { await page.close(); From d100d0bb905ce939705997008d2aa6bf27156b53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 27 Sep 2024 13:57:46 -0400 Subject: [PATCH 09/22] Update examples/hosts to async check --- examples/hosts.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/examples/hosts.js b/examples/hosts.js index 17fc4a8c9..4f858c4c7 100644 --- a/examples/hosts.js +++ b/examples/hosts.js @@ -1,5 +1,5 @@ -import { check } from 'k6'; import { browser } from 'k6/x/browser/async'; +import { check } from 'https://jslib.k6.io/k6-utils/1.5.0/index.js'; export const options = { scenarios: { @@ -23,8 +23,10 @@ export default async function() { const page = await context.newPage(); try { - const res = await page.goto('http://test.k6.io/', { waitUntil: 'load' }); - check(res, { + const res = await page.goto('http://test.k6.io/', { + waitUntil: 'load' + }); + await check(res, { 'null response': r => r === null, }); } finally { From df8a7b87f38a21a5efce642b52234819bcee598e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 27 Sep 2024 14:05:21 -0400 Subject: [PATCH 10/22] Update examples/pageon to async check --- examples/pageon.js | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/examples/pageon.js b/examples/pageon.js index 7628cd6a4..32485e975 100644 --- a/examples/pageon.js +++ b/examples/pageon.js @@ -1,5 +1,5 @@ import { browser } from 'k6/x/browser/async'; -import { check } from 'k6'; +import { check } from 'https://jslib.k6.io/k6-utils/1.5.0/index.js'; export const options = { scenarios: { @@ -23,16 +23,18 @@ export default async function() { try { await page.goto('https://test.k6.io/'); - page.on('console', async(msg) => { - const jsonValue1 = await msg.args()[0].jsonValue(); - const jsonValue2 = await msg.args()[1].jsonValue(); - check(msg, { - 'assertConsoleMessageType': msg => msg.type() == 'log', - 'assertConsoleMessageText': msg => msg.text() == 'this is a console.log message 42', - 'assertConsoleMessageArgs0': msg => jsonValue1 == 'this is a console.log message', - 'assertConsoleMessageArgs1': msg => jsonValue2 == 42, - }); - }); + page.on('console', async msg => check(msg, { + 'assert console message type': + msg => msg.type() == 'log', + 'assert console message text': + msg => msg.text() == 'this is a console.log message 42', + 'assert console message first argument': + msg => msg.args()[0].jsonValue() + .then(arg1 => arg1 == 'this is a console.log message'), + 'assert console message second argument': + msg => msg.args()[1].jsonValue() + .then(arg2 => arg2 == 42) + })); await page.evaluate(() => console.log('this is a console.log message', 42)); } finally { From 67163dad17d645d954e49897d55ff8db38db1d65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 27 Sep 2024 14:07:20 -0400 Subject: [PATCH 11/22] Update examples/querying to async check --- examples/querying.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/examples/querying.js b/examples/querying.js index f6cf87275..3497ce42a 100644 --- a/examples/querying.js +++ b/examples/querying.js @@ -1,5 +1,5 @@ -import { check } from 'k6'; import { browser } from 'k6/x/browser/async'; +import { check } from 'https://jslib.k6.io/k6-utils/1.5.0/index.js'; export const options = { scenarios: { @@ -24,12 +24,15 @@ export default async function() { try { await page.goto('https://test.k6.io/'); - const titleWithCSS = await page.$('header h1.title').then(e => e.textContent()); - const titleWithXPath = await page.$(`//header//h1[@class="title"]`).then(e => e.textContent()); - - check(page, { - 'Title with CSS selector': titleWithCSS == 'test.k6.io', - 'Title with XPath selector': titleWithXPath == 'test.k6.io', + await check(page, { + 'Title with CSS selector': + p => p.$('header h1.title') + .then(e => e.textContent()) + .then(title => title == 'test.k6.io'), + 'Title with XPath selector': + p => p.$(`//header//h1[@class="title"]`) + .then(e => e.textContent()) + .then(title => title == 'test.k6.io'), }); } finally { await page.close(); From adcb65fa5430f40593088f979d4c5d13eece6ca1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 27 Sep 2024 14:11:51 -0400 Subject: [PATCH 12/22] Update examples/shadowdom to async check --- examples/shadowdom.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/examples/shadowdom.js b/examples/shadowdom.js index ac32d8a93..6f3b16155 100644 --- a/examples/shadowdom.js +++ b/examples/shadowdom.js @@ -1,5 +1,5 @@ -import { check } from 'k6'; import { browser } from 'k6/x/browser/async'; +import { check } from 'https://jslib.k6.io/k6-utils/1.5.0/index.js'; export const options = { scenarios: { @@ -26,15 +26,17 @@ export default async function() { const shadowRoot = document.createElement('div'); shadowRoot.id = 'shadow-root'; shadowRoot.attachShadow({mode: 'open'}); - shadowRoot.shadowRoot.innerHTML = '

Shadow DOM

'; + shadowRoot.shadowRoot.innerHTML = '

Shadow DOM

'; document.body.appendChild(shadowRoot); }); - const shadowEl = page.locator("#find"); - const ok = await shadowEl.innerText() === "Shadow DOM"; - check(shadowEl, { - "shadow element exists": (e) => e !== null, - "shadow element text is correct": () => ok, + await check( + page.locator('#shadow-dom'), { + 'shadow element exists': + e => e !== null, + 'shadow element text is correct': + async e => e.innerText() + .then(text => text === 'Shadow DOM'), }); await page.close(); From 18eb2dd186ce78178845293f97873110cfcf0b4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 27 Sep 2024 14:18:22 -0400 Subject: [PATCH 13/22] Update waitforfunction.js to async check --- examples/waitforfunction.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/examples/waitforfunction.js b/examples/waitforfunction.js index d47973451..2816bceb0 100644 --- a/examples/waitforfunction.js +++ b/examples/waitforfunction.js @@ -1,5 +1,5 @@ -import { check } from 'k6'; import { browser } from 'k6/x/browser/async'; +import { check } from 'https://jslib.k6.io/k6-utils/1.5.0/index.js'; export const options = { scenarios: { @@ -30,11 +30,16 @@ export default async function() { }, 1000); }); - const ok = await page.waitForFunction("document.querySelector('h1')", { - polling: 'mutation', - timeout: 2000, + await check(page, { + 'waitForFunction successfully resolved': + p => p.waitForFunction( + "document.querySelector('h1')", { + polling: 'mutation', + timeout: 2000 + }) + .then(e => e.innerHTML()) + .then(text => text == 'Hello') }); - check(ok, { 'waitForFunction successfully resolved': ok.innerHTML() == 'Hello' }); } finally { await page.close(); } From d64fce2ce4e5ce8ed1f7e789dd4b59ea7f8e3017 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Mon, 30 Sep 2024 11:53:05 -0400 Subject: [PATCH 14/22] Update example dispatch to await --- examples/dispatch.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/examples/dispatch.js b/examples/dispatch.js index dc31bfb15..1fcb96d0b 100644 --- a/examples/dispatch.js +++ b/examples/dispatch.js @@ -28,8 +28,10 @@ export default async function() { await contacts.dispatchEvent("click"); await check(page.locator('h3'), { - 'header': async lo => lo.textContent() - .then(text => text == 'Contact us') + 'header': async lo => { + const text = await lo.textContent(); + return text == 'Contact us'; + } }); } finally { await page.close(); From 0cc556bb29a2e4409f885032556f4f375e01a3e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Mon, 30 Sep 2024 11:53:16 -0400 Subject: [PATCH 15/22] Update example elementstate to await --- examples/elementstate.js | 64 ++++++++++++++++++++++++---------------- 1 file changed, 38 insertions(+), 26 deletions(-) diff --git a/examples/elementstate.js b/examples/elementstate.js index ee3f651c8..420fa13b6 100644 --- a/examples/elementstate.js +++ b/examples/elementstate.js @@ -34,36 +34,48 @@ export default async function() { // Check state await check(page, { - 'is visible': - async p => p.$('.visible').then(e => e.isVisible()), - 'is hidden': - async p => p.$('.hidden').then(e => e.isHidden()), - 'is editable': - async p => p.$('.editable').then(e => e.isEditable()), - 'is enabled': - async p => p.$('.enabled').then(e => e.isEnabled()), - 'is disabled': - async p => p.$('.disabled').then(e => e.isDisabled()), - 'is checked': - async p => p.$('.checked').then(e => e.isChecked()), - 'is unchecked': - async p => p.$('.unchecked') - .then(async e => await e.isChecked() === false), + 'is visible': async p => { + const e = await p.$('.visible'); + return await e.isVisible(); + }, + 'is hidden': async p => { + const e = await p.$('.hidden'); + return await e.isHidden() + }, + 'is editable': async p => { + const e = await p.$('.editable'); + return await e.isEditable(); + }, + 'is enabled': async p => { + const e = await p.$('.enabled'); + return await e.isEnabled(); + }, + 'is disabled': async p => { + const e = await p.$('.disabled'); + return await e.isDisabled(); + }, + 'is checked': async p => { + const e = await p.$('.checked'); + return await e.isChecked(); + }, + 'is unchecked': async p => { + const e = await p.$('.unchecked'); + return !await e.isChecked(); + } }); // Change state and check again await check(page, { - 'is unchecked checked': - async p => p.$(".unchecked") - .then(e => e.setChecked(true)) - .then(() => p.$(".unchecked")) - .then(e => e.isChecked()), - 'is checked unchecked': - async p => p.$(".checked") - .then(e => e.setChecked(false)) - .then(() => p.$(".checked")) - .then(e => e.isChecked()) - .then(checked => !checked), + 'is unchecked checked': async p => { + const e = await p.$(".unchecked"); + await e.setChecked(true); + return e.isChecked(); + }, + 'is checked unchecked': async p => { + const e = await p.$(".checked"); + await e.setChecked(false); + return !await e.isChecked(); + } }); await page.close(); From ce0ffcefdc6c5551c86dd19c367522f25121471d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Mon, 30 Sep 2024 11:53:23 -0400 Subject: [PATCH 16/22] Update example evaluate to await --- examples/evaluate.js | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/examples/evaluate.js b/examples/evaluate.js index 034e5b020..440c2763b 100644 --- a/examples/evaluate.js +++ b/examples/evaluate.js @@ -26,20 +26,18 @@ export default async function() { // calling evaluate without arguments await check(page, { - 'result should be 210': - async p => p.evaluate( - () => 5 * 42 - ) - .then(r => r == 210) - }); + 'result should be 210': async p => { + const n = await p.evaluate(() => 5 * 42); + return n == 210; + } + }); // calling evaluate with arguments await check(page, { - 'result should be 25': - async p => p.evaluate( - ([x, y]) => x * y, [5, 5], - ) - .then(r => r == 25), + 'result should be 25': async p => { + const n = await p.evaluate((x, y) => x * y, 5, 5); + return n == 25; + } }); } finally { await page.close(); From 565c4a07bf19c09268185e860c6beb6a1c7f0f98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Mon, 30 Sep 2024 11:53:33 -0400 Subject: [PATCH 17/22] Update example fillform to await --- examples/fillform.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/examples/fillform.js b/examples/fillform.js index 3ddd3d0a1..dd0039af5 100644 --- a/examples/fillform.js +++ b/examples/fillform.js @@ -42,15 +42,17 @@ export default async function() { ]); await check(page.locator('h2'), { - 'header': async lo => lo.textContent() - .then(t => t == 'Welcome, admin!') + 'header': async lo => { + return await lo.textContent() == 'Welcome, admin!' + } }); // Check whether we receive cookies from the logged site. await check(context, { - 'session cookie is set': async ctx => ctx.cookies() - .then(cookies => cookies.find(c => c.name == 'sid')) - .then(sessionID => typeof sessionID !== 'undefined') + 'session cookie is set': async ctx => { + const cookies = await ctx.cookies(); + return cookies.find(c => c.name == 'sid') !== undefined; + } }); } finally { await page.close(); From 1bb4f1e668b9d12b617244e17230ddca3e046c2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Mon, 30 Sep 2024 11:53:40 -0400 Subject: [PATCH 18/22] Update example getattribute to await --- examples/getattribute.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/getattribute.js b/examples/getattribute.js index 2615c506e..6366a4bed 100644 --- a/examples/getattribute.js +++ b/examples/getattribute.js @@ -26,10 +26,10 @@ export default async function() { waitUntil: 'load', }); await check(page, { - "GetAttribute('mode')": - async p => p.$('#dark-mode-toggle-3') - .then(e => e.getAttribute('mode')) - .then(m => m === 'light'), + "GetAttribute('mode')": async p => { + const e = await p.$('#dark-mode-toggle-3'); + return await e.getAttribute('mode') === 'light'; + } }); } finally { await page.close(); From 4048025682dae174281e92ee0325b926b454ae65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Mon, 30 Sep 2024 11:53:49 -0400 Subject: [PATCH 19/22] Update example pageon to await --- examples/pageon.js | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/examples/pageon.js b/examples/pageon.js index 32485e975..dc43d030f 100644 --- a/examples/pageon.js +++ b/examples/pageon.js @@ -24,16 +24,18 @@ export default async function() { await page.goto('https://test.k6.io/'); page.on('console', async msg => check(msg, { - 'assert console message type': - msg => msg.type() == 'log', - 'assert console message text': - msg => msg.text() == 'this is a console.log message 42', - 'assert console message first argument': - msg => msg.args()[0].jsonValue() - .then(arg1 => arg1 == 'this is a console.log message'), - 'assert console message second argument': - msg => msg.args()[1].jsonValue() - .then(arg2 => arg2 == 42) + 'assert console message type': msg => + msg.type() == 'log', + 'assert console message text': msg => + msg.text() == 'this is a console.log message 42', + 'assert console message first argument': async msg => { + const arg1 = await msg.args()[0].jsonValue(); + return arg1 == 'this is a console.log message'; + }, + 'assert console message second argument': async msg => { + const arg2 = await msg.args()[1].jsonValue(); + return arg2 == 42; + } })); await page.evaluate(() => console.log('this is a console.log message', 42)); From a2149b7142b397b3ce2952c4e709881812ea70f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Mon, 30 Sep 2024 11:53:57 -0400 Subject: [PATCH 20/22] Update example querying to await --- examples/querying.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/examples/querying.js b/examples/querying.js index 3497ce42a..5081e5e85 100644 --- a/examples/querying.js +++ b/examples/querying.js @@ -25,14 +25,14 @@ export default async function() { await page.goto('https://test.k6.io/'); await check(page, { - 'Title with CSS selector': - p => p.$('header h1.title') - .then(e => e.textContent()) - .then(title => title == 'test.k6.io'), - 'Title with XPath selector': - p => p.$(`//header//h1[@class="title"]`) - .then(e => e.textContent()) - .then(title => title == 'test.k6.io'), + 'Title with CSS selector': async p => { + const e = await p.$('header h1.title'); + return await e.textContent() === 'test.k6.io'; + }, + 'Title with XPath selector': async p => { + const e = await p.$('//header//h1[@class="title"]'); + return await e.textContent() === 'test.k6.io'; + } }); } finally { await page.close(); From d76cfcd03dd3cc1ed97698e5c294b899c1d03b77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Mon, 30 Sep 2024 11:54:04 -0400 Subject: [PATCH 21/22] Update example shadowdom to await --- examples/shadowdom.js | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/examples/shadowdom.js b/examples/shadowdom.js index 6f3b16155..98ceb45ae 100644 --- a/examples/shadowdom.js +++ b/examples/shadowdom.js @@ -30,13 +30,11 @@ export default async function() { document.body.appendChild(shadowRoot); }); - await check( - page.locator('#shadow-dom'), { - 'shadow element exists': - e => e !== null, - 'shadow element text is correct': - async e => e.innerText() - .then(text => text === 'Shadow DOM'), + await check(page.locator('#shadow-dom'), { + 'shadow element exists': e => e !== null, + 'shadow element text is correct': async e => { + return await e.innerText() === 'Shadow DOM'; + } }); await page.close(); From 470b26f99784e28a54fb431c511d9c5cc6b91cd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Mon, 30 Sep 2024 11:54:14 -0400 Subject: [PATCH 22/22] Update example waitforfunction to await --- examples/waitforfunction.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/examples/waitforfunction.js b/examples/waitforfunction.js index 2816bceb0..44a46a585 100644 --- a/examples/waitforfunction.js +++ b/examples/waitforfunction.js @@ -30,15 +30,16 @@ export default async function() { }, 1000); }); - await check(page, { - 'waitForFunction successfully resolved': - p => p.waitForFunction( - "document.querySelector('h1')", { - polling: 'mutation', - timeout: 2000 - }) - .then(e => e.innerHTML()) - .then(text => text == 'Hello') + const e = await page.waitForFunction( + "document.querySelector('h1')", { + polling: 'mutation', + timeout: 2000 + } + ); + await check(e, { + 'waitForFunction successfully resolved': async e => { + return await e.innerHTML() === 'Hello'; + } }); } finally { await page.close();