Variable
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 a variable in TESTLUM ?
Out of the <web>
You can specify the type and value of variable, that you want to use. There are 6 types of variables:
- sql
- expression
- path
- file
- generate
- constant
Variable with sql query example:
You can create variable from the result of SQL query.
<var comment="Create variable with sql query" name="PRODUCT_ID">
<sql dbType="MYSQL" alias="SHOPIZER">
<query>
SELECT PRODUCT_ID FROM PRODUCT
WHERE SKU = 'TB12345'
</query>
</sql>
</var>
Variables with constant and expression example:
You can create both constant variables and variables with expressions, as shown in the example above.
<var comment="Create variable with constant value" name="email">
<constant value="'test@gmail.com'"/>
</var>
<var comment="Create variable with expression" name="t">
<expression value="'{{email}}'.toString.charAt(0)"/>
</var>
Variable with generate example:
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>
Variable from path example:
You can create a variable with a value from the previous 'expected' file, using jpath.
<var comment="Create a variable for the value 'product_id Referring via 'jpath' to the previous expected_file to get the desired value" 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" from="jsonFromFile"/>
</var>
<var comment="Create var for url inside html from htmlFromExpectedVar variable" name="urlFromHtml">
<path value="//a[@href='https://example.com']/@href" from="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>
Variable from file example:
<var comment="Create variable from file" name="var">
<file fileName="text.txt"/>
</var>
Inside the <web>
There are 8 types of variables inside the <web>:
- sql
- expression
- path
- file
- dom
- url
- cookie
- element
Variable with DOM example:
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>
Variable wtih URL example:
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>
Variable with cookie example:
Also, you can fetch cookie and create variable with it as well.
<var comment="Create variable with cookie" name="cookie">
<cookie/>
</var>
Variable with element example:
If element is present on the page, then value of the variable will be 'true'. Otherwise, value will be 'false'.
With 'attribute' you can create a variable from an element attribute.
<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>
<var comment="Check that element isn't present on the page" name="openModaName">
<element>
<attribute name="innerHTML" locator="modal.openModalBtn"/>
</element>
</var>