Skip to content

Commit

Permalink
add playwright setup
Browse files Browse the repository at this point in the history
  • Loading branch information
mohanrajpac committed Feb 16, 2024
1 parent 7869f55 commit b88b89f
Show file tree
Hide file tree
Showing 10 changed files with 1,027 additions and 817 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/playwright.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Playwright Tests
on:
push:
branches: [ main, master ]
pull_request:
branches: [ main, master ]
jobs:
test:
timeout-minutes: 60
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 18
- name: Install dependencies
run: npm install -g yarn && yarn
- name: Install Playwright Browsers
run: yarn playwright install --with-deps
- name: Run Playwright tests
run: yarn playwright test
- uses: actions/upload-artifact@v3
if: always()
with:
name: playwright-report
path: playwright-report/
retention-days: 30
9 changes: 8 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,11 @@ yarn-error.log
.DS_Store
testingdb
.vscode
.env
.env
.auth/
artifacts/
tests-examples/
/test-results/
/playwright-report/
/blob-report/
/playwright/.cache/
29 changes: 16 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,34 @@
"prepare": "husky install"
},
"devDependencies": {
"@testing-library/jest-dom": "^6.2.1",
"@testing-library/react": "14.1.2",
"@types/jest": "^29.5.11",
"@types/node": "^20.11.5",
"@playwright/test": "^1.41.2",
"@testing-library/jest-dom": "^6.4.2",
"@testing-library/react": "14.2.1",
"@types/jest": "^29.5.12",
"@types/node": "^20.11.17",
"@types/react-syntax-highlighter": "^15.5.11",
"@types/wordpress__components": "^23.0.11",
"@wordpress/eslint-plugin": "^17.6.0",
"@wordpress/scripts": "^27.0.0",
"@wordpress/e2e-test-utils-playwright": "^0.19.0",
"@wordpress/eslint-plugin": "^17.8.0",
"@wordpress/scripts": "^27.2.0",
"eslint": "^8.56.0",
"eslint-import-resolver-alias": "^1.1.2",
"eslint-plugin-cypress": "^2.15.1",
"eslint-plugin-import": "^2.29.1",
"husky": "^8.0.0",
"husky": "^9.0.11",
"jest-environment-jsdom": "^29.7.0",
"prettier": "^3.2.4"
"prettier": "^3.2.5"
},
"keywords": [],
"author": "Mohan Raj <https://mohanraj.dev>",
"license": "ISC",
"dependencies": {
"@wordpress/api-fetch": "^6.46.0",
"@wordpress/components": "^25.15.0",
"@wordpress/data": "^9.19.0",
"@wordpress/i18n": "^4.49.0",
"@wordpress/notices": "^4.17.0",
"@wordpress/api-fetch": "^6.48.0",
"@wordpress/components": "^26.0.1",
"@wordpress/data": "^9.21.0",
"@wordpress/i18n": "^4.51.0",
"@wordpress/notices": "^4.19.0",
"dotenv": "^16.4.4",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-syntax-highlighter": "^15.5.0",
Expand Down
81 changes: 81 additions & 0 deletions playwright.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { defineConfig, devices } from '@playwright/test';
import dotenv from 'dotenv';

dotenv.config();

/**
* Read environment variables from file.
* https://github.com/motdotla/dotenv
*/
// require('dotenv').config();

/**
* See https://playwright.dev/docs/test-configuration.
*/
export default defineConfig({
testDir: 'tests/e2e',
globalSetup: 'tests/e2e/global-setup.ts',
/* Run tests in files in parallel */
fullyParallel: true,
/* Fail the build on CI if you accidentally left test.only in the source code. */
forbidOnly: !!process.env.CI,
/* Retry on CI only */
retries: process.env.CI ? 2 : 0,
/* Opt out of parallel tests on CI. */
workers: process.env.CI ? 1 : undefined,
/* 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://localhost:8080',

/* 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: 'chromium',
use: { ...devices['Desktop Chrome'] },
},

// {
// name: 'firefox',
// use: { ...devices['Desktop Firefox'] },
// },

// {
// name: 'webkit',
// use: { ...devices['Desktop Safari'] },
// },

/* Test against mobile viewports. */
// {
// name: 'Mobile Chrome',
// use: { ...devices['Pixel 5'] },
// },
// {
// name: 'Mobile Safari',
// use: { ...devices['iPhone 12'] },
// },

/* Test against branded browsers. */
// {
// name: 'Microsoft Edge',
// use: { ...devices['Desktop Edge'], channel: 'msedge' },
// },
// {
// name: 'Google Chrome',
// use: { ...devices['Desktop Chrome'], channel: 'chrome' },
// },
],

/* Run your local dev server before starting the tests */
// webServer: {
// command: 'npm run start',
// url: 'http://127.0.0.1:3000',
// reuseExistingServer: !process.env.CI,
// },
});
1 change: 1 addition & 0 deletions src/components/flag/SubmitControls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const SubmitControls = ({
onClick={handleNewFlag}
style={{ marginRight: 15 }}
icon={'plus'}
id="add-flag"
>
{__('Add Flag', 'mr-feature-flags')}
</Button>
Expand Down
25 changes: 25 additions & 0 deletions tests/e2e/global-setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { request } from '@playwright/test';
import { RequestUtils } from '@wordpress/e2e-test-utils-playwright';

// To interact with the WP Guest Bar plugin's settings page, we must be authenticated.
// Before any tests are run, we sign in, save the cookies set by WordPress, and then discard the session.
// Later, when we need to act as a logged-in user, we make those cookies available.
// https://playwright.dev/docs/test-global-setup-teardown#configure-globalsetup-and-globalteardown
export default async function globalSetup() {
const requestContext = await request.newContext({
baseURL: process.env.WP_BASE_URL,
});
const requestUtils = new RequestUtils(requestContext, {
storageStatePath: process.env.WP_AUTH_STORAGE,
user: {
username: process.env.WP_USERNAME || '',
password: process.env.WP_PASSWORD || '',
},
});

// Alternatively, we could take a more traditional route,
// filling in the input fields for the username and password and submitting the form.
// https://playwright.dev/docs/test-global-setup-teardown#example
await requestUtils.setupRest();
await requestContext.dispose();
}
8 changes: 8 additions & 0 deletions tests/e2e/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
declare namespace NodeJS {
interface ProcessEnv {
WP_AUTH_STORAGE: string;
WP_BASE_URL: string;
WP_USERNAME: string;
WP_PASSWORD: string;
}
}
27 changes: 27 additions & 0 deletions tests/e2e/wp-admin.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { test, expect } from '@wordpress/e2e-test-utils-playwright';

// We have multiple tests in this file, all requiring us to be authenticated.
// Compare this to the front-end.spec.ts.
test.use({ storageState: process.env.WP_AUTH_STORAGE });

test.describe('Feature flags', () => {
test('Navigate to feature flag settings', async ({ page, admin }) => {
await admin.visitAdminPage('/');

await page.getByRole('link', { name: 'Feature Flags' }).click();

await expect(
page.getByRole('heading', { name: 'Feature Flags settings' })
).toBeVisible();

await page.getByRole('button', { name: 'Add Flag' }).click();

await expect(await page.getByRole('textbox').count()).toBe(3);

await page.getByRole('textbox').last().fill('hello');

await page.getByRole('button', { name: 'Save' }).click();

await expect(await page.getByText('Saved')).toBeVisible();
});
});
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@
"isolatedModules": true,
"types": ["node"]
},
"include": [ "src", "types/window.d.ts"],
"include": [ "src", "types/window.d.ts", "tests/e2e/global-setup.ts"],
}
Loading

0 comments on commit b88b89f

Please sign in to comment.