Playwright Selenium Grid Example

The Playwright selenium grid example below can quickly be run on a free Gridlastic selenium grid and at much lower cost per minute than other cloud services.

playwright.config.ts

import { defineConfig, devices } from "@playwright/test";
import { Buffer } from 'buffer';

//process.env.DEBUG = "pw:browser";
process.env.SELENIUM_REMOTE_URL = ""; // Like https://subdomain-hub.gridlastic.com/wd/hub
process.env.SELENIUM_REMOTE_CAPABILITIES = '{"platformName":"WIN11","browserName":"chrome","browserVersion":"latest","gridlastic:options":{"video":true}}';

const GRIDLASTIC_GRID_USERNAME = '';
const GRIDLASTIC_GRID_PASSWORD = '';
const gridlastic_grid_credentials = `${GRIDLASTIC_GRID_USERNAME}:${GRIDLASTIC_GRID_PASSWORD}`;
const gridlastic_grid_encodedCredentials = Buffer.from(gridlastic_grid_credentials).toString('base64');
process.env.SELENIUM_REMOTE_HEADERS=`{"Authorization":"Basic ${gridlastic_grid_encodedCredentials}"}`;

global.GRIDLASTIC_GRID_VIDEO_URL = ''; // like: https://s3-us-west-1.amazonaws.com/023a15f2-534d-31e5-f8cc-7ceaf6355377/238a51b7-c526-ceb8-8ffd-1759b612a464/play.html?

export default defineConfig({
  testDir: './tests',
  retries: 3,  // If there are no grid nodes to process your tests available, it can take several minutes to launch new ones and your test might timeout. You can also pre-launch grid nodes, https://www.gridlastic.com/pre-launch-api.html
});

example.spec.ts

import { test, expect } from '@playwright/test';

test.afterEach(async ({ page }) => {

  await page.goto('http://localhost:62000/get-selenium-session-id');
  const selenium_session_id = await page.innerText('body');
  console.log("Video URL: " + GRIDLASTIC_GRID_VIDEO_URL + selenium_session_id);

await page.waitForTimeout(3000); // wait for demo purposes
});


test('check title', async ({ page }) => {
  await page.goto('https://www.gridlastic.com/?demo');
  await page.setViewportSize({ width: 1536, height: 864 });
  // Check title.
  await expect(page).toHaveTitle(/Selenium grid/);
  await page.waitForTimeout(5000); // wait for demo purposes
});

The Gridlastic grid hostname, credentials and video url used in these code examples are presented after launching your Gridlastic selenium grid.

Get a free account and launch your Gridlastic selenium grid, then run this code locally and test your grid!