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

Add k6 browser tutorial for validating that a page has loaded #1779

Open
heitortsergent opened this issue Oct 18, 2024 · 0 comments
Open

Add k6 browser tutorial for validating that a page has loaded #1779

heitortsergent opened this issue Oct 18, 2024 · 0 comments
Assignees
Labels
Area: browser The browser module type/docs

Comments

@heitortsergent
Copy link
Collaborator

Feedback from @tom-miseur and user, we're missing a simple tutorial explaining how a user can check that a page has successfully loaded.

Example from @tom-miseur:

The most reliable way to check that a page has successfully loaded is to look for a specific web element to appear once an action has been performed.

To demonstrate:

import { browser } from 'k6/browser';

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('https://www.google.com/');
  } finally {
    await page.close();
  }
}

This will navigate to google.com, but there's no validation beyond that. Unless the page timed out, page.goto isn't going to throw an exception.

A good element to look for on the google homepage is the ubiquitous search box, which can be identified using the selector/locator //textarea[@name="q"]. So we can add that in after the page.goto navigation like this to validate that it is visible:

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('https://www.google.com/');

    const searchBoxVisible = await page.locator('//textarea[@name="q"]').isVisible();

    check(searchBoxVisible, {
      'Search box is visible': (e) => e === true,
    });
  } finally {
    await page.close();
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: browser The browser module type/docs
Projects
None yet
Development

No branches or pull requests

3 participants