WebTesting: Get web element's value

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:

  1. 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.
  2. 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.
  3. Type of Value to Retrieve: This specifies what kind of value you want to retrieve from the element. Options include:
    1. InnerText: Gets the text between the start and end tags of the element.
    2. InnerHTML: Retrieves the HTML content within the element.
    3. Value: Used to get the value attribute of input, select, and textarea.
    4. TextContent: Returns the text content of a node and its descendants.
    5. Checked: Returns the checked state of elements like checkboxes or radio buttons.
    6. IsEnabled: Checks if the element is enabled or not.
  4. Store it in the Variable: This is where you define the variable where you want to store the value that is retrieved.
    DN-2282.png

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".

  1. Navigate To the URL "https://www.loadgen.cloud/".
  2. Wait for the Page to be ready (NetworkIdle).
  3. Repeat 3 times.
  4. Wait for 'GetByRole(Link, where { Name = "Change"})' (timeout: 10, initial wait: 0).
  5. If Locator is not found, repeat. Once Locator is found, Left-click at "LastValidatedLocator".
  6. Wait for the Page to be ready (Load).
  7. Wait for 'GetByLocator(div). Filter({ HasTextRegex = ^Switch to tenant$}). GetByLocator(div label span)' (timeout: 10, initial wait: 0).
  8. If Locator is not found, repeat. Once Locator is found, Left click on "LastValidatedLocator".
  9. Wait for the Page to be ready (Load).
  10. Send Text to control LastValidatedLocator, "LoadGen".
  11. Get the 'Value' of the locator GetByPlaceholder(Enter tenancy name ... ) and store it in the variable LoadGenTenantName.
  12. Is [LoadGenTenantName] equal to 'LoadGen'?
  13. If No, Exit block as a failure.
  14. If Yes, Wait for 'GetByRole(Button, where { Name = ". Switch to the tenant"})' (timeout: 10, initial wait: 0).
  15. If Locator is not found, repeat. Once Locator is found, Left click on "LastValidatedLocator".
    DN-2279.png

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.

DN-2281.png

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.

Was this article helpful?
0 out of 0 found this helpful