From f56096f47eba6690ab4ba7723d884ba54b56240c Mon Sep 17 00:00:00 2001 From: charlyautomatiza Date: Sat, 5 Aug 2023 12:40:50 -0300 Subject: [PATCH 1/3] test: get planet Nuevos tests para validar que un planeta exista y que no exista. --- .github/workflows/pipeline.yml | 27 +++++++++++++++++-- .gitignore | 5 +++- README.md | 24 +++++++++++++++++ package-lock.json | 49 ++++++++++++++++++++++++++++++++++ package.json | 1 + playwright.config.ts | 48 +++++++++++++++++++++++++++++++++ tests/ping.setup.ts | 28 +++++++++++++++++++ tests/planets.spec.ts | 45 +++++++++++++++++++++++++++++++ 8 files changed, 224 insertions(+), 3 deletions(-) create mode 100644 playwright.config.ts create mode 100644 tests/ping.setup.ts create mode 100644 tests/planets.spec.ts diff --git a/.github/workflows/pipeline.yml b/.github/workflows/pipeline.yml index 426c1c4..70c69e1 100644 --- a/.github/workflows/pipeline.yml +++ b/.github/workflows/pipeline.yml @@ -1,10 +1,12 @@ -name: Render +name: Deploy + Tests on: push: branches: [main] + pull_request: + branches: [main] jobs: deploy: - name: Wait for Deploy + timeout-minutes: 60 runs-on: ubuntu-latest steps: - name: Wait for Render Deployment @@ -16,3 +18,24 @@ jobs: retries: 20 wait: 16000 sleep: 30000 + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 + with: + node-version: 18 + - name: Install dependencies + run: npm ci + - name: Install Playwright Browsers + run: npx playwright install --with-deps + - name: Run Playwright tests + run: npx playwright test + - uses: actions/upload-artifact@v3 + if: always() + with: + name: playwright-report + path: playwright-report/ + retention-days: 30 + - name: Publish JUnit Report + uses: mikepenz/action-junit-report@v3 + if: always() # always run even if the previous step fails + with: + report_paths: 'junit-results/*.xml' diff --git a/.gitignore b/.gitignore index 935306f..68b91a2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ build node_modules -.env \ No newline at end of file +.env +/*-results/ +/playwright-report/ +/playwright/.cache/ diff --git a/README.md b/README.md index 1114e92..61a8ef4 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,31 @@ Local: localhost:8000/api/planet ```http GET /api/planet ``` +# Ejecutando las pruebas con Playwright +```bash + # Runs the end-to-end tests. + npx playwright test +``` +```bash + # Starts the interactive UI mode. + npx playwright test --ui +``` + +```bash + # Runs the tests only on Desktop Chrome. + npx playwright test --project=chromium +``` + +```bash + # Runs the tests in a specific file. + npx playwright test example +``` + +```bash + # Runs the tests in debug mode. + npx playwright test --debug +``` ## Diagrama de la base de datos diff --git a/package-lock.json b/package-lock.json index c53951f..3e88a48 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18,6 +18,7 @@ "devDependencies": { "@commitlint/cli": "17.0.3", "@commitlint/config-conventional": "17.0.3", + "@playwright/test": "^1.36.2", "@types/cors": "2.8.12", "@types/express": "4.17.13", "@types/morgan": "1.9.3", @@ -1434,6 +1435,25 @@ "node": ">= 8" } }, + "node_modules/@playwright/test": { + "version": "1.36.2", + "resolved": "https://registry.npmjs.org/@playwright/test/-/test-1.36.2.tgz", + "integrity": "sha512-2rVZeyPRjxfPH6J0oGJqE8YxiM1IBRyM8hyrXYK7eSiAqmbNhxwcLa7dZ7fy9Kj26V7FYia5fh9XJRq4Dqme+g==", + "dev": true, + "dependencies": { + "@types/node": "*", + "playwright-core": "1.36.2" + }, + "bin": { + "playwright": "cli.js" + }, + "engines": { + "node": ">=16" + }, + "optionalDependencies": { + "fsevents": "2.3.2" + } + }, "node_modules/@prisma/client": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/@prisma/client/-/client-5.0.0.tgz", @@ -5949,6 +5969,18 @@ "node": ">=8" } }, + "node_modules/playwright-core": { + "version": "1.36.2", + "resolved": "https://registry.npmjs.org/playwright-core/-/playwright-core-1.36.2.tgz", + "integrity": "sha512-sQYZt31dwkqxOrP7xy2ggDfEzUxM1lodjhsQ3NMMv5uGTRDsLxU0e4xf4wwMkF2gplIxf17QMBCodSFgm6bFVQ==", + "dev": true, + "bin": { + "playwright-core": "cli.js" + }, + "engines": { + "node": ">=16" + } + }, "node_modules/plimit-lit": { "version": "1.5.0", "resolved": "https://registry.npmjs.org/plimit-lit/-/plimit-lit-1.5.0.tgz", @@ -8706,6 +8738,17 @@ "fastq": "^1.6.0" } }, + "@playwright/test": { + "version": "1.36.2", + "resolved": "https://registry.npmjs.org/@playwright/test/-/test-1.36.2.tgz", + "integrity": "sha512-2rVZeyPRjxfPH6J0oGJqE8YxiM1IBRyM8hyrXYK7eSiAqmbNhxwcLa7dZ7fy9Kj26V7FYia5fh9XJRq4Dqme+g==", + "dev": true, + "requires": { + "@types/node": "*", + "fsevents": "2.3.2", + "playwright-core": "1.36.2" + } + }, "@prisma/client": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/@prisma/client/-/client-5.0.0.tgz", @@ -12066,6 +12109,12 @@ } } }, + "playwright-core": { + "version": "1.36.2", + "resolved": "https://registry.npmjs.org/playwright-core/-/playwright-core-1.36.2.tgz", + "integrity": "sha512-sQYZt31dwkqxOrP7xy2ggDfEzUxM1lodjhsQ3NMMv5uGTRDsLxU0e4xf4wwMkF2gplIxf17QMBCodSFgm6bFVQ==", + "dev": true + }, "plimit-lit": { "version": "1.5.0", "resolved": "https://registry.npmjs.org/plimit-lit/-/plimit-lit-1.5.0.tgz", diff --git a/package.json b/package.json index 2141b97..58dc8d0 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,7 @@ "devDependencies": { "@commitlint/cli": "17.0.3", "@commitlint/config-conventional": "17.0.3", + "@playwright/test": "^1.36.2", "@types/cors": "2.8.12", "@types/express": "4.17.13", "@types/morgan": "1.9.3", diff --git a/playwright.config.ts b/playwright.config.ts new file mode 100644 index 0000000..cf06624 --- /dev/null +++ b/playwright.config.ts @@ -0,0 +1,48 @@ +import { defineConfig, devices } from '@playwright/test' + +/** + * 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', + /* 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', { open: 'never' }], + ['junit', { outputFile: './junit-results/results.xml' }], + ], + /* 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://127.0.0.1:3000', + + /* 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: 'ping', + testMatch: /ping\.setup\.ts/, + }, + { + name: 'chromium', + use: { ...devices['Desktop Chrome'] }, + dependencies: ['ping'], + }, + ], +}) diff --git a/tests/ping.setup.ts b/tests/ping.setup.ts new file mode 100644 index 0000000..54a7db1 --- /dev/null +++ b/tests/ping.setup.ts @@ -0,0 +1,28 @@ +import { test as ping, expect, APIRequestContext, test } from '@playwright/test' + +// Request context is reused by all tests in the file. +let apiContext: APIRequestContext + +ping.beforeAll(async ({ playwright }) => { + apiContext = await playwright.request.newContext({ + // All requests we send go to this API endpoint. + baseURL: 'https://planetas-api.onrender.com/', + extraHTTPHeaders: { + Accept: 'application/json', + }, + }) +}) + +ping.afterAll(async () => { + // Dispose all responses. + await apiContext.dispose() +}) + +/** + * API call to wake up our app + */ +ping('ping a planet', async () => { + const planet = await apiContext.get('/api/planet') + test.slow() + expect(planet.ok()).toBeTruthy() +}) diff --git a/tests/planets.spec.ts b/tests/planets.spec.ts new file mode 100644 index 0000000..4ae198a --- /dev/null +++ b/tests/planets.spec.ts @@ -0,0 +1,45 @@ +import { test, expect, APIRequestContext } from '@playwright/test' + +// Request context is reused by all tests in the file. +let apiContext: APIRequestContext + +test.beforeAll(async ({ playwright }) => { + apiContext = await playwright.request.newContext({ + // All requests we send go to this API endpoint. + baseURL: 'https://planetas-api.onrender.com/', + extraHTTPHeaders: { + Accept: 'application/json', + }, + }) +}) + +test.afterAll(async () => { + // Dispose all responses. + await apiContext.dispose() +}) + +/** + * Sample API call. + */ +test('get a planet', async () => { + const planet = await apiContext.get('/api/planet/jupiter') + expect(planet.ok()).toBeTruthy() + expect(await planet.json()).toEqual( + expect.objectContaining({ + id: '64c3ad5358fda7cddbe38a63', + name: 'Jupiter', + rotation: '9.93 hours', + revolution: '11.86 years', + radius: '69,911 km', + temperature: '-108°c', + }), + ) +}) + +/** + * Sample API call. + */ +test('get an inexistent planet', async () => { + const planet = await apiContext.get('/api/planet/badplanet') + expect(planet.status()).toEqual(404) +}) From 6cf6b6e838871ce3922b42e36dc1f44f6d368d34 Mon Sep 17 00:00:00 2001 From: charlyautomatiza Date: Sat, 5 Aug 2023 18:04:59 -0300 Subject: [PATCH 2/3] test: get planet Nuevos tests para validar que un planeta exista y que no exista. --- .github/workflows/pipeline.yml | 54 ++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 22 deletions(-) diff --git a/.github/workflows/pipeline.yml b/.github/workflows/pipeline.yml index 70c69e1..55f0aae 100644 --- a/.github/workflows/pipeline.yml +++ b/.github/workflows/pipeline.yml @@ -5,9 +5,10 @@ on: pull_request: branches: [main] jobs: - deploy: + check-deploy: timeout-minutes: 60 runs-on: ubuntu-latest + name: 👷🏻‍♀️ Check deploy steps: - name: Wait for Render Deployment uses: bounceapp/render-action@0.6.0 @@ -18,24 +19,33 @@ jobs: retries: 20 wait: 16000 sleep: 30000 - - uses: actions/checkout@v3 - - uses: actions/setup-node@v3 - with: - node-version: 18 - - name: Install dependencies - run: npm ci - - name: Install Playwright Browsers - run: npx playwright install --with-deps - - name: Run Playwright tests - run: npx playwright test - - uses: actions/upload-artifact@v3 - if: always() - with: - name: playwright-report - path: playwright-report/ - retention-days: 30 - - name: Publish JUnit Report - uses: mikepenz/action-junit-report@v3 - if: always() # always run even if the previous step fails - with: - report_paths: 'junit-results/*.xml' + testing: + needs: [ check-deploy ] + runs-on: ubuntu-latest + name: 🧐 Testing + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 + with: + node-version: '18.x' + - name: Install dependencies + run: npm ci + - name: Install Playwright + run: npx playwright install --with-deps + - name: Run Playwright tests + run: npx playwright test + - uses: actions/upload-artifact@v3 + if: always() + with: + name: playwright-test-results + path: test-results/ + - uses: actions/upload-artifact@v3 + if: always() + with: + name: allure-test-results + path: allure-report/ + - name: Publish JUnit Report + uses: mikepenz/action-junit-report@v3 + if: always() # always run even if the previous step fails + with: + report_paths: 'junit-results/*.xml' From c05af6c8f536a6c6975bd6c7590344b36f7e036a Mon Sep 17 00:00:00 2001 From: charlyautomatiza Date: Sat, 5 Aug 2023 18:52:46 -0300 Subject: [PATCH 3/3] test: get planet Nuevos tests para validar que un planeta exista y que no exista. --- .github/workflows/pipeline.yml | 54 ++++++++++++++++------------------ tests/planets.spec.ts | 8 +++-- 2 files changed, 32 insertions(+), 30 deletions(-) diff --git a/.github/workflows/pipeline.yml b/.github/workflows/pipeline.yml index 55f0aae..440a750 100644 --- a/.github/workflows/pipeline.yml +++ b/.github/workflows/pipeline.yml @@ -2,8 +2,6 @@ name: Deploy + Tests on: push: branches: [main] - pull_request: - branches: [main] jobs: check-deploy: timeout-minutes: 60 @@ -20,32 +18,32 @@ jobs: wait: 16000 sleep: 30000 testing: - needs: [ check-deploy ] + needs: [check-deploy] runs-on: ubuntu-latest name: 🧐 Testing steps: - - uses: actions/checkout@v3 - - uses: actions/setup-node@v3 - with: - node-version: '18.x' - - name: Install dependencies - run: npm ci - - name: Install Playwright - run: npx playwright install --with-deps - - name: Run Playwright tests - run: npx playwright test - - uses: actions/upload-artifact@v3 - if: always() - with: - name: playwright-test-results - path: test-results/ - - uses: actions/upload-artifact@v3 - if: always() - with: - name: allure-test-results - path: allure-report/ - - name: Publish JUnit Report - uses: mikepenz/action-junit-report@v3 - if: always() # always run even if the previous step fails - with: - report_paths: 'junit-results/*.xml' + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 + with: + node-version: '18.x' + - name: Install dependencies + run: npm ci + - name: Install Playwright + run: npx playwright install --with-deps + - name: Run Playwright tests + run: npx playwright test + - uses: actions/upload-artifact@v3 + if: always() + with: + name: playwright-test-results + path: test-results/ + - uses: actions/upload-artifact@v3 + if: always() + with: + name: allure-test-results + path: allure-report/ + - name: Publish JUnit Report + uses: mikepenz/action-junit-report@v3 + if: always() # always run even if the previous step fails + with: + report_paths: 'junit-results/*.xml' diff --git a/tests/planets.spec.ts b/tests/planets.spec.ts index 4ae198a..4829e38 100644 --- a/tests/planets.spec.ts +++ b/tests/planets.spec.ts @@ -3,6 +3,7 @@ import { test, expect, APIRequestContext } from '@playwright/test' // Request context is reused by all tests in the file. let apiContext: APIRequestContext +// Before all tests, create a new API request context. test.beforeAll(async ({ playwright }) => { apiContext = await playwright.request.newContext({ // All requests we send go to this API endpoint. @@ -13,17 +14,20 @@ test.beforeAll(async ({ playwright }) => { }) }) +// After all tests, dispose all responses. test.afterAll(async () => { // Dispose all responses. await apiContext.dispose() }) /** - * Sample API call. + * Get a planet. */ test('get a planet', async () => { const planet = await apiContext.get('/api/planet/jupiter') + // Expect the response to be OK. expect(planet.ok()).toBeTruthy() + // Expect the response to contain the expected data. expect(await planet.json()).toEqual( expect.objectContaining({ id: '64c3ad5358fda7cddbe38a63', @@ -37,7 +41,7 @@ test('get a planet', async () => { }) /** - * Sample API call. + * Try to get an inexistent planet */ test('get an inexistent planet', async () => { const planet = await apiContext.get('/api/planet/badplanet')