Run the sample Playwright Python test

Step 1: Setup environment

Requirements

  • Python 3
  • Pytest framework
  • Playwright framework
  • Access key
    • You can generate an Access key using the following instruction – Access key
  • Billing unit
    • The billing unit is your team’s unique identifier, or just the word “personal” in the case of an individual account.
      More information can be found here.

Step 2: Run sample test

After setup up the environment, you can run your first web test

import base64
import uuid

import pytest

from playwright.sync_api import sync_playwright, Page, expect

BILLING_UNIT = '<TEAM_CODE>'
ACCESS_TOKEN = 'mobitru_ak_...'
MOBITRU_HOST = 'browserhub-us.mobitru.com'
BROWSER_NAME = 'chrome'
PW_VERSION = '1.55.0'


@pytest.fixture(scope="function")
def mobitru_page(request):
    with sync_playwright() as playwright:
        session_name = str(uuid.uuid4())
        chromium = playwright.chromium
        ws_endpoint = f"wss://{BILLING_UNIT}:{ACCESS_TOKEN}@{MOBITRU_HOST}/playwright/{BROWSER_NAME}/playwright-{PW_VERSION}?enableVideo=true&sessionName={session_name}"
        browser = chromium.connect(ws_endpoint=ws_endpoint)
        page = browser.new_page()
        page.goto("https://www.mobitru.com/")

        http_credentials = f"{BILLING_UNIT}:{ACCESS_TOKEN}"
        mobitru_api_access_token = base64.b64encode(http_credentials.encode()).decode()

        session_info = page.request.get( url=f"https://{MOBITRU_HOST}/playwright/session/{session_name}",
            headers={
                'Authorization': f"Basic {mobitru_api_access_token}"
            }
        )
        json_response = session_info.json()
        session_id = json_response["sessionId"]

        yield page

        browser.close()


def test_contact_us_button(mobitru_page: Page):
    # Expect contact us button to ve visible
    expect(mobitru_page.locator("button[class*='request-demo']")).to_be_visible()

Scroll to Top