Skip to content

Instantly share code, notes, and snippets.

@checklyalex
Last active May 17, 2023 12:08
Show Gist options
  • Save checklyalex/ae0fcf7bc8acaadcf81d1dc6873bf6c2 to your computer and use it in GitHub Desktop.
Save checklyalex/ae0fcf7bc8acaadcf81d1dc6873bf6c2 to your computer and use it in GitHub Desktop.
This JavaScript script navigates to a given webpage, checks all links, and logs their status. It handles redirections, reporting the final URL and its HTTP status code.
const { test, expect } = require('@playwright/test');
const crawl_url = "https://checklyhq.com";
test('Check all links on a page', async ({ page }) => {
test.setTimeout(120000)
// Navigate to the URL
await page.goto(crawl_url); // Replace with your URL
// Get all links on the page
const links = await page.$$eval('a', as => as.map(a => a.href));
for (const link of links) {
// Navigate to each link
const response = await page.goto(link, { followRedirects: true });
// Check if the response was successful
if (response.status() >= 200 && response.status() <= 299) {
console.log(`Link works: ${response.url()}, Status: ${response.status()}`);
} else {
console.log(`Link failed: ${response.url()}, Status: ${response.status()}`);
}
// Go back to the original page for the next link
await page.goto(crawl_url);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment