< Back to Blog Overview

PyTest Automated Testing with Playwright

2020-08-13
PyTest Automated Testing with Playwright

Today we've added documentation on how to use PyTest and Playwright to run Automated Headless tests on our browser grid.


PyTest is a popular Python test framework, it allows you to write simple or complex tests. Below we'll show you how to write a simple PyTest test and run it on a headless browser instance on our cloud.

Installation

To get started, you should have Python installed. Next, we'll install PyTest and the playwright-python package with pip:

pip install playwright pytest
python -m playwright install

Once this is installed, you are ready to write your first test.


Please see the example below. This test will open a Chrome headless browser on the HeadlessTesting grid, navigate to our homepage and verify the title of the page.

import pytest
from playwright import sync_playwright

@pytest.fixture
def browser():
  pw_context = sync_playwright()
  pw = pw_context.__enter__()
  browser = getattr(pw, "chromium").connect(wsEndpoint='wss://chrome.headlesstesting.com?token=[YOUR-TOKEN]')
  browser._close = browser.close

  def _handle_close() -> None:
    browser._close()
    pw_context.__exit__(None, None, None)

  browser.close = _handle_close

  return browser

def test_title(browser):
  page = browser.newPage()
  page.goto('https://headlesstesting.com')
  assert page.title() == 'Headless Testing with Puppeteer and Playwright in the Cloud.'
  browser.close()

To find out more, please see our PyTest and Playwright documentation page.

HeadlessTesting Logo

Sign up for a Free Trial

Start Headless Testing instantly.

No credit card required!