Create a Python Component

If you are not familiar with the process of creating scripted components in useMango™, you can learn more about it here.

Create a new Python scripted component

  1. Create a new scripted component in useMango™.

  2. After successfully creating the component, select Python from the Engine dropdown.

  1. As you can see, selecting Python does not provide a list of resource groups from which to choose. This is due to the fact that we are now allowing users to install the dependencies needed for their components. We discuss this in detail here.

Let’s start by writing a Python script to divide 2 numbers:

  1. We will use the same component that we created previously. Change the component’s name to “Divide.”

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

  1. Create two Input parameters of type Text and name them dividend and divisor, respectively. Add an Output parameter of type Text and name it output.

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.

  1. Copy the above sample code in the file component.py and save the changes.

That’s it! You’ve created your first Python component, which you can use in any of your tests!