/** * Copyright 2018 Google Inc. All rights reserved. * Modifications copyright (c) Microsoft Corporation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import { test as it, expect } from './pageTest'; import { attachFrame, detachFrame } from '../config/utils'; it('should have different execution contexts', async ({ page, server }) => { await page.goto(server.EMPTY_PAGE); await attachFrame(page, 'frame1', server.EMPTY_PAGE); expect(page.frames().length).toBe(2); await page.frames()[0].evaluate(() => window['FOO'] = 'foo'); await page.frames()[1].evaluate(() => window['FOO'] = 'bar'); expect(await page.frames()[0].evaluate(() => window['FOO'])).toBe('foo'); expect(await page.frames()[1].evaluate(() => window['FOO'])).toBe('bar'); }); it('should have correct execution contexts @smoke', async ({ page, server }) => { await page.goto(server.PREFIX + '/frames/one-frame.html'); expect(page.frames().length).toBe(2); expect(await page.frames()[0].evaluate(() => document.body.textContent.trim())).toBe(''); expect(await page.frames()[1].evaluate(() => document.body.textContent.trim())).toBe(`Hi, I'm frame`); }); function expectContexts(pageImpl, count, browserName) { if (browserName === 'chromium') expect(pageImpl._delegate._mainFrameSession._contextIdToContext.size).toBe(count); else expect(pageImpl._delegate._contextIdToContext.size).toBe(count); } it('should dispose context on navigation', async ({ page, server, toImpl, browserName, isElectron }) => { it.skip(isElectron); await page.goto(server.PREFIX + '/frames/one-frame.html'); expect(page.frames().length).toBe(2); expectContexts(toImpl(page), 4, browserName); await page.goto(server.EMPTY_PAGE); expectContexts(toImpl(page), 2, browserName); }); it('should dispose context on cross-origin navigation', async ({ page, server, toImpl, browserName, isElectron }) => { it.skip(isElectron); await page.goto(server.PREFIX + '/frames/one-frame.html'); expect(page.frames().length).toBe(2); expectContexts(toImpl(page), 4, browserName); await page.goto(server.CROSS_PROCESS_PREFIX + '/empty.html'); expectContexts(toImpl(page), 2, browserName); }); it('should execute after cross-site navigation', async ({ page, server }) => { await page.goto(server.EMPTY_PAGE); const mainFrame = page.mainFrame(); expect(await mainFrame.evaluate(() => window.location.href)).toContain(server.EMPTY_PAGE); await page.goto(server.CROSS_PROCESS_PREFIX + '/empty.html'); expect(await mainFrame.evaluate(() => window.location.href)).toContain(server.CROSS_PROCESS_PREFIX); }); it('should not allow cross-frame js handles', async ({ page, server }) => { // TODO: this should actually be possible because frames script each other, // but protocol implementations do not support this. For now, assume current // behavior. await page.goto(server.PREFIX + '/frames/one-frame.html'); const handle = await page.evaluateHandle(() => { const iframe = document.querySelector('iframe'); const foo = { bar: 'baz' }; iframe.contentWindow['__foo'] = foo; return foo; }); const childFrame = page.mainFrame().childFrames()[0]; const childResult = await childFrame.evaluate(() => window['__foo']); expect(childResult).toEqual({ bar: 'baz' }); const error = await childFrame.evaluate(foo => foo.bar, handle).catch(e => e); expect(error.message).toContain('JSHandles can be evaluated only in the context they were created!'); }); it('should allow cross-frame element handles', async ({ page, server }) => { await page.goto(server.PREFIX + '/frames/one-frame.html'); const bodyHandle = await page.mainFrame().childFrames()[0].$('body'); const result = await page.evaluate(body => body.innerHTML, bodyHandle); expect(result.trim()).toBe('