MCPcopy
hub / github.com/ampproject/amphtml / newPage

Function newPage

build-system/tasks/visual-diff/browser.js:75–123  ·  view source on GitHub ↗

* Opens a new browser tab, resizes its viewport, and returns a Page handler. * * @param {!puppeteer.Browser} browser a Puppeteer controlled browser. * @param {?puppeteer.Viewport} viewport optional viewport size object with * numeric fields `width` and `height`. * @return {Promise<!puppetee

(browser, viewport = null)

Source from the content-addressed store, hash-verified

73 * @return {Promise<!puppeteer.Page>}
74 */
75async function newPage(browser, viewport = null) {
76 log('verbose', 'Creating new tab');
77
78 const context = await browser.createBrowserContext();
79 const page = await context.newPage();
80 page.setDefaultNavigationTimeout(0);
81 await page.setJavaScriptEnabled(true);
82 await page.setRequestInterception(true);
83 page.on('request', (interceptedRequest) => {
84 const requestUrl = new URL(interceptedRequest.url());
85 const mockedFilepath = path.join(
86 path.dirname(__filename),
87 'network-mocks',
88 requestUrl.hostname,
89 encodeURIComponent(
90 `${requestUrl.pathname.substr(1)}${requestUrl.search}`
91 ).replace(/%2F/g, '/')
92 );
93
94 if (
95 requestUrl.protocol === 'data:' ||
96 requestUrl.hostname === HOST ||
97 requestUrl.hostname.endsWith(`.${HOST}`)
98 ) {
99 return interceptedRequest.continue();
100 } else if (fs.existsSync(mockedFilepath)) {
101 log(
102 'verbose',
103 'Mocked network request for',
104 yellow(requestUrl.href),
105 'with file',
106 cyan(mockedFilepath)
107 );
108 return interceptedRequest.respond({
109 status: 200,
110 body: new Uint8Array(fs.readFileSync(mockedFilepath)),
111 });
112 } else {
113 log(
114 'verbose',
115 'Blocked external network request for',
116 yellow(requestUrl.href)
117 );
118 return interceptedRequest.abort('blockedbyclient');
119 }
120 });
121 await resetPage(page, viewport);
122 return page;
123}
124
125/**
126 * Resets the size of a tab and loads about:blank.

Callers 4

devModeFunction · 0.85
runVisualTestsFunction · 0.85
snapshotWebpagesFunction · 0.85
createEmptyBuildFunction · 0.85

Calls 8

yellowFunction · 0.85
cyanFunction · 0.85
resetPageFunction · 0.85
logFunction · 0.70
onMethod · 0.45
urlMethod · 0.45
replaceMethod · 0.45
abortMethod · 0.45

Tested by

no test coverage detected