{"id":8037,"date":"2020-09-30T08:26:25","date_gmt":"2020-09-30T16:26:25","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/python\/?p=8037"},"modified":"2020-10-07T13:39:17","modified_gmt":"2020-10-07T21:39:17","slug":"announcing-playwright-for-python-reliable-end-to-end-testing-for-the-web","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/python\/announcing-playwright-for-python-reliable-end-to-end-testing-for-the-web\/","title":{"rendered":"Announcing Playwright for Python: Reliable end-to-end testing for the web"},"content":{"rendered":"<p>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 <a href=\"https:\/\/github.com\/microsoft\/playwright-python\">Playwright for Python<\/a> in preview today. Playwright enables developers and testers to write reliable end-to-end tests in Python. Get started by installing <a href=\"https:\/\/pypi.org\/project\/playwright\/\">Playwright from PyPI<\/a>.<\/p>\n<p><img decoding=\"async\" class=\"wp-image-8038 size-full\" src=\"https:\/\/devblogs.microsoft.com\/python\/wp-content\/uploads\/sites\/12\/2020\/09\/playwright-python-repl.gif\" alt=\"Playwright in Python\" width=\"960\" height=\"638\" \/><\/p>\n<p style=\"text-align: center;\"><small>Click the image to see Playwright in action!<\/small><\/p>\n<p>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.<\/p>\n<p>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.<\/p>\n<p>While automation is important, end-to-end tests are prone to being slow and flaky. To fix this, we released <a href=\"https:\/\/github.com\/microsoft\/playwright\">Playwright in JavaScript<\/a> earlier this year and have enabled thousands of developers and testers to be successful at end-to-end testing. Today, we\u2019re bringing the same capabilities to Python.<\/p>\n<p>&nbsp;<\/p>\n<h2>How is Playwright different?<\/h2>\n<h4>1. Playwright delivers reliable, timeout-free automation.<\/h4>\n<p>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.<\/p>\n<p>Playwright <a href=\"https:\/\/playwright.dev\/#path=docs%2Factionability.md&amp;q=\" target=\"_blank\" rel=\"noopener noreferrer\">automatically waits for the UI<\/a> 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.<\/p>\n<h4>2. Playwright is built for the modern web.<\/h4>\n<p>The web platform is\u202f<a href=\"https:\/\/whatwebcando.today\/\" target=\"_blank\" rel=\"noopener noreferrer\">continuously evolving<\/a> and adding newer capabilities every year. Playwright is built to automate newer web features, including\u202f<a href=\"https:\/\/playwright.dev\/#path=docs%2Femulation.md&amp;q=\" target=\"_blank\" rel=\"noopener noreferrer\">emulation\u202fof mobile viewports<\/a>, geolocation and web permissions. Playwright scripts can even\u202f<a href=\"https:\/\/playwright.dev\/#path=docs%2Fnetwork.md&amp;q=\" target=\"_blank\" rel=\"noopener noreferrer\">intercept and modify network\u202factivity<\/a> and automate scenarios across multiple pages.<\/p>\n<h4>3. Playwright works on all modern browsers.<\/h4>\n<p>With Playwright, you can author automated tests for all modern browser engines: <a href=\"https:\/\/www.chromium.org\/\" target=\"_blank\" rel=\"noopener noreferrer\">Chromium<\/a> (for Google Chrome and the new Microsoft Edge),\u202f<a href=\"https:\/\/webkit.org\/\" target=\"_blank\" rel=\"noopener noreferrer\">WebKit<\/a>\u202f(for Apple Safari) and <a href=\"https:\/\/www.mozilla.org\/en-CA\/firefox\/\" target=\"_blank\" rel=\"noopener noreferrer\">Mozilla Firefox<\/a>. WebKit is supported on all platforms, which enables you to test rendering on Safari, even on Windows and Linux machines.<\/p>\n<p>&nbsp;<\/p>\n<h2>Use Playwright with pytest<\/h2>\n<p>With our <a href=\"https:\/\/github.com\/microsoft\/playwright-pytest\" target=\"_blank\" rel=\"noopener noreferrer\">pytest plugin for Playwright<\/a>, you can get started easily with end-to-end testing. To install Playwright, the plugin, and the browsers to test on, run:<\/p>\n<pre class=\"prettyprint\">pip install playwright pytest-playwright \r\npython -m playwright install\r\n<\/pre>\n<p>This plugin configures <a href=\"https:\/\/docs.pytest.org\/en\/stable\/fixture.html\" target=\"_blank\" rel=\"noopener noreferrer\">pytest fixtures<\/a> 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,\u202f<a href=\"https:\/\/github.com\/microsoft\/playwright-pytest#fixtures\" target=\"_blank\" rel=\"noopener noreferrer\">see plugin docs<\/a>.<\/p>\n<pre class=\"prettyprint\">from playwright.sync_api import Page \r\n\r\ndef test_example_is_working(page: Page): \r\n    page.goto('https:\/\/example.com') \r\n    assert page.innerText('h1') == 'Example Domain'\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h2>Use Playwright with Django<\/h2>\n<p>You can use Playwright to test views in Django web apps. To install Playwright, and the browsers to test on, run:<\/p>\n<pre class=\"prettyprint\">pip install playwright \r\npython \u2013m playwright install \r\n<\/pre>\n<p>Playwright integrates with the built-in testing tools in Django. Specifically, you can use the <a href=\"https:\/\/docs.djangoproject.com\/en\/3.1\/topics\/testing\/tools\/#liveservertestcase\" target=\"_blank\" rel=\"noopener noreferrer\">LiveServerTestCase\u202fclass<\/a> to launch a live Django server and run browser tests against it.<\/p>\n<pre class=\"prettyprint\">from django.contrib.staticfiles.testing import StaticLiveServerTestCase \r\nfrom playwright import sync_playwright \r\n\r\nclass MyViewTests(StaticLiveServerTestCase): \r\n    @classmethod \r\n    def setUpClass(cls): \r\n        super().setUpClass() \r\n        cls.playwright = sync_playwright().start() \r\n        cls.browser = cls.playwright.chromium.launch() \r\n\r\n    @classmethod \r\n    def tearDownClass(cls): \r\n        cls.browser.close() \r\n        cls.playwright.stop() \r\n        super().tearDownClass() \r\n\r\n    def test_login(self): \r\n        page = self.browser.newPage() \r\n        page.goto('%s%s' % (self.live_server_url, '\/login\/')) \r\n        page.fill('#username', 'myuser') \r\n        page.fill('#password', 'secret') \r\n        page.click('text=Log in') \r\n        assert page.url == '%s%s' % (self.live_server_url, '\/profile\/') \r\n        page.close()\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h2>Deploy Playwright tests to CI\/CD<\/h2>\n<p>Running end-to-end tests in your CI\/CD pipelines helps catch issues early. You can deploy Playwright tests to CI\/CD with the <a href=\"https:\/\/github.com\/marketplace\/actions\/run-playwright-tests\" target=\"_blank\" rel=\"noopener noreferrer\">Playwright GitHub Action<\/a> or with tools for <a href=\"https:\/\/playwright.dev\/#path=docs%2Fci.md&amp;q=\" target=\"_blank\" rel=\"noopener noreferrer\">other CI\/CD providers<\/a>.<\/p>\n<hr \/>\n<p>Playwright for Python is <a href=\"https:\/\/github.com\/microsoft\/playwright-python\" target=\"_blank\" rel=\"noopener noreferrer\">built in the open on GitHub<\/a>, and we are eager to learn more on how Playwright works for you. Feel free to share feedback or feature requests on <a href=\"https:\/\/github.com\/microsoft\/playwright-python\/issues\" target=\"_blank\" rel=\"noopener noreferrer\">GitHub issues<\/a> or <a href=\"https:\/\/join.slack.com\/t\/playwright\/shared_invite\/enQtOTEyMTUxMzgxMjIwLThjMDUxZmIyNTRiMTJjNjIyMzdmZDA3MTQxZWUwZTFjZjQwNGYxZGM5MzRmNzZlMWI5ZWUyOTkzMjE5Njg1NDg\" target=\"_blank\" rel=\"noopener noreferrer\">join the Playwright Slack community<\/a> to connect with other users.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 apps. To this end, we are announcing Playwright for Python in preview today. Playwright enables developers and testers to write reliable end-to-end tests.<\/p>\n","protected":false},"author":41134,"featured_media":8038,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-8037","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"acf":[],"blog_post_summary":"<p>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 apps. To this end, we are announcing Playwright for Python in preview today. Playwright enables developers and testers to write reliable end-to-end tests.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/python\/wp-json\/wp\/v2\/posts\/8037","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/python\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/python\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/python\/wp-json\/wp\/v2\/users\/41134"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/python\/wp-json\/wp\/v2\/comments?post=8037"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/python\/wp-json\/wp\/v2\/posts\/8037\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/python\/wp-json\/wp\/v2\/media\/8038"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/python\/wp-json\/wp\/v2\/media?parent=8037"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/python\/wp-json\/wp\/v2\/categories?post=8037"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/python\/wp-json\/wp\/v2\/tags?post=8037"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}