Skip to content

Conversation

@pkalsi97
Copy link

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

  • I have performed a self-review of my code.
  • I have commented on my code, particularly in hard-to-understand areas.
  • I have added tests for my changes.
  • 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

@pkalsi97 pkalsi97 requested a review from a team as a code owner December 11, 2025 14:42
@pkalsi97 pkalsi97 requested review from inancgumus and mstoykov and removed request for a team December 11, 2025 14:42
Copy link
Contributor

@inancgumus inancgumus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution ❤️ Please watch the test results, and see if they pass, as I ran them. I have some suggestions.

@pkalsi97 pkalsi97 temporarily deployed to azure-trusted-signing December 11, 2025 18:43 — with GitHub Actions Inactive
@pkalsi97 pkalsi97 temporarily deployed to azure-trusted-signing December 11, 2025 18:46 — with GitHub Actions Inactive
@pkalsi97
Copy link
Author

pkalsi97 commented Dec 11, 2025

@inancgumus Thanks a lot for the review and feedback.

Can you add a test with a page containing iframes? I'm suspicious that returning true from 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.

@pkalsi97
Copy link
Author

pkalsi97 commented Dec 12, 2025

@inancgumus, I have implemented all the suggestions, thanks for pointing out a critical issue with the implementation.

  1. Fixed the GoBack and GoForward
  2. Removed unnecessary comments
  3. Refactored integration test to use helper functions
  4. Added iframe integration test for GoBack and GoForward

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:

  1. Get the target URL from the browser's navigation history (we know where we're going)
  2. Execute NavigateToHistoryEntry()
  3. Poll every 50ms checking if mainFrame.URL() == targetURL

Also I am not sure why 2 test were failing in ci. please guide me here.

@pkalsi97 pkalsi97 requested a review from inancgumus December 12, 2025 05:12
@pkalsi97 pkalsi97 temporarily deployed to azure-trusted-signing December 12, 2025 08:15 — with GitHub Actions Inactive
@pkalsi97 pkalsi97 temporarily deployed to azure-trusted-signing December 12, 2025 08:17 — with GitHub Actions Inactive
Copy link
Contributor

@inancgumus inancgumus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the changes @pkalsi97.

I have some suggestions, and we need to fix the git conflicts.

Don't mind those flaky tests. Your PR is fine as long as your tests pass.

@pkalsi97 pkalsi97 requested a review from inancgumus December 12, 2025 17:46
@pkalsi97 pkalsi97 temporarily deployed to azure-trusted-signing January 6, 2026 11:18 — with GitHub Actions Inactive
@pkalsi97 pkalsi97 temporarily deployed to azure-trusted-signing January 6, 2026 11:20 — with GitHub Actions Inactive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement Page.goBack and Page.goForward

2 participants