Skip to content

Commit

Permalink
chore: rewrite next urls, trigger on shifts (microsoft#714)
Browse files Browse the repository at this point in the history
  • Loading branch information
rwoll authored Aug 4, 2022
1 parent de7f339 commit 4847e77
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 8 deletions.
20 changes: 14 additions & 6 deletions static/js/redirection.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ window.addEventListener("load", () => {
}
});

const kGoToNextToken = 'nnnnn';
const kGoToNextToken = ['Shift', 'Shift', 'Shift', 'Shift', 'Shift'];
let keyPressState = 0;
document.addEventListener('keypress', (e) => {
document.addEventListener('keydown', (e) => {
if (kGoToNextToken[keyPressState] === e.key) {
keyPressState++;
if (keyPressState === kGoToNextToken.length) {
Expand All @@ -94,13 +94,21 @@ document.addEventListener('keypress', (e) => {
}

function gotoNext() {
const parts = window.location.pathname.split('/');
if (parts.includes('next'))
return;
const docsIndex = parts.indexOf('docs');
let newPath = '/docs/next/intro';
if (window.location.pathname.startsWith('/python'))
if (docsIndex !== -1) {
parts.splice(docsIndex + 1, 0, 'next')
newPath = parts.join('/');
} else if (window.location.pathname.startsWith('/python'))
newPath = '/python/docs/next/intro';
if (window.location.pathname.startsWith('/java'))
else if (window.location.pathname.startsWith('/java'))
newPath = '/java/docs/next/intro';
if (window.location.pathname.startsWith('/dotnet'))
else if (window.location.pathname.startsWith('/dotnet'))
newPath = '/dotnet/docs/next/intro';
window.location.href = newPath;

window.location.href = newPath + location.hash;
}
});
13 changes: 13 additions & 0 deletions tests/baseTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { test as _test, expect } from "@playwright/test";

const test = _test.extend<{ switchToNext: () => Promise<void>; basePath: string }>({
switchToNext: async ({ page }, use) => {
const switchToNext = async () => {
for (let i = 0; i < 5; i++)
await page.keyboard.press('Shift');
};
await use(switchToNext);
},
});

export { expect, test };
48 changes: 47 additions & 1 deletion tests/nodejs.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { test, expect } from '@playwright/test';
import { test, expect } from './baseTest';

test.beforeEach(async ({page}) => {
await page.goto('/');
Expand All @@ -20,3 +20,49 @@ test('homepage has Playwright in title and get started link linking to the intro
// Expects the URL to contain intro.
await expect(page).toHaveURL(/.*intro/);
});

test.describe('next switcher', () => {
test('home -> next intro', async ({ page, switchToNext }) => {
const getStarted = page.locator('text=Get Started');
await expect(getStarted).toHaveAttribute('href', '/docs/intro');
await expect(page).toHaveURL(/\/$/);

await switchToNext();

await expect(page).toHaveURL(/\/docs\/next\/intro$/);
await expect(page.locator('text=Get started by installing Playwright')).toBeVisible();
});

test('community -> next intro', async ({ page, switchToNext }) => {
await page.goto('/community/welcome');
await expect(page.locator('text=Welcome to the Playwright Community')).toBeVisible();
await expect(page).toHaveURL(/\/community\/welcome$/);

await switchToNext();

await expect(page).toHaveURL(/\/docs\/next\/intro$/);
await expect(page.locator('text=Get started by installing Playwright')).toBeVisible();
});

test('docs -> next docs', async ({ page, switchToNext }) => {
await page.goto('/docs/locators');
await expect(page.locator('text=locators represent a way to find')).toBeVisible();
await expect(page).toHaveURL(/\/docs\/locators$/);

await switchToNext();

await expect(page).toHaveURL(/\/docs\/next\/locators$/);
await expect(page.locator('text=locators represent a way to find')).toBeVisible();
});

test('api -> next api (with fragment)', async ({ page, switchToNext }) => {
await page.goto('/docs/api/class-browser#browser-version');
await expect(page.locator('text=Returns the browser version.')).toBeVisible();
await expect(page).toHaveURL(/\/docs\/api\/class-browser#browser-version$/);

await switchToNext();

await expect(page.locator('text=Returns the browser version.')).toBeVisible();
await expect(page).toHaveURL(/\/docs\/next\/api\/class-browser#browser-version$/);
});
});
48 changes: 47 additions & 1 deletion tests/python.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { test, expect } from '@playwright/test';
import { test, expect } from './baseTest';

test.beforeEach(async ({page}) => {
await page.goto('/python/');
Expand All @@ -19,3 +19,49 @@ test('homepage has Playwright in title and get started link linking to the intro

await expect(page.locator('text=pytest-playwright')).toBeVisible();
});

test.describe('next switcher', () => {
test('home -> next intro', async ({ page, switchToNext }) => {
const getStarted = page.locator('text=Get Started');
await expect(getStarted).toHaveAttribute('href', '/python/docs/intro');
await expect(page).toHaveURL(/\/$/);

await switchToNext();

await expect(page).toHaveURL(/\/python\/docs\/next\/intro$/);
await expect(page.locator('text=Get started by installing Playwright')).toBeVisible();
});

test('community -> next intro', async ({ page, switchToNext }) => {
await page.goto('/python/community/welcome');
await expect(page.locator('text=Welcome to the Playwright Community')).toBeVisible();
await expect(page).toHaveURL(/\/python\/community\/welcome$/);

await switchToNext();

await expect(page).toHaveURL(/\/python\/docs\/next\/intro$/);
await expect(page.locator('text=Get started by installing Playwright')).toBeVisible();
});

test('docs -> next docs', async ({ page, switchToNext }) => {
await page.goto('/python/docs/locators');
await expect(page.locator('text=locators represent a way to find')).toBeVisible();
await expect(page).toHaveURL(/\/python\/docs\/locators$/);

await switchToNext();

await expect(page).toHaveURL(/\/python\/docs\/next\/locators$/);
await expect(page.locator('text=locators represent a way to find')).toBeVisible();
});

test('api -> next api (with fragment)', async ({ page, switchToNext }) => {
await page.goto('/python/docs/api/class-browser#browser-version');
await expect(page.locator('text=Returns the browser version.')).toBeVisible();
await expect(page).toHaveURL(/\/python\/docs\/api\/class-browser#browser-version$/);

await switchToNext();

await expect(page.locator('text=Returns the browser version.')).toBeVisible();
await expect(page).toHaveURL(/\/python\/docs\/next\/api\/class-browser#browser-version$/);
});
});

0 comments on commit 4847e77

Please sign in to comment.