From 567316cd2a3eb21099fd967e2f796157a908a7ee Mon Sep 17 00:00:00 2001 From: Max Schmitt Date: Wed, 5 Aug 2020 22:04:58 +0200 Subject: [PATCH 1/8] feat: aded Request.postDataJSON (#148) --- playwright/async_api.py | 13 +++++++++++++ playwright/network.py | 13 +++++++++++++ playwright/sync_api.py | 13 +++++++++++++ tests/async/test_network.py | 30 ++++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+) diff --git a/playwright/async_api.py b/playwright/async_api.py index 27942e4c3..579cfbf4b 100644 --- a/playwright/async_api.py +++ b/playwright/async_api.py @@ -119,6 +119,19 @@ def postData(self) -> typing.Union[str, NoneType]: """ return mapping.from_maybe_impl(self._impl_obj.postData) + @property + def postDataJSON(self) -> typing.Union[typing.Dict, NoneType]: + """Request.postDataJSON + + When the response is `application/x-www-form-urlencoded` then a key/value object of the values will be returned. Otherwise it will be parsed as JSON. + + Returns + ------- + Optional[Dict] + Parsed request's body for `form-urlencoded` and JSON as a fallback if any. + """ + return mapping.from_maybe_impl(self._impl_obj.postDataJSON) + @property def headers(self) -> typing.Dict[str, str]: """Request.headers diff --git a/playwright/network.py b/playwright/network.py index 09993f4c5..6f0442804 100644 --- a/playwright/network.py +++ b/playwright/network.py @@ -17,6 +17,7 @@ import mimetypes from pathlib import Path from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union, cast +from urllib import parse from playwright.connection import ChannelOwner, from_channel, from_nullable_channel from playwright.helper import ( @@ -63,6 +64,18 @@ def postData(self) -> Optional[str]: return None return base64.b64decode(b64_content).decode() + @property + def postDataJSON(self) -> Optional[Dict]: + post_data = self.postData + if not post_data: + return None + content_type = self.headers["content-type"] + if not content_type: + return None + if content_type == "application/x-www-form-urlencoded": + return dict(parse.parse_qsl(post_data)) + return json.loads(post_data) + @property def headers(self) -> Dict[str, str]: return parse_headers(self._initializer["headers"]) diff --git a/playwright/sync_api.py b/playwright/sync_api.py index d23b5ebcf..e74d5ac79 100644 --- a/playwright/sync_api.py +++ b/playwright/sync_api.py @@ -119,6 +119,19 @@ def postData(self) -> typing.Union[str, NoneType]: """ return mapping.from_maybe_impl(self._impl_obj.postData) + @property + def postDataJSON(self) -> typing.Union[typing.Dict, NoneType]: + """Request.postDataJSON + + When the response is `application/x-www-form-urlencoded` then a key/value object of the values will be returned. Otherwise it will be parsed as JSON. + + Returns + ------- + Optional[Dict] + Parsed request's body for `form-urlencoded` and JSON as a fallback if any. + """ + return mapping.from_maybe_impl(self._impl_obj.postDataJSON) + @property def headers(self) -> typing.Dict[str, str]: """Request.headers diff --git a/tests/async/test_network.py b/tests/async/test_network.py index 7d64f3614..c1bb5f62c 100644 --- a/tests/async/test_network.py +++ b/tests/async/test_network.py @@ -207,6 +207,36 @@ async def test_request_postdata_should_be_undefined_when_there_is_no_post_data( assert response.request.postData is None +async def test_should_parse_the_json_post_data(page, server): + await page.goto(server.EMPTY_PAGE) + server.set_route("/post", lambda req: req.finish()) + requests = [] + page.on("request", lambda r: requests.append(r)) + await page.evaluate( + """() => fetch('./post', { method: 'POST', body: JSON.stringify({ foo: 'bar' }) })""" + ) + assert len(requests) == 1 + assert requests[0].postDataJSON == {"foo": "bar"} + + +async def test_should_parse_the_data_if_content_type_is_form_urlencoded(page, server): + await page.goto(server.EMPTY_PAGE) + server.set_route("/post", lambda req: req.finish()) + requests = [] + page.on("request", lambda r: requests.append(r)) + await page.setContent( + """
""" + ) + await page.click("input[type=submit]") + assert len(requests) == 1 + assert requests[0].postDataJSON == {"foo": "bar", "baz": "123"} + + +async def test_should_be_undefined_when_there_is_no_post_data(page, server): + response = await page.goto(server.EMPTY_PAGE) + assert response.request.postDataJSON is None + + async def test_response_text_should_work(page, server): response = await page.goto(server.PREFIX + "/simple.json") assert await response.text() == '{"foo": "bar"}\n' From 4887636357402943f7eeb09b3092cc388c8a0f05 Mon Sep 17 00:00:00 2001 From: Max Schmitt Date: Thu, 6 Aug 2020 00:07:30 +0200 Subject: [PATCH 2/8] feat(roll): roll Playwright 1.2.0-next.1596653191842 (#149) --- driver/main.js | 15 ++++++++++++++- driver/package.json | 2 +- playwright/drivers/browsers.json | 2 +- tests/async/test_navigation.py | 2 +- 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/driver/main.js b/driver/main.js index 6dc6db166..6b5ef07e2 100644 --- a/driver/main.js +++ b/driver/main.js @@ -15,8 +15,21 @@ */ const path = require('path'); +const fs = require('fs'); +const os = require('os'); +const util = require('util'); + +(async () => { + if (os.platform() === 'win32') { + const checkDbPath = path.join(__dirname, 'node_modules', 'playwright', 'bin', 'PrintDeps.exe') + + const content = await util.promisify(fs.readFile)(checkDbPath); + const output = path.join(os.tmpdir(), 'ms-playwright-print-deps.exe') + await util.promisify(fs.writeFile)(output, content) + + process.env.PW_PRINT_DEPS_WINDOWS_EXECUTABLE = output + } -(async() => { if (process.argv.includes('install')) { await require('playwright/lib/install/installer').installBrowsersWithProgressBar(path.dirname(process.argv[0])); return; diff --git a/driver/package.json b/driver/package.json index 41e89ee21..af51d7670 100644 --- a/driver/package.json +++ b/driver/package.json @@ -13,7 +13,7 @@ }, "license": "Apache-2.0", "dependencies": { - "playwright": "1.2.0-next.1596151449209" + "playwright": "1.3.0-next.1596659749397" }, "devDependencies": { "pkg": "^4.4.9" diff --git a/playwright/drivers/browsers.json b/playwright/drivers/browsers.json index 376dad895..839fd9f18 100644 --- a/playwright/drivers/browsers.json +++ b/playwright/drivers/browsers.json @@ -13,7 +13,7 @@ }, { "name": "webkit", - "revision": "1319", + "revision": "1322", "download": true } ] diff --git a/tests/async/test_navigation.py b/tests/async/test_navigation.py index 3fd77a75e..5c02712e9 100644 --- a/tests/async/test_navigation.py +++ b/tests/async/test_navigation.py @@ -449,7 +449,7 @@ async def test_wait_for_nav_should_respect_timeout(page, server): # assert f'navigated to "{server.EMPTY_PAGE}"' in exc_info.value.message -@pytest.mark.skip_browser("webkit") +@pytest.mark.skip("TODO: needs to be investigated, flaky") async def test_wait_for_nav_should_work_with_both_domcontentloaded_and_load( page, server ): From 0f6451edb26022fb17f60d9307716ae5881e5ffb Mon Sep 17 00:00:00 2001 From: Max Schmitt Date: Thu, 6 Aug 2020 00:08:00 +0200 Subject: [PATCH 3/8] feat: added Request.postDataBuffer (#150) --- playwright/async_api.py | 11 ++++++++++ playwright/network.py | 13 +++++++++--- playwright/sync_api.py | 11 ++++++++++ tests/async/test_interception.py | 2 +- tests/async/test_network.py | 35 ++++++++++++++++++++++++++++++++ tests/server.py | 2 +- 6 files changed, 69 insertions(+), 5 deletions(-) diff --git a/playwright/async_api.py b/playwright/async_api.py index 579cfbf4b..6cc468f6a 100644 --- a/playwright/async_api.py +++ b/playwright/async_api.py @@ -132,6 +132,17 @@ def postDataJSON(self) -> typing.Union[typing.Dict, NoneType]: """ return mapping.from_maybe_impl(self._impl_obj.postDataJSON) + @property + def postDataBuffer(self) -> typing.Union[bytes, NoneType]: + """Request.postDataBuffer + + Returns + ------- + Optional[bytes] + Request's post body in a binary form, if any. + """ + return mapping.from_maybe_impl(self._impl_obj.postDataBuffer) + @property def headers(self) -> typing.Dict[str, str]: """Request.headers diff --git a/playwright/network.py b/playwright/network.py index 6f0442804..b4e6e986e 100644 --- a/playwright/network.py +++ b/playwright/network.py @@ -59,10 +59,10 @@ def method(self) -> str: @property def postData(self) -> Optional[str]: - b64_content = self._initializer.get("postData") - if not b64_content: + data = self.postDataBuffer + if not data: return None - return base64.b64decode(b64_content).decode() + return data.decode() @property def postDataJSON(self) -> Optional[Dict]: @@ -76,6 +76,13 @@ def postDataJSON(self) -> Optional[Dict]: return dict(parse.parse_qsl(post_data)) return json.loads(post_data) + @property + def postDataBuffer(self) -> Optional[bytes]: + b64_content = self._initializer.get("postData") + if not b64_content: + return None + return base64.b64decode(b64_content) + @property def headers(self) -> Dict[str, str]: return parse_headers(self._initializer["headers"]) diff --git a/playwright/sync_api.py b/playwright/sync_api.py index e74d5ac79..e07080efe 100644 --- a/playwright/sync_api.py +++ b/playwright/sync_api.py @@ -132,6 +132,17 @@ def postDataJSON(self) -> typing.Union[typing.Dict, NoneType]: """ return mapping.from_maybe_impl(self._impl_obj.postDataJSON) + @property + def postDataBuffer(self) -> typing.Union[bytes, NoneType]: + """Request.postDataBuffer + + Returns + ------- + Optional[bytes] + Request's post body in a binary form, if any. + """ + return mapping.from_maybe_impl(self._impl_obj.postDataBuffer) + @property def headers(self) -> typing.Dict[str, str]: """Request.headers diff --git a/tests/async/test_interception.py b/tests/async/test_interception.py index 817a400ad..f2e7c0b44 100644 --- a/tests/async/test_interception.py +++ b/tests/async/test_interception.py @@ -727,7 +727,7 @@ async def test_request_continue_should_amend_post_data(page, server): """ ), ) - assert serverRequest.post_body == "doggo" + assert serverRequest.post_body.decode() == "doggo" async def test_request_fulfill_should_work_a(page, server): diff --git a/tests/async/test_network.py b/tests/async/test_network.py index c1bb5f62c..8d7c24a16 100644 --- a/tests/async/test_network.py +++ b/tests/async/test_network.py @@ -237,6 +237,41 @@ async def test_should_be_undefined_when_there_is_no_post_data(page, server): assert response.request.postDataJSON is None +async def test_should_work_with_binary_post_data(page, server): + await page.goto(server.EMPTY_PAGE) + server.set_route("/post", lambda req: req.finish()) + requests = [] + page.on("request", lambda r: requests.append(r)) + await page.evaluate( + """async () => { + await fetch('./post', { method: 'POST', body: new Uint8Array(Array.from(Array(256).keys())) }) + }""" + ) + assert len(requests) == 1 + buffer = requests[0].postDataBuffer + assert len(buffer) == 256 + for i in range(256): + assert buffer[i] == i + + +async def test_should_work_with_binary_post_data_and_interception(page, server): + await page.goto(server.EMPTY_PAGE) + server.set_route("/post", lambda req: req.finish()) + requests = [] + await page.route("/post", lambda route: asyncio.ensure_future(route.continue_())) + page.on("request", lambda r: requests.append(r)) + await page.evaluate( + """async () => { + await fetch('./post', { method: 'POST', body: new Uint8Array(Array.from(Array(256).keys())) }) + }""" + ) + assert len(requests) == 1 + buffer = requests[0].postDataBuffer + assert len(buffer) == 256 + for i in range(256): + assert buffer[i] == i + + async def test_response_text_should_work(page, server): response = await page.goto(server.PREFIX + "/simple.json") assert await response.text() == '{"foo": "bar"}\n' diff --git a/tests/server.py b/tests/server.py index e34b3c105..9c2c523b5 100644 --- a/tests/server.py +++ b/tests/server.py @@ -78,7 +78,7 @@ def start(self): class TestServerHTTPHandler(http.Request): def process(self): request = self - self.post_body = request.content.read().decode() + self.post_body = request.content.read() request.content.seek(0, 0) uri = request.uri.decode() if request_subscribers.get(uri): From ef38e91b561ce0ee7160f092053c2bebb588ed1e Mon Sep 17 00:00:00 2001 From: Max Schmitt Date: Thu, 6 Aug 2020 00:31:12 +0200 Subject: [PATCH 4/8] feat: added Download.saveAs(path) (#147) --- playwright/async_api.py | 12 ++++ playwright/download.py | 7 ++- playwright/sync_api.py | 12 ++++ tests/async/test_download.py | 108 ++++++++++++++++++++++++++++++++++- 4 files changed, 135 insertions(+), 4 deletions(-) diff --git a/playwright/async_api.py b/playwright/async_api.py index 6cc468f6a..490f41e53 100644 --- a/playwright/async_api.py +++ b/playwright/async_api.py @@ -2939,6 +2939,18 @@ async def path(self) -> typing.Union[str, NoneType]: """ return mapping.from_maybe_impl(await self._impl_obj.path()) + async def saveAs(self, path: typing.Union[pathlib.Path, str]) -> NoneType: + """Download.saveAs + + Saves the download to a user-specified path. + + Parameters + ---------- + path : Union[pathlib.Path, str] + Path where the download should be saved. + """ + return mapping.from_maybe_impl(await self._impl_obj.saveAs(path=path)) + mapping.register(DownloadImpl, Download) diff --git a/playwright/download.py b/playwright/download.py index e0eca58ec..1c183fd86 100644 --- a/playwright/download.py +++ b/playwright/download.py @@ -12,7 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -from typing import Dict, Optional +from pathlib import Path +from typing import Dict, Optional, Union from playwright.connection import ChannelOwner @@ -39,3 +40,7 @@ async def failure(self) -> Optional[str]: async def path(self) -> Optional[str]: return await self._channel.send("path") + + async def saveAs(self, path: Union[Path, str]) -> None: + path = str(Path(path)) + return await self._channel.send("saveAs", dict(path=path)) diff --git a/playwright/sync_api.py b/playwright/sync_api.py index e07080efe..8ea629752 100644 --- a/playwright/sync_api.py +++ b/playwright/sync_api.py @@ -3065,6 +3065,18 @@ def path(self) -> typing.Union[str, NoneType]: """ return mapping.from_maybe_impl(self._sync(self._impl_obj.path())) + def saveAs(self, path: typing.Union[pathlib.Path, str]) -> NoneType: + """Download.saveAs + + Saves the download to a user-specified path. + + Parameters + ---------- + path : Union[pathlib.Path, str] + Path where the download should be saved. + """ + return mapping.from_maybe_impl(self._sync(self._impl_obj.saveAs(path=path))) + mapping.register(DownloadImpl, Download) diff --git a/tests/async/test_download.py b/tests/async/test_download.py index 784801187..70c317610 100644 --- a/tests/async/test_download.py +++ b/tests/async/test_download.py @@ -14,11 +14,12 @@ import asyncio import os from asyncio.futures import Future +from pathlib import Path from typing import Optional import pytest -from playwright import Error as PlaywrightError +from playwright import Error from playwright.async_api import Browser, Page @@ -53,10 +54,10 @@ async def test_should_report_downloads_with_acceptDownloads_false(page: Page, se download = (await asyncio.gather(page.waitForEvent("download"), page.click("a")))[0] assert download.url == f"{server.PREFIX}/downloadWithFilename" assert download.suggestedFilename == "file.txt" - error: Optional[PlaywrightError] = None + error: Optional[Error] = None try: await download.path() - except PlaywrightError as exc: + except Error as exc: error = exc assert "acceptDownloads" in await download.failure() assert error @@ -73,6 +74,107 @@ async def test_should_report_downloads_with_acceptDownloads_true(browser, server await page.close() +async def test_should_save_to_user_specified_path(tmpdir: Path, browser, server): + page = await browser.newPage(acceptDownloads=True) + await page.setContent(f'download') + [download, _] = await asyncio.gather(page.waitForEvent("download"), page.click("a")) + user_path = tmpdir / "download.txt" + await download.saveAs(user_path) + assert user_path.exists() + assert user_path.read_text("utf-8") == "Hello world" + await page.close() + + +async def test_should_save_to_user_specified_path_without_updating_original_path( + tmpdir, browser, server +): + page = await browser.newPage(acceptDownloads=True) + await page.setContent(f'download') + [download, _] = await asyncio.gather(page.waitForEvent("download"), page.click("a")) + user_path = tmpdir / "download.txt" + await download.saveAs(user_path) + assert user_path.exists() + assert user_path.read_text("utf-8") == "Hello world" + + originalPath = Path(await download.path()) + assert originalPath.exists() + assert originalPath.read_text("utf-8") == "Hello world" + await page.close() + + +async def test_should_save_to_two_different_paths_with_multiple_saveAs_calls( + tmpdir, browser, server +): + page = await browser.newPage(acceptDownloads=True) + await page.setContent(f'download') + [download, _] = await asyncio.gather(page.waitForEvent("download"), page.click("a")) + user_path = tmpdir / "download.txt" + await download.saveAs(user_path) + assert user_path.exists() + assert user_path.read_text("utf-8") == "Hello world" + + anotheruser_path = tmpdir / "download (2).txt" + await download.saveAs(anotheruser_path) + assert anotheruser_path.exists() + assert anotheruser_path.read_text("utf-8") == "Hello world" + await page.close() + + +async def test_should_save_to_overwritten_filepath(tmpdir: Path, browser, server): + page = await browser.newPage(acceptDownloads=True) + await page.setContent(f'download') + [download, _] = await asyncio.gather(page.waitForEvent("download"), page.click("a")) + user_path = tmpdir / "download.txt" + await download.saveAs(user_path) + assert len(list(Path(tmpdir).glob("*.*"))) == 1 + await download.saveAs(user_path) + assert len(list(Path(tmpdir).glob("*.*"))) == 1 + assert user_path.exists() + assert user_path.read_text("utf-8") == "Hello world" + await page.close() + + +async def test_should_create_subdirectories_when_saving_to_non_existent_user_specified_path( + tmpdir, browser, server +): + page = await browser.newPage(acceptDownloads=True) + await page.setContent(f'download') + [download, _] = await asyncio.gather(page.waitForEvent("download"), page.click("a")) + nested_path = tmpdir / "these" / "are" / "directories" / "download.txt" + await download.saveAs(nested_path) + assert nested_path.exists() + assert nested_path.read_text("utf-8") == "Hello world" + await page.close() + + +async def test_should_error_when_saving_with_downloads_disabled( + tmpdir, browser, server +): + page = await browser.newPage(acceptDownloads=False) + await page.setContent(f'download') + [download, _] = await asyncio.gather(page.waitForEvent("download"), page.click("a")) + user_path = tmpdir / "download.txt" + with pytest.raises(Error) as exc: + await download.saveAs(user_path) + assert ( + "Pass { acceptDownloads: true } when you are creating your browser context" + in exc.value.message + ) + await page.close() + + +async def test_should_error_when_saving_after_deletion(tmpdir, browser, server): + page = await browser.newPage(acceptDownloads=True) + await page.setContent(f'download') + [download, _] = await asyncio.gather(page.waitForEvent("download"), page.click("a")) + user_path = tmpdir / "download.txt" + await download.delete() + with pytest.raises(Error) as exc: + await download.saveAs(user_path) + assert "Download already deleted. Save before deleting." in exc.value.message + await page.close() + + async def test_should_report_non_navigation_downloads(browser, server): # Mac WebKit embedder does not download in this case, although Safari does. def handle_download(request): From c48e5668a86a2cd9aedff12122d8faee22617eb4 Mon Sep 17 00:00:00 2001 From: Pavel Feldman Date: Thu, 6 Aug 2020 09:53:42 -0700 Subject: [PATCH 5/8] docs: update README.md (#154) --- README.md | 144 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 109 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index 60bb25773..d95552e16 100644 --- a/README.md +++ b/README.md @@ -44,21 +44,11 @@ Playwright is built to automate the broad and growing set of web browser capabil ### Pytest -For writing end-to-end tests we recommend to use the official [Pytest plugin](https://github.com/microsoft/playwright-pytest#readme) for Playwright. It contains utilities for running it on multiple browsers, having a new page instance on every test or base-url support via a command-line argument. This will in the end look like that: +Playwright can be used as a library in your application or as a part of the testing solution. We recommend using our [Pytest plugin](https://github.com/microsoft/playwright-pytest#readme) for testing. -```py -def test_playwright_is_visible_on_google(page): - page.goto("https://www.google.com") - page.type("input[name=q]", "Playwright GitHub") - page.click("input[type=submit]") - page.waitForSelector("text=microsoft/Playwright") -``` - -For more information checkout the project on [GitHub](https://github.com/microsoft/playwright-pytest#readme). - -### Standalone +As a library, Playwright offers both blocking (synchronous) API and asyncio API (async/await). You can pick the one that works best for you. They are identical in terms of capabilities and only differ in a way one consumes the API. -For using Playwright standalone, you can either use the sync version or the async variant (async/await). In most cases the sync variant is the right choice to automate the web browsers e.g. for writing end-to-end tests. Both will get initialized with a context manager. +Below are some of the examples on how these variations of the API can be used: #### Sync variant @@ -82,33 +72,30 @@ from playwright import async_playwright async def main(): async with async_playwright() as p: - browser = await p.webkit.launch() - page = await browser.newPage() - await page.goto('http://whatsmyuseragent.org/') - await page.screenshot(path=f'example-{browser_type.name}.png') - await browser.close() + for browser_type in [p.chromium, p.firefox, p.webkit]: + browser = await browser_type.launch() + page = await browser.newPage() + await page.goto('http://whatsmyuseragent.org/') + await page.screenshot(path=f'example-{browser_type.name}.png') + await browser.close() asyncio.get_event_loop().run_until_complete(main()) ``` -## Examples - -#### Page screenshot - -This code snippet navigates to whatsmyuseragent.org in Chromium, Firefox and WebKit, and saves 3 screenshots. +#### Using pytest-playwright ```py -from playwright import sync_playwright - -with sync_playwright() as p: - for browser_type in [p.chromium, p.firefox, p.webkit]: - browser = browser_type.launch() - page = browser.newPage() - page.goto('http://whatsmyuseragent.org/') - page.screenshot(path=f'example-{browser_type.name}.png') - browser.close() +def test_playwright_is_visible_on_google(page): + page.goto("https://www.google.com") + page.type("input[name=q]", "Playwright GitHub") + page.click("input[type=submit]") + page.waitForSelector("text=microsoft/Playwright") ``` +For more information on pytest-playwright, see [GitHub](https://github.com/microsoft/playwright-pytest#readme). + +## More examples + #### Mobile and geolocation This snippet emulates Mobile Safari on a device at a given geolocation, navigates to maps.google.com, performs action and takes a screenshot. @@ -128,7 +115,6 @@ with sync_playwright() as p: page = context.newPage() page.goto('https://maps.google.com') page.click('text="Your location"') - page.waitForRequest('*preview/pwa') page.screenshot(path='colosseum-iphone.png') browser.close() ``` @@ -152,7 +138,6 @@ async def main(): page = await context.newPage() await page.goto('https://maps.google.com') await page.click('text="Your location"') - await page.waitForRequest('*preview/pwa') await page.screenshot(path='colosseum-iphone.png') await browser.close() @@ -251,9 +236,98 @@ async def main(): asyncio.get_event_loop().run_until_complete(main()) ``` +## Documentation + +We are in the process of converting the [documentation](https://playwright.dev/) from the Node form to the Python one. But you can go ahead and use the Node's documentation because the API is pretty much the same. You might have noticed that Playwright uses non-Python naming conventions, `camelCase` instead of the `snake_case` for its methods. We recognize that this is not ideal, but it was done deliberately, so that you could rely upon the stack overflow answers and the documentation of the Playwright for Node. + +### Understanding examples from the JavaScript documentation + +You can use all the same methods and arguments as [documented](https://playwright.dev/), just remember that since Python allows named arguments, we didn't need to put `options` parameter into every call as we had to in the Node version: + +So when you see example like this in JavaScript + +```js +await webkit.launch({ headless: false }); +``` + +It translates into Python like this: + +```py +webkit.launch(headless=False) +``` + +If you are using an IDE, it'll suggest parameters that are available in every call. + +### Evaluating functions + +Another difference is that in the JavaScript version, `page.evaluate` accepts JavaScript functions, while this does not make any sense in the Python version. + +In JavaScript it will be documented as: + +```js +const result = await page.evaluate(([x, y]) => { + return Promise.resolve(x * y); +}, [7, 8]); +console.log(result); // prints "56" +``` + +And in Python that would look like: + +```py +result = page.evaluate(""" + ([x, y]) => { + return Promise.resolve(x * y); + }""", + [7, 8]) +print(result) # prints "56" +``` + +The library will detect that what are passing it is a function and will invoke it with the given parameters. You can opt out of this function detection and pass `force_expr=True` to all evaluate functions, but you probably will never need to do that. + +### Using context managers + +Python enabled us to do some of the things that were not possible in the Node version and we used the opportunity. Instead of using the `page.waitFor*` methods, we recommend using corresponding `page.expect_*` context manager. + +In JavaScript it will be documented as: + +```js +const [ download ] = await Promise.all([ + page.waitForEvent('download'), // <-- start waiting for the download + page.click('button#delayed-download') // <-- perform the action that directly or indirectly initiates it. +]); +const path = await download.path(); +``` + +And in Python that would look much simpler: + +```py +with page.expect_download() as download_info: + page.click("button#delayed-download") +download = download_info.value +path = download.path() +``` + +Similarly, for waiting for the network response: + +```js +const [response] = await Promise.all([ + page.waitForResponse('**/api/fetch_data'), + page.click('button#update'), +]); +``` + +Becomes + +```py +with page.expect_response("**/api/fetch_data"): + page.click("button#update") +``` + ## Is Playwright for Python ready? -We are ready for your feedback, but we are still covering Playwright Python with the tests, so expect some API changes and don't use for production. +Yes, Playwright for Python is ready. We are still not at the version v1.0, so minor breaking API changes could potentially happen. But a) this is unlikely and b) we will only do that if we know it improves your experience with the new library. We'd like to collect your feedback before we freeze the API for v1.0. + +> Note: We don't yet support some of the edge-cases of the vendor-specific APIs such as collecting chromium trace, coverage report, etc. ## Resources From 1ce365389e8328ca551378840d84a7cff31ef73d Mon Sep 17 00:00:00 2001 From: Pavel Feldman Date: Thu, 6 Aug 2020 09:56:53 -0700 Subject: [PATCH 6/8] docs: update README.md (#155) --- README.md | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/README.md b/README.md index d95552e16..a6fec596a 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ [![PyPI version](https://badge.fury.io/py/playwright.svg)](https://pypi.python.org/pypi/playwright/) [![Join Slack](https://img.shields.io/badge/join-slack-infomational)](https://join.slack.com/t/playwright/shared_invite/enQtOTEyMTUxMzgxMjIwLThjMDUxZmIyNTRiMTJjNjIyMzdmZDA3MTQxZWUwZTFjZjQwNGYxZGM5MzRmNzZlMWI5ZWUyOTkzMjE5Njg1NDg) [![Chromium version](https://img.shields.io/badge/chromium-86.0.4217.0-blue.svg?logo=google-chrome)](https://www.chromium.org/Home) [![Firefox version](https://img.shields.io/badge/firefox-78.0b5-blue.svg?logo=mozilla-firefox)](https://www.mozilla.org/en-US/firefox/new/) [![WebKit version](https://img.shields.io/badge/webkit-14.0-blue.svg?logo=safari)](https://webkit.org/) [![Coverage Status](https://coveralls.io/repos/github/microsoft/playwright-python/badge.svg?branch=master)](https://coveralls.io/github/microsoft/playwright-python?branch=master) -##### [Docs](https://github.com/microsoft/playwright/blob/master/docs/README.md) | [API reference](https://github.com/microsoft/playwright/blob/master/docs/api.md) +##### [Docs](#documentation) | [API reference](https://github.com/microsoft/playwright/blob/master/docs/api.md) Playwright is a Python library to automate [Chromium](https://www.chromium.org/Home), [Firefox](https://www.mozilla.org/en-US/firefox/new/) and [WebKit](https://webkit.org/) with a single API. Playwright is built to enable cross-browser web automation that is **ever-green**, **capable**, **reliable** and **fast**. @@ -24,10 +24,6 @@ python -m playwright install This installs Playwright and browser binaries for Chromium, Firefox and WebKit. Once installed, you can `import` Playwright in a Python script and automate web browser interactions. -* [Getting started](https://github.com/microsoft/playwright/blob/master/docs/intro.md) -* [Installation configuration](https://github.com/microsoft/playwright/blob/master/docs/installation.md) -* [API reference](https://github.com/microsoft/playwright/blob/master/docs/api.md) - ## Capabilities Playwright is built to automate the broad and growing set of web browser capabilities used by Single Page Apps and Progressive Web Apps. From 3c333c43793a0a4da9800bd392d4e1e4c779ecff Mon Sep 17 00:00:00 2001 From: Pavel Feldman Date: Thu, 6 Aug 2020 10:43:00 -0700 Subject: [PATCH 7/8] docs: use Node.js name (#156) --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a6fec596a..c209ca198 100644 --- a/README.md +++ b/README.md @@ -234,11 +234,11 @@ asyncio.get_event_loop().run_until_complete(main()) ## Documentation -We are in the process of converting the [documentation](https://playwright.dev/) from the Node form to the Python one. But you can go ahead and use the Node's documentation because the API is pretty much the same. You might have noticed that Playwright uses non-Python naming conventions, `camelCase` instead of the `snake_case` for its methods. We recognize that this is not ideal, but it was done deliberately, so that you could rely upon the stack overflow answers and the documentation of the Playwright for Node. +We are in the process of converting the [documentation](https://playwright.dev/) from the Node.js form to the Python one. But you can go ahead and use the Node.js documentation because the API is pretty much the same. You might have noticed that Playwright uses non-Python naming conventions, `camelCase` instead of the `snake_case` for its methods. We recognize that this is not ideal, but it was done deliberately, so that you could rely upon the stack overflow answers and the documentation of the Playwright for Node.js. ### Understanding examples from the JavaScript documentation -You can use all the same methods and arguments as [documented](https://playwright.dev/), just remember that since Python allows named arguments, we didn't need to put `options` parameter into every call as we had to in the Node version: +You can use all the same methods and arguments as [documented](https://playwright.dev/), just remember that since Python allows named arguments, we didn't need to put `options` parameter into every call as we had to in the Node.js version: So when you see example like this in JavaScript @@ -282,7 +282,7 @@ The library will detect that what are passing it is a function and will invoke i ### Using context managers -Python enabled us to do some of the things that were not possible in the Node version and we used the opportunity. Instead of using the `page.waitFor*` methods, we recommend using corresponding `page.expect_*` context manager. +Python enabled us to do some of the things that were not possible in the Node.js version and we used the opportunity. Instead of using the `page.waitFor*` methods, we recommend using corresponding `page.expect_*` context manager. In JavaScript it will be documented as: From 26ac810f44e88dbade3364f850a06b7b97340a7f Mon Sep 17 00:00:00 2001 From: Max Schmitt Date: Thu, 6 Aug 2020 19:43:18 +0200 Subject: [PATCH 8/8] tests: added browser tests (#153) --- playwright/async_api.py | 23 +++++++++-------- playwright/browser.py | 5 ++-- playwright/sync_api.py | 23 +++++++++-------- tests/async/test_browser.py | 50 +++++++++++++++++++++++++++++++++++++ 4 files changed, 77 insertions(+), 24 deletions(-) create mode 100644 tests/async/test_browser.py diff --git a/playwright/async_api.py b/playwright/async_api.py index 490f41e53..b4ec806f5 100644 --- a/playwright/async_api.py +++ b/playwright/async_api.py @@ -5291,6 +5291,18 @@ def contexts(self) -> typing.List["BrowserContext"]: """ return mapping.from_impl_list(self._impl_obj.contexts) + @property + def version(self) -> str: + """Browser.version + + Returns the browser version. + + Returns + ------- + str + """ + return mapping.from_maybe_impl(self._impl_obj.version) + def isConnected(self) -> bool: """Browser.isConnected @@ -5484,17 +5496,6 @@ async def close(self) -> NoneType: """ return mapping.from_maybe_impl(await self._impl_obj.close()) - async def version(self) -> str: - """Browser.version - - Returns the browser version. - - Returns - ------- - str - """ - return mapping.from_maybe_impl(await self._impl_obj.version()) - mapping.register(BrowserImpl, Browser) diff --git a/playwright/browser.py b/playwright/browser.py index 72261bbf5..edd3f7b10 100644 --- a/playwright/browser.py +++ b/playwright/browser.py @@ -129,5 +129,6 @@ async def close(self) -> None: self._is_closed_or_closing = True await self._channel.send("close") - async def version(self) -> str: - return await self._channel.send("version") + @property + def version(self) -> str: + return self._initializer["version"] diff --git a/playwright/sync_api.py b/playwright/sync_api.py index 8ea629752..b8fc47675 100644 --- a/playwright/sync_api.py +++ b/playwright/sync_api.py @@ -5519,6 +5519,18 @@ def contexts(self) -> typing.List["BrowserContext"]: """ return mapping.from_impl_list(self._impl_obj.contexts) + @property + def version(self) -> str: + """Browser.version + + Returns the browser version. + + Returns + ------- + str + """ + return mapping.from_maybe_impl(self._impl_obj.version) + def isConnected(self) -> bool: """Browser.isConnected @@ -5716,17 +5728,6 @@ def close(self) -> NoneType: """ return mapping.from_maybe_impl(self._sync(self._impl_obj.close())) - def version(self) -> str: - """Browser.version - - Returns the browser version. - - Returns - ------- - str - """ - return mapping.from_maybe_impl(self._sync(self._impl_obj.version())) - mapping.register(BrowserImpl, Browser) diff --git a/tests/async/test_browser.py b/tests/async/test_browser.py new file mode 100644 index 000000000..167a09f20 --- /dev/null +++ b/tests/async/test_browser.py @@ -0,0 +1,50 @@ +# 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 re + +import pytest + +from playwright import Error +from playwright.async_api import Browser + + +async def test_should_create_new_page(browser): + page1 = await browser.newPage() + assert len(browser.contexts) == 1 + + page2 = await browser.newPage() + assert len(browser.contexts) == 2 + + await page1.close() + assert len(browser.contexts) == 1 + + await page2.close() + assert len(browser.contexts) == 0 + + +async def test_should_throw_upon_second_create_new_page(browser): + page = await browser.newPage() + with pytest.raises(Error) as exc: + await page.context.newPage() + await page.close() + assert "Please use browser.newContext()" in exc.value.message + + +async def test_version_should_work(browser: Browser, is_chromium): + version = browser.version + if is_chromium: + assert re.match(r"^\d+\.\d+\.\d+\.\d+$", version) + else: + assert re.match(r"^\d+\.\d+$", version)