Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions generated.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
```javascript
const { test, expect } = require('@playwright/test');
const path = require('path');

/**
* This test suite verifies the integrity of assets on the about.html page.
*/
test.describe('Asset Loading for about.html', () => {

/**
* This test case navigates to the local about.html file and checks all network
* requests initiated by the page. It fails if any asset (CSS, JS, image, font, etc.)
* returns a 4xx or 5xx HTTP status code.
*/
test('should load all linked assets successfully without 404 errors', async ({ page }) => {
// Array to store information about failed network requests.
const failedRequests = [];

// Set up a listener for all network responses on the page.
page.on('response', (response) => {
// The `ok()` method returns false for status codes 400-599.
if (!response.ok()) {
// We exclude data URIs as they are not external network requests.
if (!response.url().startsWith('data:')) {
failedRequests.push({
url: response.url(),
status: response.status(),
statusText: response.statusText(),
});
}
}
});

// Construct the absolute path to the local HTML file.
// This assumes the 'source code' directory is a sibling of the directory containing the tests.
const htmlPath = path.resolve(__dirname, '../source code/about.html');
const fileUrl = `file://${htmlPath}`;

try {
// Navigate to the local HTML file.
// 'networkidle' waits until there are no more network connections for at least 500ms,
// which is a good signal that the page and its initial assets have finished loading.
await page.goto(fileUrl, { waitUntil: 'networkidle' });
} catch (error) {
// If the main HTML file cannot be loaded, fail the test immediately with a clear error.
return test.fail(true, `Failed to load the primary HTML file at: ${fileUrl}. Please check the file path and permissions. Error: ${error.message}`);
}

// Assert that the array of failed requests is empty.
// A custom message is provided for clearer output in case of failure.
expect(failedRequests, `Found ${failedRequests.length} broken asset link(s):\n${JSON.stringify(failedRequests, null, 2)}`).toEqual([]);
});

});
```