From 3783227cc2b3e2100aeff28e1c22492f4f08a403 Mon Sep 17 00:00:00 2001 From: HannahZhuSWE <76960450+HannahZhuSWE@users.noreply.github.com> Date: Wed, 20 Jul 2022 11:05:43 -0700 Subject: [PATCH] Create Playwright test harness (#7) * updated host.json and added test harness * updated github action for test * added installation of saw and azure function core tools to github action --- .github/workflows/playwright.yml | 36 ++++++++++++++ .gitignore | 4 ++ api/host.json | 2 +- api/package-lock.json | 10 +++- package-lock.json | 74 +++++++++++++++++++++++++++++ package.json | 24 ++++++++++ playwright.config.ts | 80 ++++++++++++++++++++++++++++++++ tests/Test.README.md | 3 ++ tests/playwright.spec.ts | 8 ++++ 9 files changed, 239 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/playwright.yml create mode 100644 .gitignore create mode 100644 package-lock.json create mode 100644 package.json create mode 100644 playwright.config.ts create mode 100644 tests/Test.README.md create mode 100644 tests/playwright.spec.ts diff --git a/.github/workflows/playwright.yml b/.github/workflows/playwright.yml new file mode 100644 index 0000000..f2a6294 --- /dev/null +++ b/.github/workflows/playwright.yml @@ -0,0 +1,36 @@ +name: Playwright Tests + +# Controls when the workflow will run +on: + # Triggers the workflow on push or pull request and nightly + push: + pull_request: + schedule: + # nightly + - cron: '0 0 * * *' + + # Allows you to run this workflow manually from the Actions tab + workflow_dispatch: + +# A workflow run is made up of one or more jobs that can run sequentially or in parallel +jobs: + playwright_tests: + # Runs on an ubuntu runner + runs-on: ubuntu-latest + + strategy: + matrix: + node-version: [14.x, 16.x, 18.x] + # See supported Node.js release schedule at https://nodejs.org/en/about/releases/ + + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 + - name: Install dependencies + run: npm ci + - name: Install SWA and Azure Function tool + run: npm install -g @azure/static-web-apps-cli azure-functions-core-tools + - name: Install Playwright + run: npx playwright install --with-deps + - name: Run playwright tests + run: npm run playwright_test diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..75e854d --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +node_modules/ +/test-results/ +/playwright-report/ +/playwright/.cache/ diff --git a/api/host.json b/api/host.json index d342a8e..3491593 100644 --- a/api/host.json +++ b/api/host.json @@ -2,6 +2,6 @@ "version": "2.0", "extensionBundle": { "id": "Microsoft.Azure.Functions.ExtensionBundle", - "version": "[1.*, 2.0.0)" + "version": "2.*" } } diff --git a/api/package-lock.json b/api/package-lock.json index 0bb6c76..e2b3871 100644 --- a/api/package-lock.json +++ b/api/package-lock.json @@ -1,5 +1,13 @@ { "name": "api", "version": "1.0.0", - "lockfileVersion": 1 + "lockfileVersion": 2, + "requires": true, + "packages": { + "": { + "name": "api", + "version": "1.0.0", + "devDependencies": {} + } + } } diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..4600096 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,74 @@ +{ + "name": "vanilla-api", + "version": "1.0.0", + "lockfileVersion": 2, + "requires": true, + "packages": { + "": { + "name": "vanilla-api", + "version": "1.0.0", + "license": "ISC", + "devDependencies": { + "@playwright/test": "^1.23.2" + } + }, + "node_modules/@playwright/test": { + "version": "1.23.2", + "resolved": "https://registry.npmjs.org/@playwright/test/-/test-1.23.2.tgz", + "integrity": "sha512-umaEAIwQGfbezixg3raSOraqbQGSqZP988sOaMdpA2wj3Dr6ykOscrMukyK3U6edxhpS0N8kguAFZoHwCEfTig==", + "dev": true, + "dependencies": { + "@types/node": "*", + "playwright-core": "1.23.2" + }, + "bin": { + "playwright": "cli.js" + }, + "engines": { + "node": ">=14" + } + }, + "node_modules/@types/node": { + "version": "18.0.3", + "resolved": "https://registry.npmjs.org/@types/node/-/node-18.0.3.tgz", + "integrity": "sha512-HzNRZtp4eepNitP+BD6k2L6DROIDG4Q0fm4x+dwfsr6LGmROENnok75VGw40628xf+iR24WeMFcHuuBDUAzzsQ==", + "dev": true + }, + "node_modules/playwright-core": { + "version": "1.23.2", + "resolved": "https://registry.npmjs.org/playwright-core/-/playwright-core-1.23.2.tgz", + "integrity": "sha512-UGbutIr0nBALDHWW/HcXfyK6ZdmefC99Moo4qyTr89VNIkYZuDrW8Sw554FyFUamcFSdKOgDPk6ECSkofGIZjQ==", + "dev": true, + "bin": { + "playwright": "cli.js" + }, + "engines": { + "node": ">=14" + } + } + }, + "dependencies": { + "@playwright/test": { + "version": "1.23.2", + "resolved": "https://registry.npmjs.org/@playwright/test/-/test-1.23.2.tgz", + "integrity": "sha512-umaEAIwQGfbezixg3raSOraqbQGSqZP988sOaMdpA2wj3Dr6ykOscrMukyK3U6edxhpS0N8kguAFZoHwCEfTig==", + "dev": true, + "requires": { + "@types/node": "*", + "playwright-core": "1.23.2" + } + }, + "@types/node": { + "version": "18.0.3", + "resolved": "https://registry.npmjs.org/@types/node/-/node-18.0.3.tgz", + "integrity": "sha512-HzNRZtp4eepNitP+BD6k2L6DROIDG4Q0fm4x+dwfsr6LGmROENnok75VGw40628xf+iR24WeMFcHuuBDUAzzsQ==", + "dev": true + }, + "playwright-core": { + "version": "1.23.2", + "resolved": "https://registry.npmjs.org/playwright-core/-/playwright-core-1.23.2.tgz", + "integrity": "sha512-UGbutIr0nBALDHWW/HcXfyK6ZdmefC99Moo4qyTr89VNIkYZuDrW8Sw554FyFUamcFSdKOgDPk6ECSkofGIZjQ==", + "dev": true + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..053f5b6 --- /dev/null +++ b/package.json @@ -0,0 +1,24 @@ +{ + "name": "vanilla-api", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "start": "swa start src --api-location api", + "playwright_test": "playwright test" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/HannahZhuSWE/vanilla-api.git" + }, + "keywords": [], + "author": "", + "license": "ISC", + "bugs": { + "url": "https://github.com/HannahZhuSWE/vanilla-api/issues" + }, + "homepage": "https://github.com/HannahZhuSWE/vanilla-api#readme", + "devDependencies": { + "@playwright/test": "^1.23.2" + } +} diff --git a/playwright.config.ts b/playwright.config.ts new file mode 100644 index 0000000..e855d99 --- /dev/null +++ b/playwright.config.ts @@ -0,0 +1,80 @@ +import type { PlaywrightTestConfig } from '@playwright/test'; +import { devices } from '@playwright/test'; + +/** + * Read environment variables from file. + * https://github.com/motdotla/dotenv + */ +// require('dotenv').config(); + +/** + * See https://playwright.dev/docs/test-configuration. + */ +const config: PlaywrightTestConfig = { + testDir: './tests', + /* Maximum time one test can run for. */ + timeout: 30 * 1000, + expect: { + /** + * Maximum time expect() should wait for the condition to be met. + * For example in `await expect(locator).toHaveText();` + */ + timeout: 5000 + }, + /* 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: { + /* Maximum time each action such as `click()` can take. Defaults to 0 (no limit). */ + actionTimeout: 0, + /* Base URL to use in actions like `await page.goto('/')`. */ + baseURL: 'http://localhost:4280', + + /* 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'], + }, + }, + + ], + + /* Folder for test artifacts such as screenshots, videos, traces, etc. */ + // outputDir: 'test-results/', + + /* Run your local dev server before starting the tests */ + webServer: { + command: 'npm run start', + port: 4280, + } +}; + +export default config; diff --git a/tests/Test.README.md b/tests/Test.README.md new file mode 100644 index 0000000..65f1284 --- /dev/null +++ b/tests/Test.README.md @@ -0,0 +1,3 @@ +# Testing + +To run playwright tests run `npm run playwright_test`. In order to run the playwright tests the start script `swa start src --api-location api` was added. \ No newline at end of file diff --git a/tests/playwright.spec.ts b/tests/playwright.spec.ts new file mode 100644 index 0000000..1cb7a8d --- /dev/null +++ b/tests/playwright.spec.ts @@ -0,0 +1,8 @@ +import { test, expect } from '@playwright/test'; + +test('basic test', async ({ page }) => { + await page.goto('/'); + await page.waitForSelector('h1') + await expect(page.locator('h1')).toContainText('Vanilla JavaScript App'); + await expect(page.locator('b')).toContainText('Hello from the API') +}) \ No newline at end of file