diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..75e854d --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +node_modules/ +/test-results/ +/playwright-report/ +/playwright/.cache/ diff --git a/index.js b/index.js index 6906f7b..ab566bd 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,4 @@ -// EDIT THIS FILE TO COMPLETE ASSIGNMENT QUESTION 1 +// Solution by Ethan Ruszanowski const { chromium } = require("playwright"); async function sortHackerNewsArticles() { @@ -9,6 +9,34 @@ async function sortHackerNewsArticles() { // 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 () => {