Udemy Playwright: Web Automation Testing From Zero to Hero: Difference between revisions

Jump to navigation Jump to search
m
Line 1,570: Line 1,570:


=== Environment Variables ===
=== Environment Variables ===
There are many ways to specify environment specific values
In file <code>playwright.config.ts</code>
<nowiki>
export default defineConfig<TestOptions>({
  ...
  use: {
    /* Base URL to use in actions like `await page.goto('/')`. */
    baseURL: 'http://localhost:4200',
    .. or ..
    baseURL: process.env.DEV ? 'http://the.dev.env.com'
    : process.env.STAGING ? 'http://the.staging.env.com'
    : 'http://localhost:4200',
  ...
  },
.. or ..
  /* Configure projects for major browsers */
  projects: [
    {
      name: "Dev",
      use: {
        baseURL: 'http://the.dev.env.com',       
      },
    },
    {
      name: "Staging",
      use: {
        baseURL: 'http://the.staging.env.com',       
      },
    },
...
</nowiki>
Or Extend
Create file <code>test-options.ts</code>
<nowiki>
import {test as base} from '@playwright/test'
export type TestOptions = {
    globalsQaURL: string
}
export const test = base.extend<TestOptions>({
    globalsQaURL: ['', {option:true}]
})</nowiki>
<nowiki>
import type { TestOptions } from "./test-options"
...
export default defineConfig<TestOptions>({
  ...
  use: {
    ...
    globalsQaURL: 'https://www.globalsqa.com/demo-site/draganddrop/',
    ...
  },
.. or ..
  /* Configure projects for major browsers */
  projects: [
    {
      name: "Dev",
      use: {
        ...
        globalsQaURL: 'https://dev.globalsqa.com/demo-site/draganddrop/',
        ...
      },
    },
...
</nowiki>
Then in <code>*.spec.ts</code>
<nowiki>
test("drag and drop with iframe", async ({ page, globalsQaURL }) => {
  await page.goto(globalsQaURL)
  ...</nowiki>
Using <code>process.env</code> OS environment variables (but there'll be shell and OS variations though)
Assuming bash shell.
Specify variable in file <code>package.json</code>
<nowiki>
  "scripts": {
    ...
    "pageObjects-chrome": "npx playwright test usePageObjects.spec.ts --project=chromium",
    "autoWait-dev": "URL=http://uitestingplayground.com/ajax npm run pageObjects-chrome"
  },</nowiki>
Then in <code>*.spec.ts</code>
<nowiki>
test.beforeEach(async ({ page }) => {
  await page.goto(process.env.URL)
  ...
})
...</nowiki>
Or use a <code>.env</code> file
In file <code>playwright.config.ts</code>
<nowiki>
...
/**
* Read environment variables from file.
* https://github.com/motdotla/dotenv
*/
require('dotenv').config(); //or ...config({ path: '/custom/path/to/.env' })
...</nowiki.
<nowiki>
URL=http://uitestingplayground.com/ajax
TESTUSERNAME=test@test.com
PASSWORD=Welcome1</nowiki>
Making sure to add <code>.env</code> to <code>.gitignore</code> to avoid leaking credentials to repo.
I like to create a <code>.env.template</code> file
<nowiki>
URL=http://uitestingplayground.com/ajax
TESTUSERNAME=test@test.com
PASSWORD=***REPLACE-ME***</nowiki>
=== Configuration File ===
=== Configuration File ===
=== Fixtures ===
=== Fixtures ===

Navigation menu