2020-12-29 01:50:12 +03:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2021-02-11 17:36:15 +03:00
|
|
|
import fs from 'fs';
|
|
|
|
import path from 'path';
|
2020-12-29 01:50:12 +03:00
|
|
|
import { folio } from './cli.fixtures';
|
|
|
|
|
|
|
|
const { it, expect } = folio;
|
|
|
|
|
|
|
|
const emptyHTML = new URL('file://' + path.join(__dirname, '..', 'assets', 'empty.html')).toString();
|
|
|
|
|
2021-02-12 04:46:54 +03:00
|
|
|
it('should print the correct imports and context options', async ({ browserName, runCLI }) => {
|
2020-12-29 01:50:12 +03:00
|
|
|
const cli = runCLI(['codegen', emptyHTML]);
|
2021-02-12 04:46:54 +03:00
|
|
|
const expectedResult = `const { ${browserName} } = require('playwright');
|
2020-12-29 01:50:12 +03:00
|
|
|
|
|
|
|
(async () => {
|
2021-02-12 04:46:54 +03:00
|
|
|
const browser = await ${browserName}.launch({
|
2020-12-29 01:50:12 +03:00
|
|
|
headless: false
|
|
|
|
});
|
|
|
|
const context = await browser.newContext();`;
|
|
|
|
await cli.waitFor(expectedResult);
|
|
|
|
expect(cli.text()).toContain(expectedResult);
|
|
|
|
});
|
|
|
|
|
2021-02-12 04:46:54 +03:00
|
|
|
it('should print the correct context options for custom settings', async ({ browserName, runCLI }) => {
|
2021-03-04 09:25:14 +03:00
|
|
|
const cli = runCLI(['codegen', '--color-scheme=light', emptyHTML]);
|
2021-02-12 04:46:54 +03:00
|
|
|
const expectedResult = `const { ${browserName} } = require('playwright');
|
2020-12-29 01:50:12 +03:00
|
|
|
|
|
|
|
(async () => {
|
2021-02-12 04:46:54 +03:00
|
|
|
const browser = await ${browserName}.launch({
|
2020-12-29 01:50:12 +03:00
|
|
|
headless: false
|
|
|
|
});
|
|
|
|
const context = await browser.newContext({
|
|
|
|
colorScheme: 'light'
|
|
|
|
});`;
|
|
|
|
await cli.waitFor(expectedResult);
|
|
|
|
expect(cli.text()).toContain(expectedResult);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2021-03-12 07:22:50 +03:00
|
|
|
it('should print the correct context options when using a device', (test, { browserName }) => {
|
|
|
|
test.skip(browserName !== 'chromium');
|
|
|
|
}, async ({ runCLI }) => {
|
2021-03-04 09:25:14 +03:00
|
|
|
const cli = runCLI(['codegen', '--device=Pixel 2', emptyHTML]);
|
2020-12-29 01:50:12 +03:00
|
|
|
const expectedResult = `const { chromium, devices } = require('playwright');
|
|
|
|
|
|
|
|
(async () => {
|
|
|
|
const browser = await chromium.launch({
|
|
|
|
headless: false
|
|
|
|
});
|
|
|
|
const context = await browser.newContext({
|
|
|
|
...devices['Pixel 2'],
|
|
|
|
});`;
|
|
|
|
await cli.waitFor(expectedResult);
|
|
|
|
expect(cli.text()).toContain(expectedResult);
|
|
|
|
});
|
|
|
|
|
2021-03-12 07:22:50 +03:00
|
|
|
it('should print the correct context options when using a device and additional options', (test, { browserName }) => {
|
|
|
|
test.skip(browserName !== 'webkit');
|
|
|
|
}, async ({ runCLI }) => {
|
2021-03-04 09:25:14 +03:00
|
|
|
const cli = runCLI(['codegen', '--color-scheme=light', '--device=iPhone 11', emptyHTML]);
|
2021-02-12 04:46:54 +03:00
|
|
|
const expectedResult = `const { webkit, devices } = require('playwright');
|
2020-12-29 01:50:12 +03:00
|
|
|
|
|
|
|
(async () => {
|
2021-02-12 04:46:54 +03:00
|
|
|
const browser = await webkit.launch({
|
2020-12-29 01:50:12 +03:00
|
|
|
headless: false
|
|
|
|
});
|
|
|
|
const context = await browser.newContext({
|
2021-02-12 04:46:54 +03:00
|
|
|
...devices['iPhone 11'],
|
2020-12-29 01:50:12 +03:00
|
|
|
colorScheme: 'light'
|
|
|
|
});`;
|
|
|
|
await cli.waitFor(expectedResult);
|
|
|
|
expect(cli.text()).toContain(expectedResult);
|
|
|
|
});
|
|
|
|
|
2021-02-12 04:46:54 +03:00
|
|
|
it('should save the codegen output to a file if specified', async ({ browserName, runCLI, testInfo }) => {
|
2020-12-29 01:50:12 +03:00
|
|
|
const tmpFile = testInfo.outputPath('script.js');
|
|
|
|
const cli = runCLI(['codegen', '--output', tmpFile, emptyHTML]);
|
|
|
|
await cli.exited;
|
2021-01-13 23:52:03 +03:00
|
|
|
const content = fs.readFileSync(tmpFile);
|
2021-02-12 04:46:54 +03:00
|
|
|
expect(content.toString()).toBe(`const { ${browserName} } = require('playwright');
|
2020-12-29 01:50:12 +03:00
|
|
|
|
|
|
|
(async () => {
|
2021-02-12 04:46:54 +03:00
|
|
|
const browser = await ${browserName}.launch({
|
2020-12-29 01:50:12 +03:00
|
|
|
headless: false
|
|
|
|
});
|
|
|
|
const context = await browser.newContext();
|
|
|
|
|
|
|
|
// Open new page
|
|
|
|
const page = await context.newPage();
|
|
|
|
|
|
|
|
// Go to ${emptyHTML}
|
|
|
|
await page.goto('${emptyHTML}');
|
|
|
|
|
|
|
|
// Close page
|
|
|
|
await page.close();
|
|
|
|
|
|
|
|
// ---------------------
|
|
|
|
await context.close();
|
|
|
|
await browser.close();
|
|
|
|
})();`);
|
|
|
|
});
|
|
|
|
|
2021-02-12 04:46:54 +03:00
|
|
|
it('should print load/save storageState', async ({ browserName, runCLI, testInfo }) => {
|
2020-12-29 01:50:12 +03:00
|
|
|
const loadFileName = testInfo.outputPath('load.json');
|
|
|
|
const saveFileName = testInfo.outputPath('save.json');
|
|
|
|
await fs.promises.writeFile(loadFileName, JSON.stringify({ cookies: [], origins: [] }), 'utf8');
|
2021-03-04 09:25:14 +03:00
|
|
|
const cli = runCLI(['codegen', `--load-storage=${loadFileName}`, `--save-storage=${saveFileName}`, emptyHTML]);
|
2021-02-12 04:46:54 +03:00
|
|
|
const expectedResult1 = `const { ${browserName} } = require('playwright');
|
2020-12-29 01:50:12 +03:00
|
|
|
|
|
|
|
(async () => {
|
2021-02-12 04:46:54 +03:00
|
|
|
const browser = await ${browserName}.launch({
|
2020-12-29 01:50:12 +03:00
|
|
|
headless: false
|
|
|
|
});
|
|
|
|
const context = await browser.newContext({
|
|
|
|
storageState: '${loadFileName}'
|
2021-02-12 04:46:54 +03:00
|
|
|
});`;
|
|
|
|
await cli.waitFor(expectedResult1);
|
2020-12-29 01:50:12 +03:00
|
|
|
|
2021-02-12 04:46:54 +03:00
|
|
|
const expectedResult2 = `
|
2020-12-29 01:50:12 +03:00
|
|
|
// ---------------------
|
|
|
|
await context.storageState({ path: '${saveFileName}' });
|
|
|
|
await context.close();
|
|
|
|
await browser.close();
|
|
|
|
})();`;
|
2021-02-12 04:46:54 +03:00
|
|
|
await cli.waitFor(expectedResult2);
|
2020-12-29 01:50:12 +03:00
|
|
|
});
|