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

Async Page #1355

Merged
merged 48 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
dcf7d6f
Async page.bringToFront
inancgumus May 28, 2024
e80f998
Async page.check
inancgumus May 28, 2024
53b7e99
Async page.close
inancgumus May 28, 2024
fe04161
Async page.content
inancgumus May 28, 2024
90a70ef
Async page.dblclick
inancgumus May 28, 2024
f50a512
Async page.dispatchEvent
inancgumus May 28, 2024
38936d7
Async page.emulateMedia
inancgumus May 28, 2024
ad52c71
Async page.emulateVisionDeficiency
inancgumus May 28, 2024
d97b286
Async page.evaluate
inancgumus May 28, 2024
fe90e26
Async page.evaluateHandle
inancgumus May 28, 2024
3c93ad5
Async page.fill
inancgumus May 28, 2024
c2cb3b3
Async page.focus
inancgumus May 28, 2024
237185e
Async page.getAttribute
inancgumus May 28, 2024
af519d0
Async page.hover
inancgumus May 28, 2024
b6f4320
Async page.innerHTML
inancgumus May 28, 2024
6838228
Async page.innerText
inancgumus May 28, 2024
671a5b8
Async page.inputValue
inancgumus May 28, 2024
1e4e1af
Async page.isChecked
inancgumus May 28, 2024
c0a163b
Async page.isDisabled
inancgumus May 28, 2024
316ab0a
Async page.isEditable
inancgumus May 28, 2024
7b8768f
Async page.isEnabled
inancgumus May 28, 2024
3f513b5
Async page.isHidden
inancgumus May 28, 2024
97556a5
Async page.isVisible
inancgumus May 28, 2024
20edcf7
Async page.opener
inancgumus May 28, 2024
216f06f
Async page.press
inancgumus May 28, 2024
8366607
Async page.reload
inancgumus May 28, 2024
5bdd8f1
Async page.screenshot
inancgumus May 28, 2024
c3ff184
Async page.selectOption
inancgumus May 28, 2024
c38f603
Async page.setContent
inancgumus May 28, 2024
34ba333
Async page.setExtraHTTPHeaders
inancgumus May 28, 2024
6b4da9a
Async page.setInputFiles
inancgumus May 28, 2024
62461eb
Async page.setViewportSize
inancgumus May 28, 2024
8148845
Async page.textContent
inancgumus May 28, 2024
895c367
Async page.throttleCPU
inancgumus May 28, 2024
78b8119
Async page.throttleNetwork
inancgumus May 28, 2024
6d9134d
Async page.title
inancgumus May 28, 2024
abb570a
Async page.type
inancgumus May 28, 2024
ee81639
Async page.uncheck
inancgumus May 28, 2024
adc3814
Async page.viewportSize
inancgumus May 28, 2024
adbd3f2
Async page.waitForLoadState
inancgumus May 28, 2024
1ee0f7b
Async page.waitForSelector
inancgumus May 28, 2024
2d617a6
Async page.waitForTimeout
inancgumus May 28, 2024
67b755b
Async page.query
inancgumus May 28, 2024
8852b68
Async page.queryAll
inancgumus May 28, 2024
eb01583
Update async page tests
inancgumus May 28, 2024
0d92117
Update JS examples for async page
inancgumus May 28, 2024
ad9e5b5
Add forgetten page err checks in tests
inancgumus May 29, 2024
7608b48
Revert "Async page.viewportSize"
inancgumus May 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
350 changes: 252 additions & 98 deletions browser/page_mapping.go

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions examples/colorscheme.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default async function() {
'https://googlechromelabs.github.io/dark-mode-toggle/demo/',
{ waitUntil: 'load' },
)
const colorScheme = page.evaluate(() => {
const colorScheme = await page.evaluate(() => {
return {
isDarkColorScheme: window.matchMedia('(prefers-color-scheme: dark)').matches
};
Expand All @@ -40,6 +40,6 @@ export default async function() {
'isDarkColorScheme': cs => cs.isDarkColorScheme
});
} finally {
page.close();
await page.close();
}
}
2 changes: 1 addition & 1 deletion examples/cookies.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,6 @@ export default async function () {
'number of cookies should be zero': n => n === 0,
});
} finally {
page.close();
await page.close();
}
}
4 changes: 2 additions & 2 deletions examples/device_emulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default async function() {

try {
await page.goto('https://k6.io/', { waitUntil: 'networkidle' });
const dimensions = page.evaluate(() => {
const dimensions = await page.evaluate(() => {
return {
width: document.documentElement.clientWidth,
height: document.documentElement.clientHeight,
Expand All @@ -46,6 +46,6 @@ export default async function() {
sleep(10);
}
} finally {
page.close();
await page.close();
}
}
2 changes: 1 addition & 1 deletion examples/dispatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ export default async function() {
const ok = await h3.textContent() == "Contact us";
check(ok, { "header": ok });
} finally {
page.close();
await page.close();
}
}
32 changes: 23 additions & 9 deletions examples/elementstate.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default async function() {
const page = await context.newPage();

// Inject page content
page.setContent(`
await page.setContent(`
<div class="visible">Hello world</div>
<div style="display:none" class="hidden"></div>
<div class="editable" editable>Edit me</div>
Expand All @@ -33,13 +33,27 @@ export default async function() {
`);

// Check state
const isVisible = await page.$('.visible').isVisible();
const isHidden = await page.$('.hidden').isHidden();
const isEditable = await page.$('.editable').isEditable();
const isEnabled = await page.$('.enabled').isEnabled();
const isDisabled = await page.$('.disabled').isDisabled();
const isChecked = await page.$('.checked').isChecked();
const isUnchecked = await page.$('.unchecked').isChecked() === false;
let el = await page.$('.visible');
const isVisible = await el.isVisible();

el = await page.$('.hidden');
const isHidden = await el.isHidden();

el = await page.$('.editable');
const isEditable = await el.isEditable();

el = await page.$('.enabled');
const isEnabled = await el.isEnabled();

el = await page.$('.disabled');
const isDisabled = await el.isDisabled();

el = await page.$('.checked');
const isChecked = await el.isChecked();

el = await page.$('.unchecked');
const isUnchecked = await el.isChecked() === false;

check(page, {
'visible': isVisible,
'hidden': isHidden,
Expand All @@ -50,5 +64,5 @@ export default async function() {
'unchecked': isUnchecked,
});

page.close();
await page.close();
}
6 changes: 3 additions & 3 deletions examples/evaluate.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,22 @@ export default async function() {
await page.goto("https://test.k6.io/", { waitUntil: "load" });

// calling evaluate without arguments
let result = page.evaluate(() => {
let result = await page.evaluate(() => {
return Promise.resolve(5 * 42);
});
check(result, {
"result should be 210": (result) => result == 210,
});

// calling evaluate with arguments
result = page.evaluate(([x, y]) => {
result = await page.evaluate(([x, y]) => {
return Promise.resolve(x * y);
}, [5, 5]
);
check(result, {
"result should be 25": (result) => result == 25,
});
} finally {
page.close();
await page.close();
}
}
2 changes: 1 addition & 1 deletion examples/fillform.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@ export default async function() {
}
})
} finally {
page.close();
await page.close();
}
}
4 changes: 2 additions & 2 deletions examples/getattribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ export default async function() {
await page.goto('https://googlechromelabs.github.io/dark-mode-toggle/demo/', {
waitUntil: 'load',
});
let el = page.$('#dark-mode-toggle-3');
let el = await page.$('#dark-mode-toggle-3');
const mode = await el.getAttribute('mode');
check(mode, {
"GetAttribute('mode')": mode === 'light',
});
} finally {
page.close();
await page.close();
}
}
4 changes: 2 additions & 2 deletions examples/grant_permission.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ export default async function() {

try {
await page.goto('https://test.k6.io/');
page.screenshot({ path: `example-chromium.png` });
await page.screenshot({ path: `example-chromium.png` });
await context.clearPermissions();
} finally {
page.close();
await page.close();
}
}
2 changes: 1 addition & 1 deletion examples/hosts.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ export default async function() {
'null response': r => r === null,
});
} finally {
page.close();
await page.close();
}
}
2 changes: 1 addition & 1 deletion examples/keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ export default async function () {
await page.keyboard.press('Enter'); // submit
await page.waitForNavigation();

page.close();
await page.close();
}
2 changes: 1 addition & 1 deletion examples/locator.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,6 @@ export default async function() {
]);
console.log(await currentBet.innerText());
} finally {
page.close();
await page.close();
}
}
2 changes: 1 addition & 1 deletion examples/locator_pom.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,6 @@ export default async function() {
await bet.heads();
console.log("Current bet:", await bet.current());
} finally {
page.close();
await page.close();
}
}
4 changes: 2 additions & 2 deletions examples/multiple-scenario.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export async function messages() {
try {
await page.goto('https://test.k6.io/my_messages.php', { waitUntil: 'networkidle' });
} finally {
page.close();
await page.close();
}
}

Expand All @@ -48,6 +48,6 @@ export async function news() {
try {
await page.goto('https://test.k6.io/news.php', { waitUntil: 'networkidle' });
} finally {
page.close();
await page.close();
}
}
4 changes: 2 additions & 2 deletions examples/pageon.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ export default async function() {
});
});

page.evaluate(() => console.log('this is a console.log message', 42));
await page.evaluate(() => console.log('this is a console.log message', 42));
} finally {
page.close();
await page.close();
}
}
2 changes: 1 addition & 1 deletion examples/querying.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ export default async function() {
'Title with XPath selector': titleWithXPath == 'test.k6.io',
});
} finally {
page.close();
await page.close();
}
}
4 changes: 2 additions & 2 deletions examples/screenshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ export default async function() {

try {
await page.goto('https://test.k6.io/');
page.screenshot({ path: 'screenshot.png' });
await page.screenshot({ path: 'screenshot.png' });
// TODO: Assert this somehow. Upload as CI artifact or just an external `ls`?
// Maybe even do a fuzzy image comparison against a preset known good screenshot?
} finally {
page.close();
await page.close();
}
}
8 changes: 6 additions & 2 deletions examples/shadowdom.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,23 @@ export const options = {

export default async function() {
const page = await browser.newPage();
page.setContent("<html><head><style></style></head><body>hello!</body></html>")

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="find">Shadow DOM</p>';
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,
});
page.close();

await page.close();
}
10 changes: 5 additions & 5 deletions examples/throttle.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export async function normal() {
try {
await page.goto('https://test.k6.io/', { waitUntil: 'networkidle' });
} finally {
page.close();
await page.close();
}
}

Expand All @@ -54,11 +54,11 @@ export async function networkThrottled() {
const page = await context.newPage();

try {
page.throttleNetwork(networkProfiles['Slow 3G']);
await page.throttleNetwork(networkProfiles["Slow 3G"]);

await page.goto('https://test.k6.io/', { waitUntil: 'networkidle' });
} finally {
page.close();
await page.close();
}
}

Expand All @@ -67,10 +67,10 @@ export async function cpuThrottled() {
const page = await context.newPage();

try {
page.throttleCPU({ rate: 4 });
await page.throttleCPU({ rate: 4 });

await page.goto('https://test.k6.io/', { waitUntil: 'networkidle' });
} finally {
page.close();
await page.close();
}
}
2 changes: 1 addition & 1 deletion examples/touchscreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ export default async function () {
page.touchscreen.tap(newsLinkBox.x + newsLinkBox.width / 2, newsLinkBox.y),
]);

page.close();
await page.close();
}
4 changes: 2 additions & 2 deletions examples/waitForEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ export default async function() {
await promise
console.log('predicate passed')

page.close()
page2.close()
await page.close()
await page2.close();
};
4 changes: 2 additions & 2 deletions examples/waitforfunction.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default async function() {
const page = await context.newPage();

try {
page.evaluate(() => {
await page.evaluate(() => {
setTimeout(() => {
const el = document.createElement('h1');
el.innerHTML = 'Hello';
Expand All @@ -36,6 +36,6 @@ export default async function() {
});
check(ok, { 'waitForFunction successfully resolved': ok.innerHTML() == 'Hello' });
} finally {
page.close();
await page.close();
}
}
4 changes: 2 additions & 2 deletions tests/browser_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ func TestK6Object(t *testing.T) {
got := vu.RunPromise(t, `
const p = await browser.newPage();
await p.goto("about:blank");
const o = p.evaluate(() => window.k6);
const o = await p.evaluate(() => window.k6);
return JSON.stringify(o);
`)
assert.Equal(t, tt.want, got.Result().String())
Expand All @@ -670,7 +670,7 @@ func TestK6Object(t *testing.T) {
const c = await browser.newContext();
const p2 = await c.newPage();
await p2.goto("about:blank");
const o2 = p2.evaluate(() => window.k6);
const o2 = await p2.evaluate(() => window.k6);
return JSON.stringify(o2);
`)
assert.Equal(t, tt.want, got.Result().String())
Expand Down
2 changes: 1 addition & 1 deletion tests/browser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func TestBrowserCrashErr(t *testing.T) {
vu.SetVar(t, "browser", jsMod.Browser)
_, err := vu.RunAsync(t, `
const p = await browser.newPage();
p.close();
await p.close();
`)
assert.ErrorContains(t, err, "launching browser: Invalid devtools server port")
}
Expand Down
2 changes: 1 addition & 1 deletion tests/browser_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestBrowserTypeLaunchToConnect(t *testing.T) {
vu.SetVar(t, "browser", jsMod.Browser)
_, err := vu.RunAsync(t, `
const p = await browser.newPage();
p.close();
await p.close();
`)
require.NoError(t, err)

Expand Down
3 changes: 2 additions & 1 deletion tests/launch_options_slowmo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ func TestBrowserOptionsSlowMo(t *testing.T) {
t.Parallel()
tb := newTestBrowser(t, withFileServer())
testPageSlowMoImpl(t, tb, func(_ *testBrowser, p *common.Page) {
p.Reload(nil)
_, err := p.Reload(nil)
require.NoError(t, err)
})
})
t.Run("setContent", func(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion tests/lifecycle_wait_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,8 @@ func TestLifecycleReload(t *testing.T) {
WaitUntil: tt.waitUntil,
Timeout: 30 * time.Second,
})
p.Reload(opts)
_, err = p.Reload(opts)
require.NoError(t, err)

result, err = p.TextContent("#pingRequestText", nil)
if err != nil {
Expand Down
Loading
Loading