2020-09-30 Announcing Playwright for Python: Reliable end-to-end testing for the web ¶
Automated end-to-end tests are a powerful tool for your team to ship faster and with more confidence .
End-to-end tests automate UI interactions and can validate the functionality of your applications.
To this end, we are announcing Playwright for Python in preview today.
Playwright enables developers and testers to write reliable end-to-end tests in Python. Get started by installing Playwright from PyPI.
With the Playwright API, you can author end-to-end tests that run on all modern web browsers. Playwright delivers automation that is faster, more reliable and more capable than existing testing tools.
Automated end-to-end tests have become more important than ever before. Teams are shipping faster and building apps that run on a growing set of devices. This increase in velocity and web targets puts immense pressure on the testing process, and automation is critical.
Today, we’re bringing the same capabilities to Python .
How is Playwright different ? ¶
1. Playwright delivers reliable, timeout-free automation ¶
Modern web apps are rich and responsive, issuing network requests and DOM changes based on user interactions.
This asynchronous behavior makes modern apps harder to predictably automate.
Traditional automated tests rely on sleep timeouts to manage this complexity, but timeouts often lead to unpredictable failures.
Playwright automatically waits for the UI to be ready, which ensures tests are reliable to execute and simpler to author.
Under the hood, Playwright uses an event-driven architecture that can listen to precise browser events like DOM changes, network requests and page navigations.
2. Playwright is built for the modern web ¶
The web platform is continuously evolving and adding newer capabilities every year.
Playwright is built to automate newer web features, including emulation of mobile viewports , geolocation and web permissions.
Playwright scripts can even intercept and modify network activity and automate scenarios across multiple pages.
3. Playwright works on all modern browsers ¶
With Playwright, you can author automated tests for all modern browser engines: Chromium (for Google Chrome and the new Microsoft Edge), WebKit (for Apple Safari) and Mozilla Firefox.
WebKit is supported on all platforms, which enables you to test rendering on Safari, even on Windows and Linux machines.
Use Playwright with pytest ¶
With our pytest plugin for Playwright, you can get started easily with end-to-end testing. To install Playwright, the plugin, and the browsers to test on, run:
pip install playwright pytest-playwright python -m playwright install
This plugin configures pytest fixtures that provide building blocks you need for end-to-end browser testing.
For example, the page fixture provides a new web page to run a test. For a complete list of fixtures, see plugin docs .
from playwright.sync_api import Page def test_example_is_working(page: Page): page.goto('https://example.com') assert page.innerText('h1') == 'Example Domain'
Use Playwright with Django ¶
You can use Playwright to test views in Django web apps.
To install Playwright, and the browsers to test on, run:
pip install playwright python –m playwright install
Playwright integrates with the built-in testing tools in Django.
Specifically, you can use the LiveServerTestCase class to launch a live Django server and run browser tests against it.
1 from django.contrib.staticfiles.testing import StaticLiveServerTestCase 2 from playwright import sync_playwright 3 4 class MyViewTests(StaticLiveServerTestCase): 5 @classmethod 6 def setUpClass(cls): 7 super().setUpClass() 8 cls.playwright = sync_playwright().start() 9 cls.browser = cls.playwright.chromium.launch() 10 11 @classmethod 12 def tearDownClass(cls): 13 cls.browser.close() 14 cls.playwright.stop() 15 super().tearDownClass() 16 17 def test_login(self): 18 page = self.browser.newPage() 19 page.goto('%s%s' % (self.live_server_url, '/login/')) 20 page.fill('#username', 'myuser') 21 page.fill('#password', 'secret') 22 page.click('text=Log in') 23 assert page.url == '%s%s' % (self.live_server_url, '/profile/') 24 page.close()
See other examples ¶
Deploy Playwright tests to CI/CD ¶
Running end-to-end tests in your CI/CD pipelines helps catch issues early.
Playwright for Python is built in the open on GitHub, and we are eager to learn more on how Playwright works for you.
Feel free to share feedback or feature requests on GitHub issues or join the Playwright Slack community to connect with other users.