-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(pw-web): generate playwright/web.js which can be used in the browser #455
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,3 +21,5 @@ yarn.lock | |
/utils/browser/playwright-web.js | ||
lib/ | ||
playwright-*.tgz | ||
/web.js | ||
/web.js.map |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Bundling for Web | ||
|
||
Playwright contains a version bundled for web browsers under `playwright/web.js`, which | ||
installs playwright under `window.playwrightweb`. | ||
You can use it in the web page to drive another browser instance. | ||
|
||
API consists of a single `connect` function, similar to | ||
[chromiumPlaywright.connect(options)](api.md#chromiumplaywrightconnectoptions), | ||
[firefoxPlaywright.connect(options)](api.md#firefoxplaywrightconnectoptions) and | ||
[webkitPlaywright.connect(options)](api.md#webkitplaywrightconnectoptions). | ||
|
||
```html | ||
<script src='../playwright/web.js'></script> | ||
<script> | ||
async function usePlaywright() { | ||
const connect = window.playwrightweb('chromium'); // or 'firefox', 'webkit' | ||
const browser = await connect(options); | ||
// ... drive automation ... | ||
await browser.disconnect(); | ||
} | ||
</script> | ||
``` | ||
|
||
See our [playwright-web tests](https://github.com/Microsoft/playwright/blob/master/test/web.spec.js) for example. | ||
|
||
### Running inside Chrome Extension | ||
|
||
You might want to enable `unsafe-eval` inside the extension by adding the following | ||
to your `manifest.json` file: | ||
|
||
``` | ||
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'" | ||
``` | ||
|
||
Please see discussion in https://github.com/GoogleChrome/puppeteer/issues/3455. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* 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 { CRBrowser as ChromiumBrowser } from './chromium/crBrowser'; | ||
import { FFBrowser as FirefoxBrowser } from './firefox/ffBrowser'; | ||
import { WKBrowser as WebKitBrowser } from './webkit/wkBrowser'; | ||
|
||
function connect(browser: 'chromium' | 'firefox' | 'webkit') { | ||
if (browser === 'chromium') | ||
return ChromiumBrowser.connect; | ||
if (browser === 'firefox') | ||
return FirefoxBrowser.connect; | ||
if (browser === 'webkit') | ||
return WebKitBrowser.connect; | ||
throw new Error(`Unsupported browser "${browser}"`); | ||
} | ||
|
||
export = connect; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* 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. | ||
*/ | ||
|
||
const path = require('path'); | ||
|
||
module.exports = { | ||
entry: path.join(__dirname, 'web.ts'), | ||
devtool: 'source-map', | ||
module: { | ||
rules: [ | ||
{ | ||
test: /\.tsx?$/, | ||
loader: 'ts-loader', | ||
options: { | ||
transpileOnly: true | ||
}, | ||
exclude: /node_modules/ | ||
} | ||
] | ||
}, | ||
resolve: { | ||
extensions: [ '.tsx', '.ts', '.js' ] | ||
}, | ||
output: { | ||
filename: 'web.js', | ||
library: 'playwrightweb', | ||
libraryTarget: 'window', | ||
path: path.resolve(__dirname, '../') | ||
}, | ||
externals: { | ||
'events': 'dummy', | ||
'fs': 'dummy', | ||
'path': 'dummy', | ||
'debug': 'dummy', | ||
'buffer': 'dummy', | ||
'mime': 'dummy', | ||
'jpeg-js': 'dummy', | ||
'pngjs': 'dummy', | ||
'http': 'dummy', | ||
'https': 'dummy', | ||
'ws': 'dummy', | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<script src='../../web.js'></script> | ||
<script> | ||
async function setup(product, connectOptions) { | ||
window.connect = window.playwrightweb(product); | ||
window.browser = await window.connect(connectOptions); | ||
window.context = await window.browser.newContext(); | ||
window.page = await window.context.newPage(); | ||
} | ||
async function teardown() { | ||
await window.context.close(); | ||
await window.browser.disconnect(); | ||
} | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/** | ||
dgozman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* 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. | ||
*/ | ||
|
||
module.exports.describe = function({testRunner, expect, defaultBrowserOptions, playwright, product, WEBKIT}) { | ||
const {describe, xdescribe, fdescribe} = testRunner; | ||
const {it, fit, xit, dit} = testRunner; | ||
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner; | ||
|
||
describe.skip(WEBKIT)('Web', function() { | ||
beforeAll(async state => { | ||
state.controlledBrowserServer = await playwright.launchServer({ ...defaultBrowserOptions, pipe: false }); | ||
state.hostBrowserServer = await playwright.launchServer(defaultBrowserOptions); | ||
state.hostBrowser = await state.hostBrowserServer.connect(); | ||
}); | ||
|
||
afterAll(async state => { | ||
await state.hostBrowserServer.close(); | ||
state.hostBrowser = null; | ||
state.hostBrowserServer = null; | ||
|
||
await state.controlledBrowserServer.close(); | ||
state.controlledBrowserServer = null; | ||
state.webUrl = null; | ||
}); | ||
|
||
beforeEach(async state => { | ||
state.page = await state.hostBrowser.defaultContext().newPage(); | ||
state.page.on('console', message => console.log('TEST: ' + message.text())); | ||
await state.page.goto(state.sourceServer.PREFIX + '/test/assets/playwrightweb.html'); | ||
await state.page.evaluate((product, connectOptions) => setup(product, connectOptions), product.toLowerCase(), state.controlledBrowserServer.connectOptions()); | ||
}); | ||
|
||
afterEach(async state => { | ||
await state.page.evaluate(() => teardown()); | ||
await state.page.close(); | ||
state.page = null; | ||
}); | ||
|
||
it('should navigate', async({page, server}) => { | ||
const url = await page.evaluate(async url => { | ||
await page.goto(url); | ||
return page.evaluate(() => window.location.href); | ||
}, server.EMPTY_PAGE); | ||
expect(url).toBe(server.EMPTY_PAGE); | ||
}); | ||
|
||
it('should receive events', async({page, server}) => { | ||
const logs = await page.evaluate(async url => { | ||
const logs = []; | ||
page.on('console', message => logs.push(message.text())); | ||
await page.evaluate(() => console.log('hello')); | ||
await page.evaluate(() => console.log('world')); | ||
return logs; | ||
}, server.EMPTY_PAGE); | ||
expect(logs).toEqual(['hello', 'world']); | ||
}); | ||
|
||
it('should take screenshot', async({page, server}) => { | ||
const { base64, bufferClassName } = await page.evaluate(async url => { | ||
await page.setViewport({width: 500, height: 500}); | ||
await page.goto(url); | ||
const screenshot = await page.screenshot(); | ||
return { base64: screenshot.toString('base64'), bufferClassName: screenshot.constructor.name }; | ||
}, server.PREFIX + '/grid.html'); | ||
const screenshot = Buffer.from(base64, 'base64'); | ||
expect(screenshot).toBeGolden('screenshot-sanity.png'); | ||
// Verify that we use web versions of node-specific classes. | ||
expect(bufferClassName).toBe('BufferImpl'); | ||
}); | ||
}); | ||
}; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we will probably want to do something with modules for this at some point, but for now its fine.