Add playwright test

This commit is contained in:
Em (Ethan) Ruszanowski 2024-09-10 14:37:26 -04:00
parent 6ade503338
commit d37fd6a0ba
Signed by: em
GPG key ID: C725D6E571252B96
3 changed files with 45 additions and 16 deletions

View file

@ -19,7 +19,7 @@ async function sortHackerNewsArticles() {
let datespans = await page.locator(".age").all(); let datespans = await page.locator(".age").all();
// iterate through date spans to get timestamps // 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 // turn timestamp strings into date objects and push to array
datestamps.push(new Date(await datespans[row].getAttribute("title"))); datestamps.push(new Date(await datespans[row].getAttribute("title")));
} }

View file

@ -1,5 +1,5 @@
// @ts-check // @ts-check
const { defineConfig, devices } = require('@playwright/test'); const { defineConfig, devices } = require("@playwright/test");
/** /**
* Read environment variables from file. * Read environment variables from file.
@ -11,7 +11,7 @@ const { defineConfig, devices } = require('@playwright/test');
* @see https://playwright.dev/docs/test-configuration * @see https://playwright.dev/docs/test-configuration
*/ */
module.exports = defineConfig({ module.exports = defineConfig({
testDir: './tests', testDir: "./tests",
/* Run tests in files in parallel */ /* Run tests in files in parallel */
fullyParallel: true, fullyParallel: true,
/* Fail the build on CI if you accidentally left test.only in the source code. */ /* 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. */ /* Opt out of parallel tests on CI. */
workers: process.env.CI ? 1 : undefined, workers: process.env.CI ? 1 : undefined,
/* Reporter to use. See https://playwright.dev/docs/test-reporters */ /* 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. */ /* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
use: { use: {
/* Base URL to use in actions like `await page.goto('/')`. */ /* Base URL to use in actions like `await page.goto('/')`. */
// baseURL: 'http://127.0.0.1:3000', // baseURL: 'http://127.0.0.1:3000',
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */ /* 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 */ /* Configure projects for major browsers */
projects: [ projects: [
{ {
name: 'chromium', name: "chromium",
use: { ...devices['Desktop Chrome'] }, use: { ...devices["Desktop Chrome"], headless: false },
}, },
{ // {
name: 'firefox', // name: 'firefox',
use: { ...devices['Desktop Firefox'] }, // use: { ...devices['Desktop Firefox'] },
}, // },
{ // {
name: 'webkit', // name: 'webkit',
use: { ...devices['Desktop Safari'] }, // use: { ...devices['Desktop Safari'] },
}, // },
/* Test against mobile viewports. */ /* Test against mobile viewports. */
// { // {
@ -76,4 +76,3 @@ module.exports = defineConfig({
// reuseExistingServer: !process.env.CI, // reuseExistingServer: !process.env.CI,
// }, // },
}); });

30
tests/sort.spec.js Normal file
View file

@ -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;
});