Last active
May 17, 2023 12:08
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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