Variables - TestlumFramework/Testlum GitHub Wiki

๐Ÿงฎ Variables in Testlum

A variable is a named or differently addressable area of memory that can be used to access data. In simple words, a variable is a data store. You can put any value here (for example, a number, a line, or another data type). Variables store certain data that can later be used in the program.

Variable is flexible:

  • it can store information
  • you can extract information from it, which will not affect the value of the variable itself
  • new data can be written into it

๐Ÿ› ๏ธ How to Create Variables

You can define variables outside and inside <web> blocks.

Types of Variables Outside <web>

  • sql: Result from SQL queries.
  • constant: Static value.
  • expression: Evaluate expressions.
  • generate: Random values (alphabetic, numeric, regex).
  • path: Extract from previous JSON or variable results.
  • file: Load content from files.

Example:

sql

You can create variable from the result of SQL query.

<var name="PRODUCT_ID">
    <sql dbType="MYSQL" alias="SHOPIZER">
        <query>SELECT PRODUCT_ID FROM PRODUCT WHERE SKU = 'TB12345'</query>
    </sql>
</var>

constant

You can create constant variables.

<var comment="Create variable with constant value" name="email">
    <constant value="'[email protected]'"/>
</var>

expression

You can create variables with expressions. Like in example to store only first letter from email.

<var comment="Create variable with expression" name="t">
    <expression value="'{{email}}'.toString.charAt(0)"/>
</var>

generate

You can create a variable with a specified length and value, using options like alphabetic, alphanumeric, numeric, or randomRegexp.

<var comment="Create a variable for the customer's name" name="customerName">
    <generate length="6">
        <alphabetic/>
    </generate>
</var>

path

You can create a variable from previous execution results (expected files or other variables) using xpath or jpath

<var comment="Create a variable for the value 'product_id Referring via 'jpath' to the previous expected_file" 
      name="PRODUCT_ID">
    <path value="$.[0].content.[0].PRODUCT_ID"/>
</var>

<http comment="Use the received variable in the 'http' request to get information about a specific product" 
      alias="SHOPIZER">
    <get endpoint="/api/v1/products/{{PRODUCT_ID}}">
        <response code="200" file="expected_4.json"/>
    </get>
</http>

You can also create a variable with a html from the previous expected file, using jpath and then create variable for URL inside html from this html.

<var comment="Create var from file" name="jsonFromFile">
    <file fileName="custom_data_4.json"/>
</var>

<var comment="Create var for html content from jsonFromFile variable" name="htmlFromExpectedVar">
    <path value="$.[0].content.[0].html" fromFile="jsonFromFile"/>
</var>

<var comment="Create var for url inside html from htmlFromExpectedVar variable" name="urlFromHtml">
    <path value="//a[@href='https://example.com']/@href" fromVar="htmlFromExpectedVar"/>
</var>

<assert comment="Assert that urlFromHtml variable equals to actual url from html content">
    <equal comment="Assert for check that two contents is equal">
        <content>{{urlFromHtml}}</content>
        <content>https://example.com</content>
    </equal>
</assert>

file

You can also create variable from file content.

<var comment="Create variable from file" name="var">
    <file fileName="text.txt"/>
</var>

๐ŸŒ Variables Inside <web>

Additional types supported inside <web>:

  • dom: Capture full page or element DOM.
  • url: Get the current page URL.
  • cookie: Retrieve browser cookies.
  • element: Check element presence or attribute.

dom

You can create variable with full DOM of the current page or with DOM of the specific element from the current page.

<var comment="Create variable with full DOM of the current page" name="fullDOM">
    <dom/>
</var>

<var comment="Create variable with dom of the specific element" name="dom">
    <dom xpath="//img[@class='img-fluid']"/>
</var>

url

You can fetch URL of the current page and create variable with it.

<var comment="Create variable with URL of the current page" name="url">
    <url/>
</var>

cookie

Also, you can fetch cookie and create variable with it as well.

<var comment="Create variable with cookie" name="cookie">
    <cookie/>
</var>

element

If element is present on the page, then value of the variable will be true. Otherwise, value will be false.

<var comment="Check that element is present on the page" name="elemIsPresent">
    <element>
        <present locatorId="webVar.banner"/> 
    </element>
</var>

<var comment="Check that element isn't present on the page" name="elemNotPresent">
    <element>
        <present locatorId="webVar.inputPassword"/>
    </element>
</var>

With attribute you can extract a value from an elementโ€™s attribute and store it as a variable.

This is useful when you want to assert or reuse dynamic values from the UI (e.g., button text, links, data-* attributes).

<var comment="Check that element isn't present on the page" name="openModaName">
    <element>
        <attribute name="innerHTML" locator="modal.openModalBtn"/>
    </element>
</var>

๐Ÿ’ก Tips on common attribute names:

  • innerHTML: Captures the HTML content inside an element (e.g., button text).
  • value: Often used for input fields (e.g., <input value="Test">).
  • href: For anchor links (e.g., <a href="/home">Home</a>).
  • src: For image or script sources.
  • data-*: Custom attributes like data-id, data-name โ€” helpful for dynamic data.
  • class: Can be used to check if a specific style or state (like active, error) is applied.

โœ… Use attributes to dynamically extract values for assertions, reuse in other steps, or debugging unexpected behavior.

โš ๏ธ **GitHub.com Fallback** โš ๏ธ