Setting Parameters in Tests
Parameters are essential for creating flexible, reusable tests in Harness AI Test Automation. Whilst declaring parameters is straightforward, understanding how to set their values during test execution is equally important. This guide walks you through different methods for setting parameter values, from adding static constants upfront to using AI-powered extraction during test execution.
Why Set Parameters During Tests?
Setting parameters dynamically during test execution allows you to capture dynamic data generated by the application (like order IDs, timestamps, or calculated totals), chain test steps together by using data from one step as input for subsequent steps, build flexible workflows that adapt to different scenarios without hardcoding values, and improve test maintainability by updating parameter values in one place rather than throughout your test.
Ways to Set Parameters
There are several methods for setting parameter values in your tests. You can add a static parameter upfront with a constant value, manually assign values during test execution using dedicated steps, extract values directly from UI elements, or use AI to capture complex data through natural language prompts. Let's explore each method and understand when to use them.
Method 1: Add a Static Parameter
Sometimes you need to declare a parameter at the start of your test with a known, constant value. This is useful when you have data that remains consistent throughout the test execution, such as product names, user credentials, or configuration values.
You can add these parameters by clicking the Parameters & Config button at the top right corner of the test editor. From there, you can add a parameter with a name (like PRODUCT_1) and assign it a value (like Mystic Apparatus). This parameter is now available throughout your entire test and can be referenced using ${PRODUCT_1} in any test step.
Use static parameters when you know you'll need to store data during the test but want to set it upfront as a constant. This is common for test data that doesn't change during execution, like product names, account types, or configuration values that you want to easily modify without changing the test steps themselves.
Method 2: Set Parameter Using Add Step
This method allows you to manually assign a specific value to a parameter during test execution. It's particularly useful when you need to set a value at a specific point in your test flow, rather than at the beginning.
In your test workflow, you can click Add Step and select Set Parameter from the action menu. You then choose which parameter you want to set and enter the value you want to assign. For example, if you set PRODUCT_2 to Retro Technothing, the step will appear in your test as "Setting parameter PRODUCT_2 to Retro Technothing". Once set, the parameter is assigned that value and you can use ${PRODUCT_2} in any subsequent test steps throughout the rest of the test execution.
Method 3: Set Parameter from Target
This powerful method allows you to extract a value directly from a UI element and store it in a parameter. It's perfect for capturing dynamic data displayed on the screen, such as order totals, generated IDs, or status messages.
When you click Add Step in your test workflow, you can select Set Parameter from Target as an action. You then configure which parameter to store the value in (like TOTAL_COST), specify the target selector for the element containing the value (such as Tag="DIV"), and optionally define an extraction pattern if you need only part of the text.
For instance, if you want to capture the total cost displayed in a shopping cart, your step configuration would look like this:
Step: Click Tag="DIV" 2
Action: Setting parameter TOTAL_COST to from target Tag="DIV" Cart Total: $1431.27
When this step executes, the test clicks on the specified DIV element, extracts the text content "Cart Total: $1431.27".
Use Set Parameter from Target when you need to capture visible data from the UI. This is ideal for order totals, prices, calculated values, generated IDs, reference numbers, status messages, confirmation text, or any dynamic content displayed on the page.
Method 4: Set Parameter Using AI
The most advanced method leverages AI to understand your intent and extract complex data from the page. You simply describe what you want to capture in natural language, and the AI handles the extraction and storage.
Harness AI Test Automation includes an Extract Data to Parameter feature that allows you to use natural language prompts to capture data. When you add a Prompt (P) step to your test, you can write a natural language instruction describing what data to capture, and the AI will analyse the page, extract the relevant data, and store it in the specified parameter.
For example, if you want to capture all the details from a shopping cart (product names, quantities, prices, etc.), you can provide a prompt like "Create a parameter of 'DETAILS' and store every detail from the cart". The AI analyses the shopping cart on the page, identifies all relevant information (products, quantities, prices, subtotals), structures this data, and stores it in the DETAILS parameter. The parameter now contains comprehensive cart information that you can use in subsequent steps throughout your test.
Learn more about AI-driven parameter extraction to understand how to create parameters using natural language prompts.
Use AI to set parameters when you need to capture complex, structured data from the page, when the data is spread across multiple elements, when you want to extract information that requires understanding context, or when manual extraction would be time-consuming or brittle.
Practical Example: E-Commerce Test Workflow
Let's put all four methods together in a complete e-commerce test scenario. This example demonstrates when and how to use each method effectively.
Complete Test Steps
1. Add a blank parameter PRODUCT_1 from Parameters button on the top
2. Setting parameter PRODUCT_2 to Retro Technothing
3. Add ${PRODUCT_2} to the cart
4. Add two times the ${PRODUCT_1} to the cart
5. Click Tag="DIV" 2
Setting parameter TOTAL_COST to from target Tag="DIV" Cart Total: $1431.27
6. Create a parameter of "DETAILS" and store every detail from the cart
7. Navigate to https://testpages.eviltester.com/apps/html-table-generator/
Click Tag="SUMMARY" Table Data
8. Navigate to https://testpages.eviltester.com/apps/note-taker/
9. Write "${DETAILS}" in Id="note-details-input"
10. Write "${PRODUCT_1}" in Id="note-title-input"
11. Append "${PRODUCT_2}" in Id="note-title-input"
The Append action in Step 11 adds text to an existing input field without clearing it first. This is useful when you want to combine multiple parameter values in a single field.