2024-09-09 18:43:20 -04:00
|
|
|
// Solution by Ethan Ruszanowski
|
2024-09-09 16:34:06 -04:00
|
|
|
const { chromium } = require("playwright");
|
|
|
|
|
|
|
|
async function sortHackerNewsArticles() {
|
|
|
|
// launch browser
|
|
|
|
const browser = await chromium.launch({ headless: false });
|
|
|
|
const context = await browser.newContext();
|
|
|
|
const page = await context.newPage();
|
|
|
|
|
|
|
|
// go to Hacker News
|
|
|
|
await page.goto("https://news.ycombinator.com/newest");
|
2024-09-09 18:43:20 -04:00
|
|
|
|
2024-09-10 13:10:55 -04:00
|
|
|
// instantiate empty array for all date objects
|
2024-09-10 00:25:05 -04:00
|
|
|
let datestamps = [];
|
2024-09-09 18:43:20 -04:00
|
|
|
|
|
|
|
// loop through process per page
|
|
|
|
for (var pageview = 0; pageview <= 3; pageview++) {
|
|
|
|
// locate each article's age span
|
2024-09-10 00:25:05 -04:00
|
|
|
let datespans = await page.locator(".age").all();
|
2024-09-09 18:43:20 -04:00
|
|
|
|
|
|
|
// iterate through date spans to get timestamps
|
|
|
|
for (row = 0; row < datespans.length; row++) {
|
|
|
|
// turn timestamp strings into date objects and push to array
|
2024-09-10 00:25:05 -04:00
|
|
|
datestamps.push(new Date(await datespans[row].getAttribute("title")));
|
2024-09-09 18:43:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// click the next button as needed
|
2024-09-10 00:25:05 -04:00
|
|
|
await page.getByRole("link", { name: "More", exact: true }).click();
|
2024-09-09 18:43:20 -04:00
|
|
|
}
|
|
|
|
|
2024-09-10 00:25:05 -04:00
|
|
|
// drop extra array elements
|
|
|
|
datestamps.splice(100, 20);
|
2024-09-09 18:43:20 -04:00
|
|
|
|
2024-09-10 13:10:55 -04:00
|
|
|
// check if the website dates are sorted and return results
|
|
|
|
return datestamps === datestamps.sort();
|
2024-09-09 16:34:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
(async () => {
|
2024-09-10 13:10:55 -04:00
|
|
|
if (await sortHackerNewsArticles()) {
|
|
|
|
console.log("Success! The first 100 articles were sorted correctly.");
|
|
|
|
} else {
|
|
|
|
console.log("Failure… The first 100 articles were not sorted correctly.");
|
|
|
|
}
|
2024-09-09 16:34:06 -04:00
|
|
|
})();
|