qa-wolf/index.js

45 lines
1.4 KiB
JavaScript

// Solution by Ethan Ruszanowski
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");
// instantiate empty array for all date objects
let datestamps = [];
// loop through process per page
for (var pageview = 0; pageview <= 3; pageview++) {
// locate each article's age span
let datespans = await page.locator(".age").all();
// iterate through date spans to get timestamps
for (var row = 0; row < datespans.length; row++) {
// turn timestamp strings into date objects and push to array
datestamps.push(new Date(await datespans[row].getAttribute("title")));
}
// click the next button as needed
await page.getByRole("link", { name: "More", exact: true }).click();
}
// drop extra array elements
datestamps.splice(100, 20);
// check if the website dates are sorted and return results
return datestamps === datestamps.sort();
}
(async () => {
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.");
}
})();