๐Ÿ“ฆ microsoft / playwright

๐Ÿ“„ launchApp.ts ยท 126 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126/**
 * 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 fs from 'fs';
import path from 'path';

import { isUnderTest, rewriteErrorMessage, wrapInASCIIBox } from '../utils';
import { buildPlaywrightCLICommand, findChromiumChannelBestEffort } from './registry';
import { registryDirectory } from './registry';
import { ProgressController } from './progress';

import type { BrowserType } from './browserType';
import type { CRPage } from './chromium/crPage';
import type { Page } from './page';
import type * as types from './types';


export async function launchApp(browserType: BrowserType, options: {
  sdkLanguage: string,
  windowSize: types.Size,
  windowPosition?: types.Point,
  persistentContextOptions?: Parameters<BrowserType['launchPersistentContext']>[2];
}) {
  const args = [...options.persistentContextOptions?.args ?? []];

  let channel = options.persistentContextOptions?.channel;
  if (browserType.name() === 'chromium') {
    args.push(
        '--app=data:text/html,',
        `--window-size=${options.windowSize.width},${options.windowSize.height}`,
        ...(options.windowPosition ? [`--window-position=${options.windowPosition.x},${options.windowPosition.y}`] : []),
        '--test-type=',
    );
    if (!channel && !options.persistentContextOptions?.executablePath)
      channel = findChromiumChannelBestEffort(options.sdkLanguage);
  }

  const controller = new ProgressController();
  let context;
  try {
    context = await controller.run(progress => browserType.launchPersistentContext(progress, '', {
      ignoreDefaultArgs: ['--enable-automation'],
      ...options?.persistentContextOptions,
      channel,
      noDefaultViewport: options.persistentContextOptions?.noDefaultViewport ?? true,
      acceptDownloads: options?.persistentContextOptions?.acceptDownloads ?? (isUnderTest() ? 'accept' : 'internal-browser-default'),
      colorScheme: options?.persistentContextOptions?.colorScheme ?? 'no-override',
      args,
    }), 0); // Deliberately no timeout for our apps.
  } catch (error) {
    if (channel) {
      error = rewriteErrorMessage(error, [
        `Failed to launch "${channel}" channel.`,
        'Using custom channels could lead to unexpected behavior due to Enterprise policies (chrome://policy).',
        'Install the default browser instead:',
        wrapInASCIIBox(`${buildPlaywrightCLICommand(options.sdkLanguage, 'install')}`, 2),
      ].join('\n'));
    }
    throw error;
  }
  const [page] = context.pages();
  // Chromium on macOS opens a new tab when clicking on the dock icon.
  // See https://github.com/microsoft/playwright/issues/9434
  if (browserType.name() === 'chromium' && process.platform === 'darwin') {
    context.on('page', async (newPage: Page) => {
      if (newPage.mainFrame().url() === 'chrome://new-tab-page/') {
        await page.bringToFront();
        await newPage.close();
      }
    });
  }
  if (browserType.name() === 'chromium')
    await installAppIcon(page);
  return { context, page };
}

async function installAppIcon(page: Page) {
  const icon = await fs.promises.readFile(require.resolve('./chromium/appIcon.png'));
  const crPage = page.delegate as CRPage;
  await crPage._mainFrameSession._client.send('Browser.setDockTile', {
    image: icon.toString('base64')
  });
}

export async function syncLocalStorageWithSettings(page: Page, appName: string) {
  if (isUnderTest())
    return;
  const settingsFile = path.join(registryDirectory, '.settings', `${appName}.json`);

  const controller = new ProgressController();
  await controller.run(async progress => {
    await page.exposeBinding(progress, '_saveSerializedSettings', false, (_, settings) => {
      fs.mkdirSync(path.dirname(settingsFile), { recursive: true });
      fs.writeFileSync(settingsFile, settings);
    });

    const settings = await fs.promises.readFile(settingsFile, 'utf-8').catch(() => ('{}'));
    await page.addInitScript(progress,
        `(${String((settings: any) => {
          // iframes w/ snapshots, etc.
          if (location && location.protocol === 'data:')
            return;
          if (window.top !== window)
            return;
          Object.entries(settings).map(([k, v]) => localStorage[k] = v);
          (window as any).saveSettings = () => {
            (window as any)._saveSerializedSettings(JSON.stringify({ ...localStorage }));
          };
        })})(${settings});
    `);
  });
}