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

Update sync examples with check to async check #1448

Merged
merged 22 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
ee46a5f
Update examples/colorscheme to async check
inancgumus Sep 27, 2024
3167545
Update examples/cookies to async check
inancgumus Sep 27, 2024
4ef8c0a
Update examples/device_emulation to async check
inancgumus Sep 27, 2024
9c49e6f
Update examples/dispatch to async check
inancgumus Sep 27, 2024
89999c9
Update examples/elementstate to async check
inancgumus Sep 27, 2024
bf09afb
Update examples/evaluate to async check
inancgumus Sep 27, 2024
686b21e
Update examples/fillform to async check
inancgumus Sep 27, 2024
f1277a5
Update examples/getattribute to async check
inancgumus Sep 27, 2024
d100d0b
Update examples/hosts to async check
inancgumus Sep 27, 2024
df8a7b8
Update examples/pageon to async check
inancgumus Sep 27, 2024
67163da
Update examples/querying to async check
inancgumus Sep 27, 2024
adcb65f
Update examples/shadowdom to async check
inancgumus Sep 27, 2024
18eb2dd
Update waitforfunction.js to async check
inancgumus Sep 27, 2024
d64fce2
Update example dispatch to await
inancgumus Sep 30, 2024
0cc556b
Update example elementstate to await
inancgumus Sep 30, 2024
ce0ffce
Update example evaluate to await
inancgumus Sep 30, 2024
565c4a0
Update example fillform to await
inancgumus Sep 30, 2024
1bb4f1e
Update example getattribute to await
inancgumus Sep 30, 2024
4048025
Update example pageon to await
inancgumus Sep 30, 2024
a2149b7
Update example querying to await
inancgumus Sep 30, 2024
d76cfcd
Update example shadowdom to await
inancgumus Sep 30, 2024
470b26f
Update example waitforfunction to await
inancgumus Sep 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 6 additions & 12 deletions examples/colorscheme.js
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -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();
Expand Down
23 changes: 11 additions & 12 deletions examples/cookies.js
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -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
Expand Down Expand Up @@ -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,
allansson marked this conversation as resolved.
Show resolved Hide resolved
});
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,
Expand All @@ -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',
});
Expand All @@ -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,
});
inancgumus marked this conversation as resolved.
Show resolved Hide resolved
} finally {
await page.close();
Expand Down
6 changes: 3 additions & 3 deletions examples/device_emulation.js
Original file line number Diff line number Diff line change
@@ -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: {
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
11 changes: 7 additions & 4 deletions examples/dispatch.js
Original file line number Diff line number Diff line change
@@ -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: {
Expand Down Expand Up @@ -27,9 +27,12 @@ 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 => {
const text = await lo.textContent();
return text == 'Contact us';
}
});
} finally {
await page.close();
}
Expand Down
67 changes: 41 additions & 26 deletions examples/elementstate.js
Original file line number Diff line number Diff line change
@@ -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: {
Expand Down Expand Up @@ -33,34 +33,49 @@ 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 => {
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 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 => {
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();
Expand Down
23 changes: 11 additions & 12 deletions examples/evaluate.js
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -25,20 +25,19 @@ 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 => {
const n = await p.evaluate(() => 5 * 42);
return n == 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 => {
const n = await p.evaluate((x, y) => x * y, 5, 5);
return n == 25;
}
});
} finally {
await page.close();
Expand Down
20 changes: 11 additions & 9 deletions examples/fillform.js
Original file line number Diff line number Diff line change
@@ -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: {
Expand Down Expand Up @@ -41,17 +41,19 @@ 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 => {
return await lo.textContent() == '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 => {
const cookies = await ctx.cookies();
return cookies.find(c => c.name == 'sid') !== undefined;
}
})
});
} finally {
await page.close();
}
Expand Down
11 changes: 6 additions & 5 deletions examples/getattribute.js
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -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 => {
const e = await p.$('#dark-mode-toggle-3');
return await e.getAttribute('mode') === 'light';
}
});
} finally {
await page.close();
Expand Down
8 changes: 5 additions & 3 deletions examples/hosts.js
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -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 {
Expand Down
26 changes: 15 additions & 11 deletions examples/pageon.js
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -23,16 +23,20 @@ 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': 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));
} finally {
Expand Down
Loading
Loading