Welcome to the new Parasoft forums! We hope you will enjoy the site and try out some of the new features, like sharing an idea you may have for one of our products or following a category.
How to import random in jython
LegacyForum
Posts: 1,664 ✭✭
how to import random in jython
In jython script, I have added:import random
This immediately makes the list of Methods disappear. How do I access a random function in a Jython external tool script?
You can make this happen simply by typing this into an External tool text window for language Python:
from com.parasoft.api import *
from soaptest.api import *
def test_method(input, context):
import random
Tagged:
0
Comments
-
Hello Brian,
Strictly speaking, SOAtest actually uses Jython, not Python. The
distinction is important in this case, because some of the extended
Python libraries are not included with Jython - but all of the Java
libraries are included. In this case, it would be easier to use the Java
Random class to generate random numbers.
For example, you can accomplish this using the following script (it just
prints out the number - you can do something more useful with it):
from java.util import Random
from com.parasoft.api import *
def showRandomNumber(input, context):
random = Random()
value = random.nextInt(90)+10
Application.showMessage(str(value))
For more information about the Java Random class, find the online docs
here: http://java.sun.com/j2se/1.5.0/docs/api/ja...til/Random.html
If you really want to use the Jython libraries, you can install Jython 2.2.1 and point SOAtest to that directory (Parasoft->preferences->Parasoft->SOAtest->Scripting-> .... set the Jython Home to the install directory).
Regards,
James0