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 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 (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 articles from last page grab
datestamps = datestamps.slice(0, 100);
// report results
if (datestamps === datestamps.sort()) {
console.log("Success! The first 100 articles are correctly sorted by posting time.")
} else {
console.log("Failure… The first 100 articles are not correctly sorted by posting time.")
}
}
(async () => {
await sortHackerNewsArticles();
})();