How can I retrieve/use the project location in a relative path to open and read/write to a text file
I am using the following Jython script to read and then update a value from a text file on my local pc, but need to be able to use a relative path to identify the file in both line 7 and 14. I have verified my PROJECT_LOC variable is: "C:/Users//File/Path/To/Workspace/Project". Any help is appreciated.
Works Locally but not on Server:
from java.util import *
from java.text import *
from java.io.File import *
import cStringIO
def writeString():
outwrite = open("C:/Users//File/Path/To/Workspace/Project/File.txt","r+")
OrderNoStr = outwrite.readline()
print OrderNoStr
OrderNoInt = int(OrderNoStr)
print OrderNoInt
NewOrderNoInt = OrderNoInt + 1
print NewOrderNoInt
outwrite = open("C:/Users//File/Path/To/Workspace/Project/File.txt","w")
outwrite.write(str(NewOrderNoInt))
outwrite.close()
Best Answer
-
You can modify the method to accept two arguments:
def writeString(input, context)
From the 2nd parameter to the method (called "context" in my example), you can call
context.getAbsolutePathFile("File.txt")
This will return a java.io.File object, whose path is created by resolving the path passed into getAbsolutePathFile with the path to the .tst file that the script lives inside. So if the .tst is at c:/myworkspace/myproject/mytest.tst, then the call above would return a File object for c:/myworkspace/myproject/File.txt.
You can view JavaDocs for this method by going to Help > Help Contents > Parasoft SOAtest Extensibility API and viewing the ScriptingContext interface.
6
Answers
-
Hi tcammans,
The script itself looks correct. The one question I have is related to the server portion though: what OS is your server running on? Looking at the script, it looks like the path to your workspace is hardcoded. If this is the case and the server is running on Linux architecture, the script will fail as it is trying to open a file given by a windows path.
If it is on a Windows server, is the users directory in your path hardcoded to an incorrect/nonexistent directory? If this is the case, then that could also cause the script to fail.
Unless someone else on here can come up with an easier means of grabbing this info through a script, one thing that you could to do dynamically grab this value is have a messaging client set to the None with ${project_loc} in the request body. Then chain a text databank to the request and extract the location.
From your script you can then grab the value using context.getValue("Generated Data Source", "your_extraction_name_here")
EDIT: I would highly recommend going with Nathan's solution in the post below mine, much simpler without as many moving parts .
Hope this helps!
0 -
Thanks @jakubiak and @Thomas Moore I was able to get it working with your help. What the working script looks like now is:
from com.parasoft.api import *
from java.util import *
from java.text import *
from java.io.File import *
import cStringIOdef writeString(input, context):
outwrite = open(str(context.getAbsolutePathFile("File.txt")),"r+") OrderNoStr = outwrite.readline() print OrderNoStr OrderNoInt = int(OrderNoStr) print OrderNoInt NewOrderNoInt = OrderNoInt + 1 print NewOrderNoInt outwrite = open(str(context.getAbsolutePathFile("File.txt")),"w") outwrite.write(str(NewOrderNoInt)) outwrite.close()
I had not realized that I needed to make the changes on the Input Tab to use a File input and establish the file to use as File.txt (in this example).
Thanks again.
0