Skip to content

Commit

Permalink
tests: refactor testing
Browse files Browse the repository at this point in the history
  • Loading branch information
davidsneighbour committed Dec 8, 2024
1 parent 7b612d0 commit f8f1b45
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 72 deletions.
11 changes: 7 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ assets/css/theme.css
data/dnb/kollitsch/config.json
release.json

# playwright output
test-results.json
/test-results/
/playwright-report/
/blob-report/
/playwright/.cache/

#####################################################################
# ignores via API
#####################################################################
Expand Down Expand Up @@ -319,7 +326,3 @@ fabric.properties
!.idea/runConfigurations

# End of https://www.toptal.com/developers/gitignore/api/phpstorm+all
/test-results/
/playwright-report/
/blob-report/
/playwright/.cache/
81 changes: 25 additions & 56 deletions playwright.config.ts
Original file line number Diff line number Diff line change
@@ -1,87 +1,56 @@
import { defineConfig, devices } from '@playwright/test';

/**
* Read environment variables from file.
* https://github.com/motdotla/dotenv
*/
// import dotenv from 'dotenv';
// import path from 'path';
// dotenv.config({ path: path.resolve(__dirname, '.env') });

/**
* See https://playwright.dev/docs/test-configuration.
*/
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import dotenv from 'dotenv';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
dotenv.config({ path: path.resolve(__dirname, '.env') });

// @see https://playwright.dev/docs/test-configuration
export default defineConfig({
testDir: './tests/e2e',
/* 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: [
['list'],
//['json', { outputFile: 'test-results.json' }]
],
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
workers: process.env.CI ? 1 : 16,
// @see https://playwright.dev/docs/test-reporters
reporter: process.env.CI
? 'github'
: [
['list'],
['json', { outputFile: 'test-results.json' }],
['html', { open: 'on-failure', outputFile: 'test-results.html' }],
],
// @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',
baseURL: 'https://localhost:1313',

/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
// @see https://playwright.dev/docs/trace-viewer
trace: 'on-first-retry',
ignoreHTTPSErrors: true,
colorScheme: 'dark',
// geolocation: { longitude: 12.492507, latitude: 41.889938 },
// isMobile: false,
screenshot: { mode: 'only-on-failure', fullPage: true },
serviceWorkers: 'allow',
video: 'retain-on-failure',
},

/* 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,
// },

webServer: {
command: 'npm run server',
url: 'http://localhost:1313',
Expand Down
35 changes: 29 additions & 6 deletions tests/e2e/all-pages.spec.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,51 @@
import { test, expect, request } from '@playwright/test';
import { expect, request, test } from '@playwright/test';

test('Verify each link in links.json is reachable', async ({ page, baseURL }) => {
console.log(`Attempting to load links.json from ${baseURL}/links.json`);
let links: string[] = [];

// A setup function to load links.json before all tests
test.beforeAll(async ({ baseURL }) => {
if (!baseURL) {
throw new Error(
'baseURL is not defined. Ensure the baseURL is properly set in Playwright config.',
);
}

const apiContext = await request.newContext();
const response = await apiContext.get(`${baseURL}/links.json`);

if (!response.ok()) {
console.error(`Failed to load links.json with status: ${response.status()}`);
console.error(
`Failed to load links.json with status: ${response.status()}`,
);
throw new Error('Could not load links.json');
}

let links: string[] = [];
try {
const data = await response.json();
links = data.links;
console.log(`Loaded ${links.length} links from links.json`);
//console.log(`Loaded ${links.length} links from links.json`);
} catch (error) {
console.error('Failed to parse links.json:', error);
throw error;
}
});

// A test to verify each link in links.json is reachable
test('Verify each link in links.json is reachable', async ({ page }) => {
if (links.length === 0) {
throw new Error(
'No links were loaded. Ensure links.json is available and correctly parsed.',
);
}

for (const link of links) {
await page.goto(link);
expect(page.url()).toBe(link);
//console.log(`Successfully navigated to ${link}`);
}
});

test('has title', async ({ page }) => {
await page.goto('/');
await expect(page).toHaveTitle(/.*KOLLITSCH\.dev\*.*/);
});
6 changes: 0 additions & 6 deletions tests/e2e/general.spec.ts

This file was deleted.

0 comments on commit f8f1b45

Please sign in to comment.