From 6f76acbd5100a93a415ca9ec9b706039f45e21cb Mon Sep 17 00:00:00 2001 From: eshfaq-ux <64083614+eshfaq-ux@users.noreply.github.com> Date: Fri, 3 Oct 2025 10:48:57 +0530 Subject: [PATCH] feat: Add generated.test.js --- generated.test.js | 55 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 generated.test.js diff --git a/generated.test.js b/generated.test.js new file mode 100644 index 0000000..9feb458 --- /dev/null +++ b/generated.test.js @@ -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([]); + }); + +}); +``` \ No newline at end of file