0

Is there a way to pass arguments into a playwright test in python? I want to be able to check a certain string appears on the browser screen, but this string is variable.

e.g. if i call the test script with "pytest testscript.py --expectedString test123"

where abouts would the argument go in the test script?

from playwright.sync_api import sync_playwright, Page, expect
import re

def test():
    playwright = sync_playwright().start()
    chromium = playwright.chromium # or "firefox" or "webkit".
    browser = chromium.launch(headless=False) # TODO remove this later
    page = browser.new_page()

    page.goto("https://playwright.dev/")

    # Expect a title "to contain" a substring.
    expect(page).to_have_title(re.compile("Playwright"))

   # create a locator
   get_started = page.get_by_role("link", name="Get started")

   # Expect an attribute "to be strictly equal" to the value.
   expect(get_started).to_have_attribute("href", "/docs/intro")

   # Click the get started link.
   get_started.click()

   # Expects the URL to contain intro.
   expect(page).to_have_url(re.compile(".*intro"))
  • Passing a parameter like this? https://stackoverflow.com/questions/40880259/how-to-pass-arguments-in-pytest-by-command-line – candre Aug 21 '23 at 10:04
  • You should not pass as parameter a string to verify on the screen. This is not the expected usage of the parameters. – Vishal Aggarwal Aug 21 '23 at 13:58

2 Answers2

0

Just run it with env vars.

EXPECTED_STRING=1 pytest testscript.py
import os
str = os.getenv('EXPECTED_STRING')
// some code
expect(page).to_have_url(re.compile(str))
unickq
  • 1,497
  • 12
  • 18
0

Use regex , no need to pass parameter , if its just random number added on an fixed string.

/test\d{3}/
Vishal Aggarwal
  • 1,929
  • 1
  • 13
  • 23