|
| 1 | +/** |
| 2 | + * Copyright 2017 Google Inc. All rights reserved. |
| 3 | + * Modifications copyright (c) Microsoft Corporation. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +const path = require('path'); |
| 19 | +const fs = require('fs'); |
| 20 | +const utils = require('./utils'); |
| 21 | +const {FFOX, CHROMIUM, WEBKIT, WIN, USES_HOOKS, CHANNEL} = testOptions; |
| 22 | + |
| 23 | +it('should work', async({browserType, defaultBrowserOptions}) => { |
| 24 | + const browserServer = await browserType.launchServer(defaultBrowserOptions); |
| 25 | + const browser = await browserType.connect({ wsEndpoint: browserServer.wsEndpoint() }); |
| 26 | + const browserContext = await browser.newContext(); |
| 27 | + expect(browserContext.pages().length).toBe(0); |
| 28 | + expect(browserServer.wsEndpoint()).not.toBe(null); |
| 29 | + const page = await browserContext.newPage(); |
| 30 | + expect(await page.evaluate('11 * 11')).toBe(121); |
| 31 | + await page.close(); |
| 32 | + await browser.close(); |
| 33 | + await browserServer._checkLeaks(); |
| 34 | + await browserServer.close(); |
| 35 | +}); |
| 36 | + |
| 37 | +it('should fire "disconnected" when closing the server', async({browserType, defaultBrowserOptions}) => { |
| 38 | + const browserServer = await browserType.launchServer(defaultBrowserOptions); |
| 39 | + const browser = await browserType.connect({ wsEndpoint: browserServer.wsEndpoint() }); |
| 40 | + const disconnectedEventPromise = new Promise(resolve => browser.once('disconnected', resolve)); |
| 41 | + const closedPromise = new Promise(f => browserServer.on('close', f)); |
| 42 | + browserServer.kill(); |
| 43 | + await Promise.all([ |
| 44 | + disconnectedEventPromise, |
| 45 | + closedPromise, |
| 46 | + ]); |
| 47 | +}); |
| 48 | + |
| 49 | +it('should fire "close" event during kill', async({browserType, defaultBrowserOptions}) => { |
| 50 | + const order = []; |
| 51 | + const browserServer = await browserType.launchServer(defaultBrowserOptions); |
| 52 | + const closedPromise = new Promise(f => browserServer.on('close', () => { |
| 53 | + order.push('closed'); |
| 54 | + f(); |
| 55 | + })); |
| 56 | + await Promise.all([ |
| 57 | + browserServer.kill().then(() => order.push('killed')), |
| 58 | + closedPromise, |
| 59 | + ]); |
| 60 | + expect(order).toEqual(['closed', 'killed']); |
| 61 | +}); |
| 62 | + |
| 63 | +it('should return child_process instance', async ({browserType, defaultBrowserOptions}) => { |
| 64 | + const browserServer = await browserType.launchServer(defaultBrowserOptions); |
| 65 | + expect(browserServer.process().pid).toBeGreaterThan(0); |
| 66 | + await browserServer.close(); |
| 67 | +}); |
| 68 | + |
| 69 | +it('should fire close event', async ({browserType, defaultBrowserOptions}) => { |
| 70 | + const browserServer = await browserType.launchServer(defaultBrowserOptions); |
| 71 | + const [result] = await Promise.all([ |
| 72 | + new Promise(f => browserServer.on('close', (exitCode, signal) => f({ exitCode, signal }))), |
| 73 | + browserServer.close(), |
| 74 | + ]); |
| 75 | + expect(result.exitCode).toBe(0); |
| 76 | + expect(result.signal).toBe(null); |
| 77 | +}); |
| 78 | + |
| 79 | +it('should reject navigation when browser closes', async({browserType, defaultBrowserOptions, server}) => { |
| 80 | + server.setRoute('/one-style.css', () => {}); |
| 81 | + const browserServer = await browserType.launchServer(defaultBrowserOptions); |
| 82 | + const remote = await browserType.connect({ wsEndpoint: browserServer.wsEndpoint() }); |
| 83 | + const page = await remote.newPage(); |
| 84 | + const navigationPromise = page.goto(server.PREFIX + '/one-style.html', {timeout: 60000}).catch(e => e); |
| 85 | + await server.waitForRequest('/one-style.css'); |
| 86 | + await remote.close(); |
| 87 | + const error = await navigationPromise; |
| 88 | + expect(error.message).toContain('Navigation failed because page was closed!'); |
| 89 | + await browserServer._checkLeaks(); |
| 90 | + await browserServer.close(); |
| 91 | +}); |
| 92 | + |
| 93 | +it('should reject waitForSelector when browser closes', async({browserType, defaultBrowserOptions, server}) => { |
| 94 | + server.setRoute('/empty.html', () => {}); |
| 95 | + const browserServer = await browserType.launchServer(defaultBrowserOptions); |
| 96 | + const remote = await browserType.connect({ wsEndpoint: browserServer.wsEndpoint() }); |
| 97 | + const page = await remote.newPage(); |
| 98 | + const watchdog = page.waitForSelector('div', { state: 'attached', timeout: 60000 }).catch(e => e); |
| 99 | + |
| 100 | + // Make sure the previous waitForSelector has time to make it to the browser before we disconnect. |
| 101 | + await page.waitForSelector('body', { state: 'attached' }); |
| 102 | + |
| 103 | + await remote.close(); |
| 104 | + const error = await watchdog; |
| 105 | + expect(error.message).toContain('Protocol error'); |
| 106 | + await browserServer._checkLeaks(); |
| 107 | + await browserServer.close(); |
| 108 | +}); |
| 109 | + |
| 110 | +it('should throw if used after disconnect', async({browserType, defaultBrowserOptions}) => { |
| 111 | + const browserServer = await browserType.launchServer(defaultBrowserOptions); |
| 112 | + const remote = await browserType.connect({ wsEndpoint: browserServer.wsEndpoint() }); |
| 113 | + const page = await remote.newPage(); |
| 114 | + await remote.close(); |
| 115 | + const error = await page.evaluate('1 + 1').catch(e => e); |
| 116 | + expect(error.message).toContain('has been closed'); |
| 117 | + await browserServer._checkLeaks(); |
| 118 | + await browserServer.close(); |
| 119 | +}); |
| 120 | + |
| 121 | +it('should emit close events on pages and contexts', async({browserType, defaultBrowserOptions}) => { |
| 122 | + const browserServer = await browserType.launchServer(defaultBrowserOptions); |
| 123 | + const remote = await browserType.connect({ wsEndpoint: browserServer.wsEndpoint() }); |
| 124 | + const context = await remote.newContext(); |
| 125 | + const page = await context.newPage(); |
| 126 | + let pageClosed = false; |
| 127 | + page.on('close', e => pageClosed = true); |
| 128 | + await Promise.all([ |
| 129 | + new Promise(f => context.on('close', f)), |
| 130 | + browserServer.close() |
| 131 | + ]); |
| 132 | + expect(pageClosed).toBeTruthy(); |
| 133 | +}); |
| 134 | + |
| 135 | +it('should terminate network waiters', async({browserType, defaultBrowserOptions, server}) => { |
| 136 | + const browserServer = await browserType.launchServer(defaultBrowserOptions); |
| 137 | + const remote = await browserType.connect({ wsEndpoint: browserServer.wsEndpoint() }); |
| 138 | + const newPage = await remote.newPage(); |
| 139 | + const results = await Promise.all([ |
| 140 | + newPage.waitForRequest(server.EMPTY_PAGE).catch(e => e), |
| 141 | + newPage.waitForResponse(server.EMPTY_PAGE).catch(e => e), |
| 142 | + browserServer.close() |
| 143 | + ]); |
| 144 | + for (let i = 0; i < 2; i++) { |
| 145 | + const message = results[i].message; |
| 146 | + expect(message).toContain('Page closed'); |
| 147 | + expect(message).not.toContain('Timeout'); |
| 148 | + } |
| 149 | +}); |
0 commit comments