Menu
Open source

newContext([options])

Creates and returns a new BrowserContext, if one hasn’t already been initialized for the Browser. If one has already been initialized an error is thrown.

Note

A 1-to-1 mapping between Browser and BrowserContext means you cannot run BrowserContexts concurrently. Due to this restriction, if one already exists, it must be closed first before creating a new one.
ParameterTypeDefaultDescription
optionsobjectnull
options.bypassCSPbooleanfalseWhether to bypass a page’s Content-Security-Policy.
options.colorSchemestring'light'Whether to display a page in dark or light mode by emulating the ‘prefers-colors-scheme’ media feature. It can be one of 'light', 'dark', 'no-preference'.
options.deviceScaleFactor number1Sets the resolution ratio in physical pixels to the resolution in CSS pixels i.e. if set higher than 1, then images will look sharper on high pixel density screens. See an example below.
options.extraHTTPHeadersobjectnullContains additional HTTP headers to be sent with every request, where the keys are HTTP headers and values are HTTP header values.
options.geolocation objectnullSets the user’s geographical location.
options.geolocation.latitudenumber0Latitude should be between -90 and 90.
options.geolocation.longitudenumber0Longitude should be between -180 and 180.
options.geolocation.accuracynumber0Accuracy should only be a non-negative number. Defaults to 0.
options.hasTouch booleanfalseWhether to simulate a device with touch events.
options.httpCredentialsobjectnullSets the credentials for HTTP authentication using Basic Auth.
options.httpCredentials.usernamestring''Username to pass to the web browser for Basic HTTP Authentication.
options.httpCredentials.passwordstring''Password to pass to the web browser for Basic HTTP Authentication.
options.ignoreHTTPSErrorsbooleanfalseWhether to ignore HTTPS errors that may be caused by invalid certificates.
options.isMobilebooleanfalseWhether to simulate a mobile device.
options.javaScriptEnabledbooleantrueWhether to activate JavaScript support for the context.
options.localestringsystemSpecifies the user’s locale, such as 'en-US', 'tr-TR', etc.
options.offlinebooleanfalseWhether to emulate an offline network.
options.permissionsArraynullPermissions to grant for the context’s pages. See browserContext.grantPermissions() for the options.
options.reducedMotionstring'no-preference'Minimizes the amount of motion by emulating the ‘prefers-reduced-motion’ media feature. It can be one of 'reduce' and 'no-preference'. See page.emulateMedia() for the options.
options.screenobject{'width': 1280, 'height': 720}Sets a window screen size for all pages in the context. It can only be used when the viewport is set.
options.screen.widthnumber1280Page width in pixels.
options.screen.heightnumber720Page height in pixels.
options.timezoneIDstringsystemChanges the context’s timezone. See ICU’s metaZones.txt for a list of supported timezone IDs.
options.userAgentstringbrowserSpecifies the user agent to use in the context.
options.viewportobject{'width': 1280, 'height': 720}Sets a viewport size for all pages in the context. null disables the default viewport.
options.viewport.widthnumber1280Page width in pixels.
options.viewport.heightnumber720Page height in pixels.

Returns

TypeDescription
Promise<BrowserContext>A Promise that fulfills with a BrowserContext object.

deviceScaleFactor example

JavaScript
import { browser } from 'k6/browser';

export const options = {
  scenarios: {
    browser: {
      executor: 'shared-iterations',
      options: {
        browser: {
          type: 'chromium',
        },
      },
    },
  },
};

export default async function () {
  const context = await browser.newContext({
    viewport: {
      width: 375,
      height: 812,
    },
    deviceScaleFactor: 3,
  });
  const page = await context.newPage();

  try {
    await page.goto('https://test.k6.io/');
  } finally {
    await page.close();
  }
}