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';
|
2021-05-09 03:45:04 +03:00
|
|
|
import { test, expect } from './inspectorTest';
|
2020-12-29 01:50:12 +03:00
|
|
|
|
2021-04-06 01:51:45 +03:00
|
|
|
const emptyHTML = new URL('file://' + path.join(__dirname, '..', 'assets', 'empty.html')).toString();
|
2021-03-15 18:07:57 +03:00
|
|
|
const launchOptions = (channel: string) => {
|
|
|
|
return channel ? `headless=False, channel="${channel}"` : 'headless=False';
|
|
|
|
};
|
2020-12-29 01:50:12 +03:00
|
|
|
|
2021-05-13 20:22:23 +03:00
|
|
|
test('should print the correct imports and context options', async ({ runCLI, channel, browserName }) => {
|
2021-03-15 18:07:57 +03:00
|
|
|
const cli = runCLI(['--target=python', emptyHTML]);
|
2021-06-26 23:11:32 +03:00
|
|
|
const expectedResult = `from playwright.sync_api import Playwright, sync_playwright
|
2020-12-29 01:50:12 +03:00
|
|
|
|
2021-06-26 23:11:32 +03:00
|
|
|
|
|
|
|
def run(playwright: Playwright) -> None:
|
2021-05-13 20:22:23 +03:00
|
|
|
browser = playwright.${browserName}.launch(${launchOptions(channel)})
|
2021-01-13 23:52:03 +03:00
|
|
|
context = browser.new_context()`;
|
2020-12-29 01:50:12 +03:00
|
|
|
await cli.waitFor(expectedResult);
|
|
|
|
expect(cli.text()).toContain(expectedResult);
|
|
|
|
});
|
|
|
|
|
2021-05-13 20:22:23 +03:00
|
|
|
test('should print the correct context options for custom settings', async ({ runCLI, channel, browserName }) => {
|
2021-03-15 18:07:57 +03:00
|
|
|
const cli = runCLI(['--color-scheme=light', '--target=python', emptyHTML]);
|
2021-06-26 23:11:32 +03:00
|
|
|
const expectedResult = `from playwright.sync_api import Playwright, sync_playwright
|
|
|
|
|
2020-12-29 01:50:12 +03:00
|
|
|
|
2021-06-26 23:11:32 +03:00
|
|
|
def run(playwright: Playwright) -> None:
|
2021-05-13 20:22:23 +03:00
|
|
|
browser = playwright.${browserName}.launch(${launchOptions(channel)})
|
2021-01-13 23:52:03 +03:00
|
|
|
context = browser.new_context(color_scheme="light")`;
|
2020-12-29 01:50:12 +03:00
|
|
|
await cli.waitFor(expectedResult);
|
|
|
|
expect(cli.text()).toContain(expectedResult);
|
|
|
|
});
|
|
|
|
|
2021-05-13 20:22:23 +03:00
|
|
|
test('should print the correct context options when using a device', async ({ browserName, channel, runCLI }) => {
|
2021-03-12 07:22:50 +03:00
|
|
|
test.skip(browserName !== 'chromium');
|
2021-04-02 21:19:26 +03:00
|
|
|
|
2021-03-15 18:07:57 +03:00
|
|
|
const cli = runCLI(['--device=Pixel 2', '--target=python', emptyHTML]);
|
2021-06-26 23:11:32 +03:00
|
|
|
const expectedResult = `from playwright.sync_api import Playwright, sync_playwright
|
2020-12-29 01:50:12 +03:00
|
|
|
|
2021-06-26 23:11:32 +03:00
|
|
|
|
|
|
|
def run(playwright: Playwright) -> None:
|
2021-05-13 20:22:23 +03:00
|
|
|
browser = playwright.chromium.launch(${launchOptions(channel)})
|
2021-01-13 23:52:03 +03:00
|
|
|
context = browser.new_context(**playwright.devices["Pixel 2"])`;
|
2020-12-29 01:50:12 +03:00
|
|
|
await cli.waitFor(expectedResult);
|
|
|
|
expect(cli.text()).toContain(expectedResult);
|
|
|
|
});
|
|
|
|
|
2021-05-13 20:22:23 +03:00
|
|
|
test('should print the correct context options when using a device and additional options', async ({ browserName, channel, runCLI }) => {
|
2021-03-12 07:22:50 +03:00
|
|
|
test.skip(browserName !== 'webkit');
|
2021-04-02 21:19:26 +03:00
|
|
|
|
2021-03-15 18:07:57 +03:00
|
|
|
const cli = runCLI(['--color-scheme=light', '--device=iPhone 11', '--target=python', emptyHTML]);
|
2021-06-26 23:11:32 +03:00
|
|
|
const expectedResult = `from playwright.sync_api import Playwright, sync_playwright
|
|
|
|
|
2020-12-29 01:50:12 +03:00
|
|
|
|
2021-06-26 23:11:32 +03:00
|
|
|
def run(playwright: Playwright) -> None:
|
2021-05-13 20:22:23 +03:00
|
|
|
browser = playwright.webkit.launch(${launchOptions(channel)})
|
2021-02-12 04:46:54 +03:00
|
|
|
context = browser.new_context(**playwright.devices["iPhone 11"], color_scheme="light")`;
|
2020-12-29 01:50:12 +03:00
|
|
|
await cli.waitFor(expectedResult);
|
|
|
|
expect(cli.text()).toContain(expectedResult);
|
|
|
|
});
|
|
|
|
|
2021-05-13 20:22:23 +03:00
|
|
|
test('should save the codegen output to a file if specified', async ({ runCLI, channel, browserName }, testInfo) => {
|
2020-12-29 01:50:12 +03:00
|
|
|
const tmpFile = testInfo.outputPath('script.js');
|
2021-03-15 18:07:57 +03:00
|
|
|
const cli = runCLI(['--target=python', '--output', tmpFile, emptyHTML]);
|
2020-12-29 01:50:12 +03:00
|
|
|
await cli.exited;
|
|
|
|
const content = fs.readFileSync(tmpFile);
|
2021-06-26 23:11:32 +03:00
|
|
|
expect(content.toString()).toBe(`from playwright.sync_api import Playwright, sync_playwright
|
2020-12-29 01:50:12 +03:00
|
|
|
|
2021-06-26 23:11:32 +03:00
|
|
|
|
|
|
|
def run(playwright: Playwright) -> None:
|
2021-05-13 20:22:23 +03:00
|
|
|
browser = playwright.${browserName}.launch(${launchOptions(channel)})
|
2021-01-13 23:52:03 +03:00
|
|
|
context = browser.new_context()
|
2020-12-29 01:50:12 +03:00
|
|
|
|
|
|
|
# Open new page
|
2021-01-13 23:52:03 +03:00
|
|
|
page = context.new_page()
|
2020-12-29 01:50:12 +03:00
|
|
|
|
|
|
|
# Go to ${emptyHTML}
|
|
|
|
page.goto("${emptyHTML}")
|
|
|
|
|
|
|
|
# Close page
|
|
|
|
page.close()
|
|
|
|
|
|
|
|
# ---------------------
|
|
|
|
context.close()
|
|
|
|
browser.close()
|
|
|
|
|
2021-06-26 23:11:32 +03:00
|
|
|
|
2020-12-29 01:50:12 +03:00
|
|
|
with sync_playwright() as playwright:
|
2021-06-26 23:11:32 +03:00
|
|
|
run(playwright)
|
|
|
|
`);
|
2020-12-29 01:50:12 +03:00
|
|
|
});
|
|
|
|
|
2021-05-13 20:22:23 +03:00
|
|
|
test('should print load/save storage_state', async ({ runCLI, channel, browserName }, 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-15 18:07:57 +03:00
|
|
|
const cli = runCLI([`--load-storage=${loadFileName}`, `--save-storage=${saveFileName}`, '--target=python', emptyHTML]);
|
2021-06-26 23:11:32 +03:00
|
|
|
const expectedResult1 = `from playwright.sync_api import Playwright, sync_playwright
|
2020-12-29 01:50:12 +03:00
|
|
|
|
2021-06-26 23:11:32 +03:00
|
|
|
|
|
|
|
def run(playwright: Playwright) -> None:
|
2021-05-13 20:22:23 +03:00
|
|
|
browser = playwright.${browserName}.launch(${launchOptions(channel)})
|
2021-08-24 05:38:50 +03:00
|
|
|
context = browser.new_context(storage_state="${loadFileName.replace(/\\/g, '\\\\')}")`;
|
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 = `
|
2021-01-13 23:52:03 +03:00
|
|
|
# ---------------------
|
2021-08-24 05:38:50 +03:00
|
|
|
context.storage_state(path="${saveFileName.replace(/\\/g, '\\\\')}")
|
2021-01-13 23:52:03 +03:00
|
|
|
context.close()
|
|
|
|
browser.close()
|
2020-12-29 01:50:12 +03:00
|
|
|
|
2021-06-26 23:11:32 +03:00
|
|
|
|
2021-01-13 23:52:03 +03:00
|
|
|
with sync_playwright() as playwright:
|
2021-06-26 23:11:32 +03:00
|
|
|
run(playwright)
|
|
|
|
`;
|
2021-02-12 04:46:54 +03:00
|
|
|
await cli.waitFor(expectedResult2);
|
2020-12-29 01:50:12 +03:00
|
|
|
});
|