Skip to content
This repository has been archived by the owner on Jan 30, 2025. It is now read-only.

Async WaitForNavigation #440

Closed
Tracked by #428
inancgumus opened this issue Jul 7, 2022 · 0 comments · Fixed by #467
Closed
Tracked by #428

Async WaitForNavigation #440

inancgumus opened this issue Jul 7, 2022 · 0 comments · Fixed by #467
Assignees
Labels
async supports async (promises) feature A new feature
Milestone

Comments

@inancgumus
Copy link
Member

inancgumus commented Jul 7, 2022

A usage example can be seen as follows:

import { check } from 'k6';
import { chromium } from 'k6/x/browser';

export default function() {
  const browser = chromium.launch({
    headless: true,
  });
  const context = browser.newContext();
  const page = context.newPage();

  page.goto('https://test.k6.io/', { waitUntil: 'networkidle' });
  const elem = page.$('a[href="/my_messages.php"]');
  elem.click().then(() => {
    page.$('input[name="login"]').type('admin');
    page.$('input[name="password"]').type('123');
  }).then(() =>
    page.$('input[type="submit"]').click()
  ).then(() =>
    page.waitForNavigation()
  ).then(() => {
    check(page, {
      'header': page.$('h2').textContent() == 'Welcome, admin!',
    });
  }).finally(() => {
    page.close();
    browser.close();
  });
}

Another example can be using Promise.all for Click and WaitForNavigation as in PW as follows:

import { chromium } from 'k6/x/browser';

export default function() {
  const browser = chromium.launch({
    headless: true,
  });
  const context = browser.newContext();
  const page = context.newPage();

  page.setContent(`
    <html>
      <head>
        <title>Navigation test within the same document</title>
      </head>
      <body>
        <a id="nav-history" href="#">Navigate with History API</a>
        <a id="nav-anchor" href="#anchor">Navigate with anchor link</a>
        <div id="anchor">Some div...</div>
        <script>
          const el = document.querySelector('a#nav-history');
          el.addEventListener('click', function(evt) {
            evt.preventDefault();
            history.pushState({}, 'navigated', '/nav2');
          });
        </script>
      </body>
    </html>  
  `);

  Promise.all([
    page.click('a#nav-anchor'),
    page.waitForNavigation({timeout: 500}),
  ]).then(() => { 
    console.log("ok");
  }).catch(e => {
    console.log("err: "+ e);
  }).finally(() => {
    page.close();
    browser.close();
  })
}
@inancgumus inancgumus added feature A new feature async supports async (promises) labels Jul 7, 2022
@inancgumus inancgumus added this to the v0.5.0 milestone Jul 7, 2022
@inancgumus inancgumus self-assigned this Jul 13, 2022
@inancgumus inancgumus changed the title Async: Page.WaitForNavigation Async: WaitForNavigation Jul 19, 2022
@imiric imiric added breaking PRs that need to be mentioned in the breaking changes section of the release notes and removed breaking PRs that need to be mentioned in the breaking changes section of the release notes labels Jul 27, 2022
@ankur22 ankur22 self-assigned this Aug 4, 2022
@inancgumus inancgumus changed the title Async: WaitForNavigation Async WaitForNavigation May 8, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
async supports async (promises) feature A new feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants