Hello guys, I have been working some weeks ago with playwright, and it's have been amazing, but until today.
My team and I we had a project with React, using webpack 4.
Today we migrated the project to vite/typescript and here start the problems. when i run my project with playwright i get the error window is not defined, in a specific file that i use in the frontend. I don't understand why this happens, can someone help me?
This thread is trying to answer question "Why am I getting a 'window is not defined' error when running my project with playwright after migrating from webpack 4 to vite/typescript?"
my playwright config
import { defineConfig, devices } from '@playwright/test';
/**
* Read environment variables from file.
* https://github.com/motdotla/dotenv
*/
// require('dotenv').config();
/**
* See https://playwright.dev/docs/test-configuration.
*/
export default defineConfig({
testDir: './e2e',
/* Run tests in files in parallel */
fullyParallel: true,
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
reporter: 'html',
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
use: {
/* Base URL to use in actions like `await page.goto('/')`. */
// baseURL: 'http://127.0.0.1:3000',
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
trace: 'on-first-retry',
},
/* Configure projects for major browsers */
projects: [
{ name: 'setup', testMatch: /.*\.setup\.ts/ },
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] },
dependencies: ['setup']
},
],
});
to access window you need to run code in page.evaluate, https://playwright.dev/docs/api/class-page#page-evaluate
or evaluateHandle perhaps, it has an example with window
https://playwright.dev/docs/api/class-page#page-evaluate-handle
Did you solve the SyntaxError: Cannot use 'import.meta' outside a module
?
Because I had this problem too, then also added "type": "module"
in the package.json
but I still get the same error. The package that uses import.meta
is an api package in a monorepo that is only consumed by the apps but does not run by itself.
Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].