Skip to content

Commit

Permalink
Multiple Environments: Test on a single PW test that it can pick up U…
Browse files Browse the repository at this point in the history
…RL value from a dotenv file
  • Loading branch information
ashwiniraokarai committed May 2, 2024
1 parent f0943fb commit 4728c97
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 1 deletion.
2 changes: 2 additions & 0 deletions env/.env.local
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
URL="http://localhost:3000"
WHERE="local machine, relax"
2 changes: 2 additions & 0 deletions env/.env.prod
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
URL="https://todomvc.com/examples/emberjs/todomvc/dist/"
WHERE="!! PRODUCTION !!"
19 changes: 19 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@
"devDependencies": {
"@playwright/test": "^1.43.1",
"@types/node": "^20.12.7"
},
"dependencies": {
"dotenv": "^16.4.5"
}
}
5 changes: 5 additions & 0 deletions playwright.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { defineConfig, devices } from '@playwright/test';
import dotenv from 'dotenv';

dotenv.config({
path: './env/.env.prod'
})

/**
* Read environment variables from file.
Expand Down
9 changes: 8 additions & 1 deletion tests/adding-items.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,15 @@ let navigate: Navigate;

test.beforeEach(
async({ page })=>{
console.log(process.env.URL);
console.log(process.env.WHERE);
navigate = new Navigate(page);
await navigate.toHomePage();

//cast to string so the arg type (string | undefined) becomes compatible with the expected type (string)
page.goto(process.env.URL as string);

//uncomment when you're done confirming that the process.env.URL is picking up the value
//await navigate.toHomePage();

//Invoke the page object to grab locator(s)
todoForm = new TodoForm(page);
Expand Down

1 comment on commit 4728c97

@ashwiniraokarai
Copy link
Owner Author

@ashwiniraokarai ashwiniraokarai commented on 4728c97 May 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need the dotenv module installed on your machine before you can import it in your code files:
npm install dotenv --save //the --save flag saves the dependency into package.json for you

Please sign in to comment.