No results for

Powered byAlgolia
⚠️ This documentation is outdated. Please visit for the latest k6 documentation.📚

Page object model

When working with large test suites, a popular design pattern to improve your code’s maintainability and readability is the page object model.

A page object commonly represents an HTML page or significant elements/components within a page, such as a header or a footer. It is a form of encapsulation that hides the details of the UI structure from other places, such as your test files. Through page object models, any changes you need to make on a specific page or element within a page are constrained into a single place, resulting in ease of maintenance and avoiding code duplication.

Since k6 browser aims to provide rough compatibility with the Playwright API, you can leverage any existing page objects you have and easily re-use them with your k6 browser tests.


Let's take an example of a website with a booking form added to the homepage. Imagine you want to write a test that checks that a user can fill out the booking form successfully.

To model a page object for the homepage, we've created a page object class called homepage.js. Different locators are created inside the constructor so that when the homepage class is instantiated, the page locator elements are ready to be used.

The homepage.js class also contains different methods for:

  • Navigating to the homepage
  • Submitting the form
  • Getting the verification message

When locators need to be updated or other specific changes related to the homepage are made, you only need to update the homepage.js class.

1import { bookingData } from '../data/booking-data.js'
3export class Homepage {
4 constructor(page) {
5 = page
6 this.nameField = page.locator('[data-testid="ContactName"]')
7 this.emailField = page.locator('[data-testid="ContactEmail"]')
8 this.phoneField = page.locator('[data-testid="ContactPhone"]');
9 this.subjectField = page.locator('[data-testid="ContactSubject"]');
10 this.descField = page.locator('[data-testid="ContactDescription"]');
11 this.submitButton = page.locator('#submitContact');
12 this.verificationMessage = page.locator(' h2')
13 }
15 async goto() {
16 await'https://myexamplewebsite/')
17 }
19 async submitForm() {
20 const { name, email, phone, subject, description } = bookingData
22 this.nameField.type(name)
23 this.emailField.type(email)
24 this.phoneField.type(phone)
25 this.subjectField.type(subject)
26 this.descField.type(description)
27 await
28 }
30 getVerificationMessage() {
31 return this.verificationMessage.innerText()
32 }

You can import the Homepage class within your test class and invoke the methods you need. This makes the code easier to understand and enforces the separation between your test and business logic.

1import { chromium } from 'k6/experimental/browser'
2import { expect } from ''
4import { Homepage } from '../pages/homepage.js'
5import { bookingData } from '../data/booking-data.js'
7export default async function () {
8 const browser = chromium.launch()
9 const page = browser.newPage()
11 const { name } = bookingData
13 const homepage = new Homepage(page)
14 await homepage.goto()
15 await homepage.submitForm()
17 expect(homepage.getVerificationMessage()).to.contain(name)
19 page.close()
20 browser.close()