1/**
2 * Copyright 2018 Google Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import expect from 'expect';
18import { getTestState, setupTestBrowserHooks } from './mocha-utils'; // eslint-disable-line import/extensions
19
20describe('Browser specs', function () {
21  setupTestBrowserHooks();
22
23  describe('Browser.version', function () {
24    it('should return whether we are in headless', async () => {
25      const { browser, isHeadless } = getTestState();
26
27      const version = await browser.version();
28      expect(version.length).toBeGreaterThan(0);
29      expect(version.startsWith('Headless')).toBe(isHeadless);
30    });
31  });
32
33  describe('Browser.userAgent', function () {
34    it('should include WebKit', async () => {
35      const { browser, isChrome } = getTestState();
36
37      const userAgent = await browser.userAgent();
38      expect(userAgent.length).toBeGreaterThan(0);
39      if (isChrome) expect(userAgent).toContain('WebKit');
40      else expect(userAgent).toContain('Gecko');
41    });
42  });
43
44  describe('Browser.target', function () {
45    it('should return browser target', async () => {
46      const { browser } = getTestState();
47
48      const target = browser.target();
49      expect(target.type()).toBe('browser');
50    });
51  });
52
53  describe('Browser.process', function () {
54    it('should return child_process instance', async () => {
55      const { browser } = getTestState();
56
57      const process = await browser.process();
58      expect(process.pid).toBeGreaterThan(0);
59    });
60    it('should not return child_process for remote browser', async () => {
61      const { browser, puppeteer } = getTestState();
62
63      const browserWSEndpoint = browser.wsEndpoint();
64      const remoteBrowser = await puppeteer.connect({ browserWSEndpoint });
65      expect(remoteBrowser.process()).toBe(null);
66      remoteBrowser.disconnect();
67    });
68  });
69
70  describe('Browser.isConnected', () => {
71    it('should set the browser connected state', async () => {
72      const { browser, puppeteer } = getTestState();
73
74      const browserWSEndpoint = browser.wsEndpoint();
75      const newBrowser = await puppeteer.connect({ browserWSEndpoint });
76      expect(newBrowser.isConnected()).toBe(true);
77      newBrowser.disconnect();
78      expect(newBrowser.isConnected()).toBe(false);
79    });
80  });
81});
82