Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 4 additions & 1 deletion examples/browser/colorscheme.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { browser } from 'k6/browser';
import { check } from 'https://jslib.k6.io/k6-utils/1.5.0/index.js';
import { expect } from "https://jslib.k6.io/k6-testing/0.5.0/index.js";

export const options = {
scenarios: {
Expand All @@ -25,10 +26,12 @@ export default async function() {
const page = await context.newPage();

try {
await page.goto(
const response = await page.goto(
'https://quickpizza.grafana.com/test.k6.io',
{ waitUntil: 'load' },
)
expect(response.status()).toBe(200);

await check(page, {
'isDarkColorScheme':
p => p.evaluate(() => window.matchMedia('(prefers-color-scheme: dark)').matches)
Expand Down
5 changes: 4 additions & 1 deletion examples/browser/device_emulation.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { browser, devices } from 'k6/browser';
import { check } from 'https://jslib.k6.io/k6-utils/1.5.0/index.js';
import { expect } from "https://jslib.k6.io/k6-testing/0.5.0/index.js";

export const options = {
scenarios: {
Expand Down Expand Up @@ -27,7 +28,9 @@ export default async function() {
const page = await context.newPage();

try {
await page.goto('https://quickpizza.grafana.com/test.k6.io/', { waitUntil: 'networkidle' });
const response = await page.goto('https://quickpizza.grafana.com/test.k6.io/', { waitUntil: 'networkidle' });
expect(response.status()).toBe(200);

const dimensions = await page.evaluate(() => {
return {
width: document.documentElement.clientWidth,
Expand Down
4 changes: 3 additions & 1 deletion examples/browser/dispatch.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { browser } from 'k6/browser';
import { check } from 'https://jslib.k6.io/k6-utils/1.5.0/index.js';
import { expect } from "https://jslib.k6.io/k6-testing/0.5.0/index.js";

export const options = {
scenarios: {
Expand All @@ -22,7 +23,8 @@ export default async function() {
const page = await context.newPage();

try {
await page.goto('https://quickpizza.grafana.com/test.k6.io/', { waitUntil: 'networkidle' });
const response = await page.goto('https://quickpizza.grafana.com/test.k6.io/', { waitUntil: 'networkidle' });
expect(response.status()).toBe(200);

const contacts = page.locator('a[href="/contacts.php"]');
await contacts.dispatchEvent("click");
Expand Down
4 changes: 3 additions & 1 deletion examples/browser/grant_permission.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { browser } from 'k6/browser';
import { expect } from "https://jslib.k6.io/k6-testing/0.5.0/index.js";

export const options = {
scenarios: {
Expand Down Expand Up @@ -26,7 +27,8 @@ export default async function() {
const page = await context.newPage();

try {
await page.goto('https://quickpizza.grafana.com/test.k6.io/');
const response = await page.goto('https://quickpizza.grafana.com/test.k6.io/');
expect(response.status()).toBe(200);
await page.screenshot({ path: `example-chromium.png` });
await context.clearPermissions();
} finally {
Expand Down
26 changes: 15 additions & 11 deletions examples/browser/keyboard.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { browser } from 'k6/browser';
import { expect } from "https://jslib.k6.io/k6-testing/0.5.0/index.js";

export const options = {
scenarios: {
Expand All @@ -16,18 +17,21 @@ export const options = {
export default async function () {
const page = await browser.newPage();

await page.goto('https://quickpizza.grafana.com/my_messages.php', { waitUntil: 'networkidle' });
try {
const response = await page.goto('https://quickpizza.grafana.com/my_messages.php', { waitUntil: 'networkidle' });
expect(response.status()).toBe(200);

const userInput = page.locator('input[name="login"]');
await userInput.click();
await page.keyboard.type("admin");
const userInput = page.locator('input[name="login"]');
await userInput.click();
await page.keyboard.type("admin");

const pwdInput = page.locator('input[name="password"]');
await pwdInput.click();
await page.keyboard.type("123");
const pwdInput = page.locator('input[name="password"]');
await pwdInput.click();
await page.keyboard.type("123");

await page.keyboard.press('Enter'); // submit
await page.waitForNavigation();

await page.close();
await page.keyboard.press('Enter'); // submit
await page.waitForNavigation();
} finally {
await page.close();
}
}
6 changes: 4 additions & 2 deletions examples/browser/locator.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { browser } from 'k6/browser';
import { expect } from "https://jslib.k6.io/k6-testing/0.5.0/index.js";

export const options = {
scenarios: {
Expand All @@ -21,9 +22,10 @@ export default async function() {
const page = await context.newPage();

try {
await page.goto("https://quickpizza.grafana.com/flip_coin.php", {
const response = await page.goto("https://quickpizza.grafana.com/flip_coin.php", {
waitUntil: "networkidle",
})
});
expect(response.status()).toBe(200);

/*
In this example, we will use two locators, matching a
Expand Down
7 changes: 5 additions & 2 deletions examples/browser/locator_pom.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { browser } from 'k6/browser';
import { expect } from "https://jslib.k6.io/k6-testing/0.5.0/index.js";

export const options = {
scenarios: {
Expand Down Expand Up @@ -33,8 +34,10 @@ export class Bet {
this.currentBet = page.locator("//p[starts-with(text(),'Your bet: ')]");
}

goto() {
return this.page.goto("https://quickpizza.grafana.com/flip_coin.php", { waitUntil: "networkidle" });
async goto() {
const response = await this.page.goto("https://quickpizza.grafana.com/flip_coin.php", { waitUntil: "networkidle" });
expect(response.status()).toBe(200);
return response;
}

heads() {
Expand Down
4 changes: 3 additions & 1 deletion examples/browser/multiple-scenario.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { browser } from 'k6/browser';
import { expect } from "https://jslib.k6.io/k6-testing/0.5.0/index.js";

export const options = {
scenarios: {
Expand Down Expand Up @@ -36,7 +37,8 @@ export async function messages() {
const page = await browser.newPage();

try {
await page.goto('https://quickpizza.grafana.com/my_messages.php', { waitUntil: 'networkidle' });
const response = await page.goto('https://quickpizza.grafana.com/my_messages.php', { waitUntil: 'networkidle' });
expect(response.status()).toBe(200);
} finally {
await page.close();
}
Expand Down
4 changes: 3 additions & 1 deletion examples/browser/querying.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { browser } from 'k6/browser';
import { check } from 'https://jslib.k6.io/k6-utils/1.5.0/index.js';
import { expect } from "https://jslib.k6.io/k6-testing/0.5.0/index.js";

export const options = {
scenarios: {
Expand All @@ -22,7 +23,8 @@ export default async function() {
const page = await context.newPage();

try {
await page.goto('https://quickpizza.grafana.com/test.k6.io/');
const response = await page.goto('https://quickpizza.grafana.com/test.k6.io/');
expect(response.status()).toBe(200);

await check(page, {
'Title with CSS selector': async p => {
Expand Down
34 changes: 18 additions & 16 deletions examples/browser/shadowdom.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,24 @@ export const options = {
export default async function() {
const page = await browser.newPage();

await page.setContent("<html><head><style></style></head><body>hello!</body></html>")
try {
await page.setContent("<html><head><style></style></head><body>hello!</body></html>")

await page.evaluate(() => {
const shadowRoot = document.createElement('div');
shadowRoot.id = 'shadow-root';
shadowRoot.attachShadow({mode: 'open'});
shadowRoot.shadowRoot.innerHTML = '<p id="shadow-dom">Shadow DOM</p>';
document.body.appendChild(shadowRoot);
});
await page.evaluate(() => {
const shadowRoot = document.createElement('div');
shadowRoot.id = 'shadow-root';
shadowRoot.attachShadow({mode: 'open'});
shadowRoot.shadowRoot.innerHTML = '<p id="shadow-dom">Shadow DOM</p>';
document.body.appendChild(shadowRoot);
});

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();
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';
}
});
} finally {
await page.close();
}
}
28 changes: 15 additions & 13 deletions examples/browser/touchscreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,21 @@ export const options = {
export default async function () {
const page = await browser.newPage();

await page.goto("https://quickpizza.grafana.com/test.k6.io/", { waitUntil: "networkidle" });
try {
await page.goto("https://quickpizza.grafana.com/test.k6.io/", { waitUntil: "networkidle" });

// Obtain ElementHandle for news link and navigate to it
// by tapping in the 'a' element's bounding box
const newsLinkBox = await page.$('a[href="/news.php"]').then((e) => e.boundingBox());
// Obtain ElementHandle for news link and navigate to it
// by tapping in the 'a' element's bounding box
const newsLinkBox = await page.$('a[href="/news.php"]').then((e) => e.boundingBox());

// Wait until the navigation is done before closing the page.
// Otherwise, there will be a race condition between the page closing
// and the navigation.
await Promise.all([
page.waitForNavigation(),
page.touchscreen.tap(newsLinkBox.x + newsLinkBox.width / 2, newsLinkBox.y),
]);

await page.close();
// Wait until the navigation is done before closing the page.
// Otherwise, there will be a race condition between the page closing
// and the navigation.
await Promise.all([
page.waitForNavigation(),
page.touchscreen.tap(newsLinkBox.x + newsLinkBox.width / 2, newsLinkBox.y),
]);
} finally {
await page.close();
}
}
37 changes: 22 additions & 15 deletions examples/browser/useragent.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,18 @@ export default async function() {
userAgent: 'k6 test user agent',
})
let page = await context.newPage();
await check(page, {
'user agent is set': async p => {
const userAgent = await p.evaluate(() => navigator.userAgent);
return userAgent.includes('k6 test user agent');
}
});
await page.close();
await context.close();

try {
await check(page, {
'user agent is set': async p => {
const userAgent = await p.evaluate(() => navigator.userAgent);
return userAgent.includes('k6 test user agent');
}
});
} finally {
await page.close();
await context.close();
}

context = await browser.newContext();
check(context.browser(), {
Expand All @@ -39,11 +43,14 @@ export default async function() {
});

page = await context.newPage();
await check(page, {
'chromium user agent does not contain headless': async p => {
const userAgent = await p.evaluate(() => navigator.userAgent);
return userAgent.includes('Headless') === false;
}
});
await page.close();
try {
await check(page, {
'chromium user agent does not contain headless': async p => {
const userAgent = await p.evaluate(() => navigator.userAgent);
return userAgent.includes('Headless') === false;
}
});
} finally {
await page.close();
}
}
16 changes: 9 additions & 7 deletions examples/browser/waitForEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ export default async function() {
const page = await context.newPage();
const page2 = await context.newPage();

// We await for the page creation events to be processed and the predicate
// to pass.
await promise
console.log('predicate passed')

await page.close()
await page2.close();
try {
// We await for the page creation events to be processed and the predicate
// to pass.
await promise
console.log('predicate passed')
} finally {
await page.close()
await page2.close();
}
};
Loading