Complete assessment

This commit is contained in:
Em (Ethan) Ruszanowski 2024-09-09 18:43:20 -04:00
parent 1fc4ffca79
commit 1b70ac612e
Signed by: em
GPG key ID: C725D6E571252B96
2 changed files with 33 additions and 1 deletions

4
.gitignore vendored Normal file
View file

@ -0,0 +1,4 @@
node_modules/
/test-results/
/playwright-report/
/playwright/.cache/

View file

@ -1,4 +1,4 @@
// EDIT THIS FILE TO COMPLETE ASSIGNMENT QUESTION 1 // Solution by Ethan Ruszanowski
const { chromium } = require("playwright"); const { chromium } = require("playwright");
async function sortHackerNewsArticles() { async function sortHackerNewsArticles() {
@ -9,6 +9,34 @@ async function sortHackerNewsArticles() {
// go to Hacker News // go to Hacker News
await page.goto("https://news.ycombinator.com/newest"); 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 () => { (async () => {