PyTest & Puppeteer


PyTest and Pyppeteer are a great combination to do Headless Testing with Python.
Together with pytest-asyncio, you can write asynchronous PyTest tests which connect to the HeadlessTesting browser grid. Run tests in parallel on our browsers with PyTest, Pyppeteer and pytest-asyncio.


To get started, please install these packages:

pip install pytest-asyncio pyppeteer

Now you're ready to use Python with Puppeteer. There's some more information about Pypeteer on their documentation pages.

Example

To get started, please see this simple example below.
This will start a new Chrome browser in the HeadlessTesting grid, and allow PyTest to control the browser via Puppteer.


import pytest
import pyppeteer

@pytest.mark.asyncio
async def test_title():
  try:
    browser = await pyppeteer.connect(browserWSEndpoint='wss://chrome.headlesstesting.com/?token=[YOUR-TOKEN]')

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

This example will open a Chrome browser, navigate to our homepage and verify the document's title.


We use @pytest.mark.asyncio to let PyTest know that we're using await.
Please see pytest-asyncio for more information about event loops, awaits and fixtures.

Parallel Testing with Pytest


One of the great advantages of our service is that you can run multiple tests simultaneously (in parallel).
This drastically shortens the total duration of your test suite, as multiple tests will run concurrently.


We recommend using pytest-parallel.