TestProject Forum

Passing different values in a Javascript code execution

Hello,

I wanted to know how could I reuse a Javascript code in different test cases in which a variable value differs in which test case the code is being executed?

For example, I have a step in a test case which contains a javascript code with an if statement:
if(AllForms[i].innerText == “form a”)

However, other test cases will want to use the same javascript code in which the if statement will be:
if(AllForms[i].innerText == “form b”)

… and so on for the following test cases.

I would like to just pass the value as a function instead of having to re-write the the javascript code for each of the test cases.

What would be the best way to approach this when you have different values that are different within the same code and the code is being used in different test cases?

I wanted to use the code as a Paramenter for the step; however, I don;'t think I will be able to just pass a value(s) for the variable(s) depending on the test case(s).

Thanks!

Hello @luis.stolk,

You can create a test with a single step that performs the “Execute JavaScript” action, in that action, under the “Code” input field, you can insert your JS code, and parameterize the If statement.
For example:

image

You can remove the “ApplicationURL” parameter, as you won’t use it for this subtest.

image

So the test contains the following single step:

image

Then, you can import this test, as a subtest, in any test case you wish, by clicking on the “Add test as a step” button located on the bottom of the recorder.

image

The step we imported will require a parameter to set, the one which will be set in the JS code

image

This way, you can import this subtest to any test you wish, reuse the JS code, and fill out the required param value for each scenario.

Meidan, this is an awesome solution. It is exactly what I was looking for.

Now, What if my code requires 2 or more parameters? how do I use those parameters as INPUTS in the Param section ?

Greatly appreciate your help and time.

Have a great day!

Hey @luis.stolk,

You can use as many parameters as needed in the input field, for example:

(when creating a parameter, by default, it’s configured as an input parameter)

image

Then, once importing this test as a step in your main test, you will be able to set its parameters:

I figured that out after I realized I was using the parameters from the Project and not from Test.
Now is working exactly as you described.

Works perfect!
Many thanks for all your help Meidan!