Skip to main content

Advanced Code Block

DevAssure provides the capability to execute custom JavaScript code during your test runs.

This feature allows you to perform complex validations and seamlessly integrate custom code logic tailored to your test cases.

  • Navigate to the Editor view of the test case and hover over the test step where you wish to insert the code block. Then, select Add Advanced Code Block or choose Add Code Block to append code after the preceding test step.
Icon
  • To access DevAssure Test Actions within the code block, start typing "::" . This will trigger a list of suggestions for available functions to help you complete the code.

  • The test data and constants can also be referenced inside the code block.

Icon
  • Example 1 : Below illustrates a simple JavaScript code block demonstrating how to clear the quantity of items, update the quantity with test data values, and verify the functionality of adding items to a cart on an e-commerce website.
codeblcok
  • Example 2 : Additionally, presented here is another example of Java script code block showcasing the generation of random email IDs, which can be utilized in login test cases.
codeblockemail
  • Example 3 - Conditional Logic in CodeBlock : This code block is designed to iterate through a list of flights, check if a "lowest fare" tag is present for each flight, and conditionally perform actions based on that presence. If the lowest fare tag is found, it logs the flight price and then selects that particular flight.

  • Prerequisites

    • A variable named Noofflights which holds the total number of flights to iterate through. This variable must be defined and accessible before this code block executes.
    • Defined page objects/locators for:
      • FlightSearch-SearchFlightTimings_Prices_AkasaAir: The parent container or section for flight timings and prices.
      • Flightprice: The element containing the price for an individual flight.
      • Flighttag_low_price: The element representing the "lowest fare" tag.
      • FlightSelect: The element used to select an individual flight.
conditionalogiccodeblock
# verify if the lowest fare tag is present
for (let i=0; i<Noofflights; i++) {
flightPrice = Get text of web > FlightSearch-SearchFlightTimings_Prices_AkasaAir > Flightprice ( count: ${i} )

isPresent_0 = Is element web > FlightSearch-SearchFlightTimings_Prices_AkasaAir > Flighttag_low_price ( count: ${i} ) present within 1000 milliseconds

if (isPresent_0) {
Print log flightPrice

Click on web > FlightSearch-SearchFlightTimings_Prices_AkasaAir > FlightSelect ( count: ${i} )
}
}
Print log "Lowest Fare Tag is not present"

Referencing Variables within Code Blocks

To access variables that have been defined or modified within a code block and use them in subsequent DevAssure test actions outside that block, you need to prefix the variable name with ::. This mechanism signals to DevAssure that you're referring to a variable that exists in the test case's scope, potentially originating from a previous code block.

Consider the following example:

conditionalogiccodeblock

In the image above, within the first "Code Block" test step, a JavaScript variable modifiedUrl is created and assigned a value. To use this modifiedUrl variable in the subsequent "Print log" test action (which is outside the code block), we reference it as ::modifiedUrl. This allows the value of modifiedUrl to be passed from the JavaScript environment of the code block to the DevAssure test action.

When you want to invoke variable values or evaluate expressions directly within the JavaScript code block, you should use the ${} syntax. This is standard JavaScript template literal syntax and is used for string interpolation.

For example, to display the iteration number within a loop in the log, you would use ${i+1}:

for (let i = 0; i < 10; i++) {
Print log ${i+1}
}
  • Example 4 : The attached code block illustrates iterating through lists of data and using those values to interact with web elements.
conditionalogiccodeblock

${name} and ${type}: This is the essential syntax for embedding the values of JavaScript variables (like name and type) directly into DevAssure test actions when those actions are called from within the JavaScript code block. This allows your JavaScript logic to dynamically control the input and selection processes in your web application.

note
  • For passing dynamic values to elements referenced in code block, please refer to the documentation here.