browser: implement `page.goBack()` and `page.goForward()`
What?
This PR implements page.goBack() and page.goForward() to allow users to navigate through the browser's session history.
I have kept the implementation inline with Reload() and followed guidelines mentioned in the issue.
Why?
This functionality was missing and users had to rely on page.evaluate(() => window.history.back()), which is racy and unreliable for testing purposes as mentioned in the issue #5400
Example
import { browser } from 'k6/browser';
import { check } from 'k6';
export const options = {
scenarios: {
ui: {
executor: 'shared-iterations',
options: {
browser: {
type: 'chromium',
},
},
},
},
};
export default async function () {
const page = await browser.newPage();
try {
await page.goto('data:text/html,<h1>Page 1</h1>');
await page.goto('data:text/html,<h1>Page 2</h1>');
await page.goBack();
const url1 = await page.url();
check(url1, {
'goBack navigated to Page 1': (u) => u.includes('Page 1') || u.includes('Page%201'),
});
await page.goForward();
const url2 = await page.url();
check(url2, {
'goForward navigated to Page 2': (u) => u.includes('Page 2') || u.includes('Page%202'),
});
const nullResp = await page.goForward();
check(nullResp, {
'goForward at boundary returns null': (r) => r === null,
});
} finally {
await page.close();
}
}
Checklist
- [x] I have performed a self-review of my code.
- [x] I have commented on my code, particularly in hard-to-understand areas.
- [x] I have added tests for my changes.
- [x] I have run linter and tests locally (
make check) and all pass.
Checklist: Documentation (only for k6 maintainers and if relevant)
Please do not merge this PR until the following items are filled out.
- [ ] I have added the correct milestone and labels to the PR.
- [ ] I have updated the release notes: link
- [ ] I have updated or added an issue to the k6-documentation: grafana/k6-docs#NUMBER if applicable
- [ ] I have updated or added an issue to the TypeScript definitions: grafana/k6-DefinitelyTyped#NUMBER if applicable
Related PR(s)/Issue(s)
Closes #5400
@inancgumus Thanks a lot for the review and feedback.
Can you add a test with a page containing iframes? I'm suspicious that returning
truefrom here might not work as expected. Also, please add a test that goes back in a frame (if possible 🙇) 🤞
Regarding this, indeed you are right. The test are timing out. I overlooked this. I'll take some time to investigate and resolve this.
@inancgumus, I have implemented all the suggestions, thanks for pointing out a critical issue with the implementation.
- Fixed the
GoBackandGoForward - Removed unnecessary comments
- Refactored integration test to use helper functions
- Added iframe integration test for
GoBackandGoForward
What ?
GoBack and GoForward methods were timing out when navigating between real HTML pages, similar was observed with iframes.
FIX
Changed the implementation to poll for URL change instead of waiting for EventFrameNavigation:
- Get the target URL from the browser's navigation history (we know where we're going)
- Execute
NavigateToHistoryEntry() - Poll every 50ms checking if
mainFrame.URL() == targetURL
Also I am not sure why 2 test were failing in ci. please guide me here.