The ability to retrieve a web element's value during automated web testing can provide invaluable insight into the current state of a webpage. This ability to collect values from various elements on a webpage, such as input fields, checkboxes, and more, is critical in checking the validity of test cases. One such function that allows us to do this is the Get web element's value function.
How It Works
The Get web element's value function allows you to select a particular element from a webpage and retrieve its value. The process involves defining a selector or a locator to identify the specific element on the webpage. Following this, the type of value that needs to be retrieved is specified, and the result is stored in a predefined variable for further use.
Here are the steps to use this function:
- Retrieve Selector's Value: This is where you define what value you want to retrieve. It can be the value of a specific selector that you input.
- Locator(s): This is where you specify the locator. A locator is a command that tells the function where to find the element on the webpage you want to interact with.
- Type of Value to Retrieve: This specifies what kind of value you want to retrieve from the element. Options include:
- InnerText: Gets the text between the start and end tags of the element.
- InnerHTML: Retrieves the HTML content within the element.
- Value: Used to get the value attribute of input, select, and textarea.
- TextContent: Returns the text content of a node and its descendants.
- Checked: Returns the checked state of elements like checkboxes or radio buttons.
- IsEnabled: Checks if the element is enabled or not.
- Store it in the Variable: This is where you define the variable where you want to store the value that is retrieved.
An Example Scenario
To better understand the functionality, consider the following scenario where the function is used to check whether the value of an input field equals "LoadGen".
- Navigate To the URL "https://www.loadgen.cloud/".
- Wait for the Page to be ready (NetworkIdle).
- Repeat 3 times.
- Wait for 'GetByRole(Link, where { Name = "Change"})' (timeout: 10, initial wait: 0).
- If Locator is not found, repeat. Once Locator is found, Left-click at "LastValidatedLocator".
- Wait for the Page to be ready (Load).
- Wait for 'GetByLocator(div). Filter({ HasTextRegex = ^Switch to tenant$}). GetByLocator(div label span)' (timeout: 10, initial wait: 0).
- If Locator is not found, repeat. Once Locator is found, Left click on "LastValidatedLocator".
- Wait for the Page to be ready (Load).
- Send Text to control LastValidatedLocator, "LoadGen".
- Get the 'Value' of the locator GetByPlaceholder(Enter tenancy name ... ) and store it in the variable LoadGenTenantName.
- Is [LoadGenTenantName] equal to 'LoadGen'?
- If No, Exit block as a failure.
- If Yes, Wait for 'GetByRole(Button, where { Name = ". Switch to the tenant"})' (timeout: 10, initial wait: 0).
- If Locator is not found, repeat. Once Locator is found, Left click on "LastValidatedLocator".
In this scenario, the 'Value' of the input field (found using the placeholder text 'Enter tenancy name ...') is retrieved and stored in a variable called LoadGenTenantName. Then, this variable is checked to see if it equals 'LoadGen'. If it does, the function proceeds; if not, it exits the block as a failure.
Overall, the Get web element's value function is a versatile tool in the realm of automated web testing, allowing for a great deal of precision and flexibility in retrieving and verifying data from web elements.