Reputation: 11931
I am unable to get the url value from the .env
file in my playwright
test, while running the test, it is throwing error baseURL of undefined,could someone please advise on the issue ?
TypeError: Cannot read property 'baseURL' of undefined
10 | test('Access the Playwright Page', async ({ page }) => {
11 | const playwrightDev = new PlaywrightDevPage(page);
> 12 | await playwrightDev.goto(Config.baseURL);
| ^
Following is my .env
file.
URL=https://playwright.dev/
Below is my playwright.config.js
file and settings
const { devices } = require('@playwright/test');
/** @type {import('@playwright/test').PlaywrightTestConfig} */
const config = {
forbidOnly: !!process.env.CI,
retries: process.env.CI ? 2 : 0,
use: {
trace: 'on-first-retry',
baseURL: process.env.URL,
},
projects: [
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] },
},
],
};
module.exports = config;
Below is my example.spec.js
file
const { test, expect } = require('@playwright/test');
const { PlaywrightDevPage } = require('./playwright-dev-page');
const { Config } = require('../playwright.config');
test('Access the Playwright Page', async ({ page }) => {
const playwrightDev = new PlaywrightDevPage(page);
await playwrightDev.goto(Config.baseURl);
});
Upvotes: 3
Views: 7808
Reputation: 409
step 1 : npm install dotenv --save
step 2 : Create an .env file in root of the project
step 3 : Add env veriable like this
BASE_URL = 'https://example.net'
USERNAME = 'username'
PASSWORD = 'password'
step 4 : go to playwrigth.config.ts file and add the following block
import dotenv from 'dotenv'
dotenv.config();
step 5 :
const baseUrl = process.env.BASE_URL as string;
const userName = process.env.USERNAME as string;
const password = process.env.PASSWORD as string;
step 6 : use this in your code. Happy coding :)
Upvotes: 4
Reputation: 3152
You need to load dotenv
inside your config via: require('dotenv').config()
Then you can inside your tests use the env vars of your "dotenv files" via e.g.: process.env.FOOBAR
See here for dotenv usage: https://www.npmjs.com/package/dotenv
Upvotes: 5