From d37fd6a0bac83a6b55e61639034589d9b7adf260 Mon Sep 17 00:00:00 2001 From: Ethan Ruszanowski Date: Tue, 10 Sep 2024 14:37:26 -0400 Subject: [PATCH] Add playwright test --- index.js | 2 +- playwright.config.js | 29 ++++++++++++++--------------- tests/sort.spec.js | 30 ++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 16 deletions(-) create mode 100644 tests/sort.spec.js diff --git a/index.js b/index.js index b80b198..9367094 100644 --- a/index.js +++ b/index.js @@ -19,7 +19,7 @@ async function sortHackerNewsArticles() { let datespans = await page.locator(".age").all(); // iterate through date spans to get timestamps - for (row = 0; row < datespans.length; row++) { + 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"))); } diff --git a/playwright.config.js b/playwright.config.js index b15af2e..1c77313 100644 --- a/playwright.config.js +++ b/playwright.config.js @@ -1,5 +1,5 @@ // @ts-check -const { defineConfig, devices } = require('@playwright/test'); +const { defineConfig, devices } = require("@playwright/test"); /** * Read environment variables from file. @@ -11,7 +11,7 @@ const { defineConfig, devices } = require('@playwright/test'); * @see https://playwright.dev/docs/test-configuration */ module.exports = defineConfig({ - testDir: './tests', + testDir: "./tests", /* Run tests in files in parallel */ fullyParallel: true, /* Fail the build on CI if you accidentally left test.only in the source code. */ @@ -21,32 +21,32 @@ module.exports = defineConfig({ /* Opt out of parallel tests on CI. */ workers: process.env.CI ? 1 : undefined, /* Reporter to use. See https://playwright.dev/docs/test-reporters */ - reporter: 'html', + reporter: "html", /* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */ use: { /* Base URL to use in actions like `await page.goto('/')`. */ // baseURL: 'http://127.0.0.1:3000', /* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */ - trace: 'on-first-retry', + trace: "on-first-retry", }, /* Configure projects for major browsers */ projects: [ { - name: 'chromium', - use: { ...devices['Desktop Chrome'] }, + name: "chromium", + use: { ...devices["Desktop Chrome"], headless: false }, }, - { - name: 'firefox', - use: { ...devices['Desktop Firefox'] }, - }, + // { + // name: 'firefox', + // use: { ...devices['Desktop Firefox'] }, + // }, - { - name: 'webkit', - use: { ...devices['Desktop Safari'] }, - }, + // { + // name: 'webkit', + // use: { ...devices['Desktop Safari'] }, + // }, /* Test against mobile viewports. */ // { @@ -76,4 +76,3 @@ module.exports = defineConfig({ // reuseExistingServer: !process.env.CI, // }, }); - diff --git a/tests/sort.spec.js b/tests/sort.spec.js new file mode 100644 index 0000000..d01db2b --- /dev/null +++ b/tests/sort.spec.js @@ -0,0 +1,30 @@ +const { expect, test } = require("playwright/test"); + +test("First 100 articles sorted", async ({ page }) => { + // 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); + + // Expect match + await expect(datestamps === datestamps.sort()).toBeTruthy; +});