e2e: refactor tests to improve performance

Refactors some tests to reuse page contexts. This
speeds up the overall test run by about 25%.

Change-Id: Id3e16d4b82c19119a1767788f4c9520e59c779af
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/311310
Trust: Jamal Carvalho <jamal@golang.org>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
This commit is contained in:
Jamal Carvalho 2021-04-18 21:21:15 -04:00
Родитель d461afa5f1
Коммит 5bcad7d5c0
7 изменённых файлов: 77 добавлений и 65 удалений

Просмотреть файл

@ -11,12 +11,12 @@ import puppeteer, { Page } from 'puppeteer';
describe('Badge', () => {
let page: Page;
beforeEach(async () => {
beforeAll(async () => {
page = await newPage();
await page.goto(baseURL + '/badge');
});
afterEach(async () => {
afterAll(async () => {
await page.close();
});

Просмотреть файл

@ -11,12 +11,12 @@ import puppeteer, { Page } from 'puppeteer';
describe('Error', () => {
let page: Page;
beforeEach(async () => {
beforeAll(async () => {
page = await newPage();
await page.goto(baseURL + '/@bad-request');
});
afterEach(async () => {
afterAll(async () => {
await page.close();
});

Просмотреть файл

@ -11,12 +11,12 @@ import puppeteer, { Page } from 'puppeteer';
describe('Homepage', () => {
let page: Page;
beforeEach(async () => {
beforeAll(async () => {
page = await newPage();
await page.goto(baseURL);
});
afterEach(async () => {
afterAll(async () => {
await page.close();
});

Просмотреть файл

@ -11,12 +11,12 @@ import puppeteer, { Page } from 'puppeteer';
describe('License Policy', () => {
let page: Page;
beforeEach(async () => {
beforeAll(async () => {
page = await newPage();
await page.goto(baseURL + '/license-policy');
});
afterEach(async () => {
afterAll(async () => {
await page.close();
});

Просмотреть файл

@ -11,12 +11,12 @@ import puppeteer, { Page } from 'puppeteer';
describe('Search Help', () => {
let page: Page;
beforeEach(async () => {
beforeAll(async () => {
page = await newPage();
await page.goto(baseURL + '/search-help');
});
afterEach(async () => {
afterAll(async () => {
await page.close();
});

Просмотреть файл

@ -46,29 +46,31 @@ describe('Search', () => {
* @param page The page to prepare
*/
async function prepare(page: Page): Promise<void> {
await page.$$eval('[data-test-id="snippet-title"]', els =>
els.map(el => {
el.innerHTML = 'net/http/pprof';
(el as HTMLAnchorElement).href = 'net/http/pprof';
})
);
await page.$$eval('[data-test-id="snippet-synopsis"]', els =>
els.map(
el =>
(el.innerHTML =
'Package pprof serves via its HTTP server runtime profiling data in the format expected by the pprof visualization tool.')
)
);
await page.$$eval('[data-test-id="snippet-version"]', els =>
els.map(el => (el.innerHTML = 'go1.16.3'))
);
await page.$$eval('[data-test-id="snippet-published"]', els =>
els.map(el => (el.innerHTML = 'Apr 1, 2021'))
);
await page.$$eval('[data-test-id="snippet-importedby"]', els =>
els.map(el => (el.innerHTML = '11632'))
);
await page.$$eval('[data-test-id="snippet-license"]', els =>
els.map(el => (el.innerHTML = 'BSD-3-Clause'))
);
await Promise.all([
page.$$eval('[data-test-id="snippet-title"]', els =>
els.map(el => {
el.innerHTML = 'net/http/pprof';
(el as HTMLAnchorElement).href = 'net/http/pprof';
})
),
page.$$eval('[data-test-id="snippet-synopsis"]', els =>
els.map(el => {
el.innerHTML =
'Package pprof serves via its HTTP server runtime profiling ' +
'data in the format expected by the pprof visualization tool.';
})
),
page.$$eval('[data-test-id="snippet-version"]', els =>
els.map(el => (el.innerHTML = 'go1.16.3'))
),
page.$$eval('[data-test-id="snippet-published"]', els =>
els.map(el => (el.innerHTML = 'Apr 1, 2021'))
),
page.$$eval('[data-test-id="snippet-importedby"]', els =>
els.map(el => (el.innerHTML = '11632'))
),
page.$$eval('[data-test-id="snippet-license"]', els =>
els.map(el => (el.innerHTML = 'BSD-3-Clause'))
),
]);
}

Просмотреть файл

@ -8,43 +8,51 @@
import './global-types';
import puppeteer, { Page } from 'puppeteer';
describe('Unit Details - golang.org/x/pkgsite', () => {
describe('Unit Details - golang.org/x/pkgsite - desktop', () => {
let page: Page;
beforeEach(async () => {
beforeAll(async () => {
page = await newPage();
await page.goto(baseURL + '/golang.org/x/pkgsite');
await prepare(page);
});
afterEach(async () => {
afterAll(async () => {
await page.close();
});
test('desktop viewport matches image snapshot', async () => {
await prepare(page);
test('viewport matches image snapshot', async () => {
const image = await page.screenshot({ fullPage: true });
expect(image).toMatchImageSnapshot({});
});
test('mobile viewport matches image snapshot', async () => {
await page.emulate(puppeteer.devices['Pixel 2']);
await prepare(page);
const image = await page.screenshot({ fullPage: true });
expect(image).toMatchImageSnapshot();
});
test('desktop fixed header appears after scrolling', async () => {
await prepare(page);
test('fixed header appears after scrolling', async () => {
await page.mouse.wheel({ deltaY: 250 });
// wait for css transition
await page.waitForTimeout(500);
const image = await page.screenshot();
expect(image).toMatchImageSnapshot();
});
});
test('mobile fixed header appears after scrolling', async () => {
describe('Unit Details - golang.org/x/pkgsite - mobile', () => {
let page: Page;
beforeAll(async () => {
page = await newPage();
await page.emulate(puppeteer.devices['Pixel 2']);
await page.goto(baseURL + '/golang.org/x/pkgsite');
await prepare(page);
});
afterAll(async () => {
await page.close();
});
test('viewport matches image snapshot', async () => {
const image = await page.screenshot({ fullPage: true });
expect(image).toMatchImageSnapshot();
});
test('fixed header appears after scrolling', async () => {
await page.mouse.wheel({ deltaY: 250 });
// wait for css transition
await page.waitForTimeout(500);
@ -59,18 +67,20 @@ describe('Unit Details - golang.org/x/pkgsite', () => {
* @param page The page to prepare
*/
async function prepare(page: Page): Promise<void> {
await page.$eval(
'[data-test-id="UnitHeader-version"] a',
el => ((el as HTMLElement).innerHTML = '<span>Version: </span>v0.0.0')
);
await page.$eval(
'[data-test-id="UnitHeader-commitTime"]',
el => ((el as HTMLElement).innerHTML = 'Published: Apr 16, 2021')
);
await page.$$eval('[data-test-id="UnitHeader-imports"] a', els =>
els.map(el => (el.innerHTML = 'Imports: 0'))
);
await page.$$eval('[data-test-id="UnitHeader-importedby"] a', els =>
els.map(el => (el.innerHTML = 'Imported by: 0'))
);
await Promise.all([
page.$eval(
'[data-test-id="UnitHeader-version"] a',
el => ((el as HTMLElement).innerHTML = '<span>Version: </span>v0.0.0')
),
page.$eval(
'[data-test-id="UnitHeader-commitTime"]',
el => ((el as HTMLElement).innerHTML = 'Published: Apr 16, 2021')
),
page.$$eval('[data-test-id="UnitHeader-imports"] a', els =>
els.map(el => (el.innerHTML = 'Imports: 0'))
),
page.$$eval('[data-test-id="UnitHeader-importedby"] a', els =>
els.map(el => (el.innerHTML = 'Imported by: 0'))
),
]);
}