Skip to main content

Custom Code Samples

The following code samples can be useful in various scenarios.

Get the current Date and Time

The following step code will get the current date and time (in the UTC timezone), format it into a string, and save the string into a temporary variable named today:

variables.today = datetime.datetime.now(tz=datetime.UTC).strftime("%B %d, %Y")

In later steps of the same test, you can reference variable today in descriptions using curly braces, for example:

Assertion: the date displayed is {{ today }}

Additional notes:

  • You can change the format used by strftime to change the actual text saved in the variable. See python's official docs for full details, but here are some examples:
    • %B %d, %Y: July 31, 2025
    • %Y-%m-%d: 2025-07-31
    • %Y-%m-%d at %I:%M %p: 2025-07-31 at 08:21 PM
    • %Y-%m-%dT%H:%M:%S%z: 2025-07-31T20:21:45+0000
  • You can adjust the timezone used to determine the date and time by changing the value of the tz parameter:
    • Timezone("America/Denver")
    • Timezone("US/Eastern")
    • datetime.UTC (or Timezone("UTC"))
    • etc.

See also:

Use a Unique Email Address for each run

Some tests may need to use a unique email address for each test run.

Some email providers (including Gmail) allow for ad-hoc email address aliases by adding a + infix. For example if your email address is my_email@gmail.com, you will also receive emails sent to my_email+123@gmail.com my_email+qualiti@gmail.com, and others. This feature allows us to create unique email addresses that all connect to the same user/inbox.

The following step code will create a unique email address that will be different for each execution:

timestamp = round(time.time())
letters = "".join(random.choices("abcdef", k=6))
variables.unique_email = f"my_email+{timestamp}{letters}@gmail.com"

In later steps of the same test, you can reference variable unique_email in descriptions using curly braces, for example:

Type {{ unique_email }} into the "Email" textbox

Additional notes:

  • Be sure to update the my_email and @gmail.com placeholders to reflect the actual email address and domain needed for your test
  • You can adjust the details of how the + suffix are generated using normal python coding techniques

See also:

Direct URL navigation

The following step code will navigate directly to a different URL within your environment, and check that the navigation returned a successful status code.

response = page.goto("/my-example/url/path?lang=eng")
if response is not None:
assert response.ok, f"bad response status: {response.status} {response.status_text}"

Additional notes:

  • Be sure to change the URL string to use the correct values needed for your test
  • The URL string given to page.goto will be used relative to the URL of the Environment that the test is currently using; this allows the same code to work even when run with different Environments
  • If you need to navigate to a certain URL that is not relative to the test's current Environment, you can still use the full URL instead of a relative URL, e.g. page.goto("https://www.google.com/")

See also: