Skip to content

Commit

Permalink
Playwright
Browse files Browse the repository at this point in the history
  • Loading branch information
sARY77 committed Sep 18, 2022
1 parent bf7d525 commit 0082064
Show file tree
Hide file tree
Showing 5 changed files with 305 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules/
/test-results/
/playwright-report/
/playwright/.cache/
99 changes: 99 additions & 0 deletions e2e/test-1.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { test, expect, Page } from '@playwright/test';

const routeMapUrl = 'https://www.alaskaair.com/route-map/';
const overlayLocator = '#route-map-root > div.App > div[class^="Overlay__"]';
const viewListLocator = 'div[class^="topLeftControlsWrapper__"] > div[class^="formWrapper__"] > div[class^="toggleListWrapper__"] > a[class^="desktopListToggleButton__"]';
const routeItemsLocator = 'div[class^="UnorderedListView__"] > div[class^="ListViewWrapper__"] > div[class^="listWrapper__"] > ul[class^="ListOfRoutes__"] > li[class^="ListItem__"]';
const itemLinkLocator = 'div[class^="booking__"] > a';
const originLabel = 'Set as origin';
const destinationLabel = 'View dates';
const connectionsLocator = 'div[class^="connections__"]';
const nonstopLabel = 'Nonstop';
const resetLocator = '#content > div[class^="input_field_wrapper__"] > div[class^="react-autosuggest__"] > button[class^="input_field_clear__"]';
const routes = new Map<string, string[]>();

async function getRoutesAsync(page: Page): Promise<Map<string, string[]>> {
await page.goto(routeMapUrl);
const overlay = page.locator(overlayLocator);
await overlay.locator(viewListLocator).click();
const routeItems = overlay.locator(routeItemsLocator);
const firstItemLink = routeItems.first().locator(itemLinkLocator);
await expect(firstItemLink).toHaveText(originLabel);
const numberOfOrigins = await routeItems.count();
for (let originIndex = 0; originIndex < numberOfOrigins; ++originIndex) {
// if (originIndex > 1) break;
const originItem = routeItems.nth(originIndex);
const itemLink = originItem.locator(itemLinkLocator);
await expect(itemLink).toHaveText(originLabel);
const origin = await originItem.getAttribute('id');
if (origin === null) continue;
console.log(`${originIndex + 1} of ${numberOfOrigins} ${origin}`);
await itemLink.click();
await expect(firstItemLink).toHaveText(destinationLabel);
const destinations: string[] = [];
const numberOfDestinations = await routeItems.count();
for (let destinationIndex = 0; destinationIndex < numberOfDestinations; ++destinationIndex) {
const destinationItem = routeItems.nth(destinationIndex);
const destination = await destinationItem.getAttribute('id');
if (destination === null) continue;
if (await destinationItem.locator(connectionsLocator).textContent() !== nonstopLabel) break;
console.log(`${destination}`);
destinations.push(destination);
}
routes.set(origin, destinations);
await page.waitForTimeout(1000);
await page.locator(resetLocator).click();
}
return routes;
}

const airportUrl = 'https://www.airnav.com/airports/get?s={0}';
const airportInfoLocator = 'body > table:nth-child(4) > tbody > tr > td:nth-child(2) > font';
const airportInfoRegExp = new RegExp('^<b>(.+)</b><br>(.+), (.+), USA$');
const internationalAirports = ['LTO', 'SJD', 'MZT', 'PVR', 'GDL', 'ZLO', 'ZIH', 'CUN', 'BZE', 'LIR', 'SJO', 'YYJ', 'YVR', 'YLW', 'YYC', 'YEG'];
const airports = new Map<string, string>();

async function getAirportInfoAsync(page: Page, airport: string): Promise<string> {
if (airports[airport] !== undefined) return airports[airport];
if (internationalAirports.some(code => code === airport)) return ',,';
await page.waitForTimeout(1000);
await page.goto(airportUrl.replace('{0}', airport));
const nameAndAddress = await page.locator(airportInfoLocator).innerHTML();
const results = airportInfoRegExp.exec(nameAndAddress);
const airportInfo = `${results?.at(1)},${results?.at(2)},${results?.at(3)}`;
airports[airport] = airportInfo;
console.log(`${airportInfo}`);
return airportInfo;
}

const distanceUrl = 'http://www.gcmap.com/mapui?P={0}-{1}';
const distanceLocator ='#mdist > tfoot > tr > td.d';
const distances = new Map<string, number>();

async function getDistanceAsync(page: Page, origin: string, destination: string): Promise<number> {
const route = origin < destination ? `${origin}${destination}` : `${destination}${origin}`;
if (distances[route] !== undefined) return distances[route];
await page.waitForTimeout(1000);
await page.goto(distanceUrl.replace('{0}', origin).replace('{1}', destination));
const miles = await page.locator(distanceLocator).textContent();
const distance = Number(miles?.replace(' mi', '').replace(',', ''));
distances[route] = distance;
console.log(`${distance}`);
return distance;
}

const results: string[] = [];

test('test', async ({ page }) => {
for (const route of await getRoutesAsync(page)) {
const origin = route[0];
const originInfo = await getAirportInfoAsync(page, origin);
for (const destination of route[1]) {
const destinationInfo = await getAirportInfoAsync(page, destination);
const distance = await getDistanceAsync(page, origin, destination);
results.push(`${origin},${originInfo},${destination},${destinationInfo},${distance}`);
}
}
console.clear();
for (const result of results) console.log(result);
});
74 changes: 74 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "assorted",
"version": "1.0.0",
"description": "\"# Assorted\"",
"main": "index.js",
"scripts": {},
"repository": {
"type": "git",
"url": "git+https://github.com/sARY77/Assorted.git"
},
"keywords": [],
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/sARY77/Assorted/issues"
},
"homepage": "https://github.com/sARY77/Assorted#readme",
"devDependencies": {
"@playwright/test": "^1.25.2"
}
}
107 changes: 107 additions & 0 deletions playwright.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import type { PlaywrightTestConfig } from '@playwright/test';
import { devices } from '@playwright/test';

/**
* Read environment variables from file.
* https://github.com/motdotla/dotenv
*/
// require('dotenv').config();

/**
* See https://playwright.dev/docs/test-configuration.
*/
const config: PlaywrightTestConfig = {
testDir: './e2e',
/* Maximum time one test can run for. */
timeout: 30 * 1000,
expect: {
/**
* Maximum time expect() should wait for the condition to be met.
* For example in `await expect(locator).toHaveText();`
*/
timeout: 5000
},
/* Run tests in files in parallel */
fullyParallel: true,
/* Fail the build on CI if you accidentally left test.only in the source code. */
forbidOnly: !!process.env.CI,
/* Retry on CI only */
retries: process.env.CI ? 2 : 0,
/* 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',
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
use: {
/* Maximum time each action such as `click()` can take. Defaults to 0 (no limit). */
actionTimeout: 0,
/* Base URL to use in actions like `await page.goto('/')`. */
// baseURL: 'http://localhost:3000',

/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
trace: 'on-first-retry',
},

/* Configure projects for major browsers */
projects: [
{
name: 'chromium',
use: {
...devices['Desktop Chrome'],
},
},

// {
// name: 'firefox',
// use: {
// ...devices['Desktop Firefox'],
// },
// },

// {
// name: 'webkit',
// use: {
// ...devices['Desktop Safari'],
// },
// },

/* Test against mobile viewports. */
// {
// name: 'Mobile Chrome',
// use: {
// ...devices['Pixel 5'],
// },
// },
// {
// name: 'Mobile Safari',
// use: {
// ...devices['iPhone 12'],
// },
// },

/* Test against branded browsers. */
// {
// name: 'Microsoft Edge',
// use: {
// channel: 'msedge',
// },
// },
// {
// name: 'Google Chrome',
// use: {
// channel: 'chrome',
// },
// },
],

/* Folder for test artifacts such as screenshots, videos, traces, etc. */
// outputDir: 'test-results/',

/* Run your local dev server before starting the tests */
// webServer: {
// command: 'npm run start',
// port: 3000,
// },
};

export default config;

0 comments on commit 0082064

Please sign in to comment.