qa-wolf/index.js

49 lines
1.4 KiB
JavaScript
Raw Normal View History

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
// instantiate 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 00:25:05 -04:00
// check if the website dates are sorted and report results
2024-09-09 18:43:20 -04:00
if (datestamps === datestamps.sort()) {
2024-09-10 00:25:05 -04:00
console.log(
"Success! The first 100 articles are correctly sorted by posting time.",
);
2024-09-09 18:43:20 -04:00
} else {
2024-09-10 00:25:05 -04:00
console.log(
"Failure… The first 100 articles are not correctly sorted by posting time.",
);
2024-09-09 18:43:20 -04:00
}
2024-09-09 16:34:06 -04:00
}
(async () => {
await sortHackerNewsArticles();
})();