With Python Playwright moving from pre to release with version 1.8, the API has changed since the last 0.170.0 pre-release version:
Snake case notation for methods and arguments:
# oldbrowser.newPage()
# newbrowser.new_page()
Import has changed to include sync vs async mode explicitly:
# oldfromplaywrightimportsync_playwright
# newfromplaywright.sync_apiimportsync_playwright
This script will migrate pre-release test cases to the new API. It uses Bowler (https://pybowler.io/) to do the heavy lifting for migration. The script handles automatic conversion of camelCase method names to snake_case. See methods list for method names that can be converted. https://github.com/jmmjsolutions/playwright-python-migrate/blob/master/playwright_migrate/main.py
It will also fix sync_playwright imports of the following format:
fromplaywrightimportsync_playwrightThe Playwright migration utility can be installed from GitHub using pip
pip install git+https://github.com/jmmjsolutions/playwright-python-migrate.git
... or download the source distribution from GitHub, unarchive, and run
python setup.py install
The Playwright migration utility is a Python program that reads the test case source code and applies a series of fixers to transform the test case to valid Playwright 1.8.0 API calls.
So if we have a simple test case source file: test_example.py
fromplaywrightimportsync_playwrightwithsync_playwright() aspw:
browser=pw.webkit.launch(headless=False)
context=browser.newContext()
page=context.newPage()
page.goto("https://playwright.dev/python/docs/intro/")
assertpage.url=="https://playwright.dev/python/docs/intro/"browser.close()We can run the migration utility in a non destructive mode to see what changes would be made.
playwright-migrate test_example.py
This will produce the following output on the console:
--- test_example.py+++ test_example.py@@ -1,9 +1,9 @@-from playwright import sync_playwright+from playwright.sync_api import sync_playwright
with sync_playwright() as pw:
browser = pw.webkit.launch(headless=False)
- context = browser.newContext()- page = context.newPage()+ context = browser.new_context()+ page = context.new_page()
page.goto("https://playwright.dev/python/docs/intro/")
assert page.url == "https://playwright.dev/python/docs/intro/"To apply the changes to the actual source code, run the migration utility with the --write argument. The utility creates a backup e.g. test_example.py.bak before overwriting the original file with the changes.
playwright-migrate --write test_example.py
The test_example.py file will now look like:
fromplaywright.sync_apiimportsync_playwrightwithsync_playwright() aspw:
browser=pw.webkit.launch(headless=False)
context=browser.new_context()
page=context.new_page()
page.goto("https://playwright.dev/python/docs/intro/")
assertpage.url=="https://playwright.dev/python/docs/intro/"browser.close()