Back to template gallery

Playwright + Chrome Test Runner

Example of using the Playwright Test project to run automated website tests in the cloud and display their results. Usable as an API.






Use cases

Web scraping

import { Actor } from 'apify';
import log from '@apify/log';
import { Dictionary } from 'apify-client';
import fs from 'fs';
import path from 'path';
import { execSync } from 'child_process';
import { collectAttachmentPaths, transformToTabular } from './transform';

function ensureFolder(pathname: string) {
    if (!fs.existsSync(pathname)) {
        fs.mkdirSync(pathname, { recursive: true });

function getConfigPath(){
    return `${__dirname}/../playwright.config.ts`;

function getResultDir(){
    return `${__dirname}/../playwright-report`;

const getConfig = (options: {screen: {width: number, height: number}, headful: boolean, timeout: number, locale: string, darkMode: boolean, ignoreHTTPSErrors: boolean, video: string}) => {
    const {screen, headful, timeout, ignoreHTTPSErrors, darkMode, locale, video} = options;

    return `
// Watch out! This file gets regenerated on every run of the actor.
// Any changes you make will be lost.

// Tweak your configuration through the Actor's input through the Apify console or directly in the \`input.json\` file.
import { defineConfig } from '@playwright/test';
export default defineConfig({
    timeout: ${timeout},
    use: {
        headless: ${!headful},
        viewport: { width: ${screen.width}, height: ${screen.height} },
        ignoreHTTPSErrors: ${ignoreHTTPSErrors},
        colorScheme: '${darkMode ? 'dark' : 'light'}',
        locale: '${locale}',
        video: '${video}',
    reporter: [
        ['html', { outputFolder: '${getResultDir()}', open: 'never' }],
        ['json', { outputFile: '${getResultDir()}/test-results.json' }]
function runTests() {
    try {
        execSync(`npx playwright test --config=${getConfigPath()}`, {
            cwd: __dirname,
            encoding: 'utf8',
            stdio: 'inherit',
    } catch (e) {
        // suppress error, the report will be generated anyway

function updateConfig(args: {
    screenWidth?: number,
    screenHeight?: number,
    headful?: boolean,
    timeout?: number,
    darkMode?: boolean,
    locale?: string,
    ignoreHTTPSErrors?: boolean,
    video?: string,
}) {
    const {
        screenWidth = 1280,
        screenHeight =  720,
        headful = false,
        timeout = 60,
        darkMode = false,
        locale = 'en-US',
        ignoreHTTPSErrors = true,
        video = 'off'
    } = args;

    const config = getConfig({screen: { width: screenWidth, height: screenHeight }, headful, timeout: timeout * 1000, locale, darkMode, ignoreHTTPSErrors, video});
    fs.writeFileSync(getConfigPath(), config, { encoding: 'utf-8' });

(async () => {
    await Actor.init();
    const input = (await Actor.getInput() ?? {}) as Dictionary;



    const kvs = await Actor.openKeyValueStore();
    await kvs.setValue('report', fs.readFileSync(path.join(getResultDir(), 'index.html'), { encoding: 'utf-8' }), { contentType: 'text/html' });
    const jsonReport = JSON.parse(fs.readFileSync(path.join(getResultDir(), 'test-results.json'), { encoding: 'utf-8' }));
    const attachmentPaths = collectAttachmentPaths(jsonReport);

    const attachmentLinks = await Promise.all( (x) => {
        const attachment = fs.readFileSync(x.path);
        await kvs.setValue(x.key, attachment, { contentType: x.type ?? 'application/octet' });
        return {...x, url: await kvs.getPublicUrl(x.key)};

    await Actor.pushData(transformToTabular(jsonReport, attachmentLinks));

    const reportURL = await kvs.getPublicUrl('report');'The test run has finished! The report is available in the Output tab or at the link below:');

    await Actor.exit();

Playwright test template

Run your Playwright tests on the Apify platform effectively and easily. Just set up your test environment using a user-friendly UI and let the platform do the rest.

Note: This is a custom version of Playwright Test Runner Actor. Unlike the original Actor, this version reads test suite files from the tests folder and does not allow you to pass the test files via Apify input.


Run your Playwright tests on the Apify platform

No more pre-commit hooks or CI/CD pipelines. Integrate your tests with the Apify Platform using a user-friendly UI and forget about the hassle of setting up your test environment.

Test configuration with comprehensive UI

Collect and analyze your test results online

After running the tests, the Apify platform stores the results in comprehensive datasets. You can view the results directly on the platform or download them to your local machine using a REST API.

Analyzing understandable test reports

No more problems with incompatible browser versions

Playwright Test toolkit automatically downloads the latest versions of Chromium, Firefox, and WebKit browsers and installs them in the Apify platform.

This way, you can test your websites using all the popular browsers without worrying about compatibility issues.

Testing with multiple browser versions at once

How to use

Just provide your test suite files in the tests folder and run the Actor. The Actor will automatically run all the tests in the tests folder and store the results in the KVS/dataset fields.

You can also customize the test run by specifying other options in the input, e.g. the screen size, headful/headless execution or the maximum run time.

Test Generator

You can also use the Playwright Codegen to compose your test suites even faster. Just run npm run codegen in your project folder and record your workflow.

The code generator will automatically create a test suite file for you and save it in the tests folder.


Already have a solution in mind?

Sign up for a free Apify account and deploy your code to the platform in just a few minutes! If you want a head start without coding it yourself, browse our Store of existing solutions.