If you are not familiar with the process of creating scripted components in useMango™, you can learn more about it here.
Create a new scripted component in useMango™.
After successfully creating the component, select Python from the Engine dropdown.
Let’s start by writing a Python script to divide 2 numbers:
We will use the same component that we created previously. Change the component’s name to “Divide.”
Let’s include some parameters that will be passed to our newly created component.
If you are not familiar with the process of using parameters in scripted components in useMango™, you can learn more about it here.
Here’s a sample code snippet that divides 2 numbers:
def execute(log, P1, P2):
dividend = float(P1)
divisor = float(P2)
if divisor != 0:
result = dividend/divisor
return Pass("Division successful.", P3=result)
else:
raise Exception("Zero division error!")
The name of the component function should be execute. We are passing a log parameter that can be used to show information / debug the component.
To log information, we can use the info() method, for example,
log.info ("This is informing")
To debug, use the debug() method, for example,
log.debug ("This is debugging")
It also accepts a variable number of input arguments, which are identified by their parameter Ids, in this case P1 and P2.
To pass a component, we need to return a Pass object whose constructor is of the form:
Pass(message, **outputs)
The message parameter, which specifies the success message, is of type string.
The outputs parameter, which specifies the component outputs, is a variable number of keyword arguments that can be specified in the format key1=value1, key2=value2, and so on. In our case, the output is saved in a variable with the Id P3.
To indicate that a component has failed, we must throw an Exception object containing the failure message string.
That’s it! You’ve created your first Python component, which you can use in any of your tests!