How should be the exe file variable declaration, if i pass any variable from a java program.
Ex->
String[] var = {"var1","var2"};
Process p = Runtime.getRuntime().exec("open /Users/*****/Documents/batch/dateCommand",var);
Any help would be appreciated
Related
May I know how to print the directory path which is 1 level up? (eg. the groovy file is located in "abc/def/ghi/dummy.groovy" and I want to get the "abc/def" path)
here is my dummy.groovy script
File fileCon= new File("/../")
logger.debug((String.format("[%s]", fileCon))
groovy file could be loaded from plain file, from jar, from url.
i'd not recommend to use this approach - it will not work for all cases.
def url = this.getClass().getProtectionDomain().getCodeSource()?.getLocation()
println new URL(url, '..')
Here is how you get the parent directory as File:
def file = new File('abc/def/ghi/dummy.groovy')
println "Parent: ${file.getParentFile().absolutePath}"
it will give you abc/def/ghi/. You may get parent folder from the result:
println "Parent: ${file.getParentFile().getParentFile().absolutePath}"
you'll get your desired abc/def.
I didn't see any File in GroovyDocs, so I presume this is a Java Class.
So why not just use:
def file = new File('abc/def/ghi/dummy.groovy')
def filePath = file.getParent().getParent()
Consider that we have the following Groovy script:
temp = a + b
temp * 10
Now, given that a and b are bound to the context with their respective values, and that the script is executed using a groovy shell script.
Is there a way I could obtain the value of thetemp variable assignment, without printing/logging the value to the console? For instance, given a=2 and b=3, I would like to not only know that the script returned 50 but also that temp=5. Is there a way to intercept every assignment to capture the values?
Any suggestions or alternatives are appreciated. Thanks in advance!
You can capture all bindings and assignments from the script by passing a Binding instance to the GroovyShell object. Consider following example:
def binding = new Binding()
def shell = new GroovyShell(binding)
def script = '''
a = 2
b = 3
temp = a + b
temp * 10
'''
println shell.run(script, 'script.groovy', [])
println binding.variables
Running this script prints following two lines to the console:
50
[args:[], a:2, b:3, temp:5]
If you want to access the value of temp script variable you simply do:
binding.variables.temp
How to pass a buffer to a function instead of passing a file path?
This doesn't work (it's a bug) so I need another way to pass a buffer to the first argument which sends to stdout, but how?
page.render('/dev/stdout', {format : 'pdf'});
I can do something like this, but don't want to create a file
var pdf_file = 'test.pdf';
page.render(pdf_file, {format : 'pdf'});
var pdf = fs.read(pdf_file);
require('system').stdout.write(pdf);
fs.remove(pdf_file);
I wrote a groovy script in soapui to create files in certain location in my pc. How can I make it dynamic and enable the user to write the location the files are saved to by write the location in configuration file imported at test suite level.
if(context.expand('${#Project#ProduceReports}') == 'true') {
def resultDir = new File("D:\\Reports");
if(!resultDir.exists()) {
resultDir.mkdirs();
}
def resultsFile = new File(resultDir, "CSVReport.csv");
}
If you want to get the path from a testSuite property, you can do it as you do with the project property, using context.expand:
def yourPath = context.expand('${#TestSuite#pathDirectory}')
Or alternatively you can do the same with:
def yourPath = context.testCase.testSuite.getPropertyValue('pathDirectory')
Maybe this is out of scope for your question, but could be helpful. If you need you can also use UISupport to ask the user to enter the path he wants with the follow code:
def ui = com.eviware.soapui.support.UISupport;
// the prompt question, title, and default value
def path = ui.prompt("Enter the path","Title","/base/path");
log.info path
This shows:
Define project level custom property REPORT_PATH with value D:/Reports/CSVReport.csv i.e., full path including file and path separate by / slash even on windows platform.
Then use the below script to write the data.
//Define the content that goes as report file. Of course, you may change the content as need by you
def content = """Name,Result
Test1,passed
Test2,failed"""
//Read the project property where path is configured
def reportFileName = context.expand('${#Project#REPORT_PATH}')
//Create file object for reports
def reportFile = new File(reportFileName)
//Create parent directories if does not exists
if (!reportFile.parentFile.exists()) {
reportFile.parentFile.mkdirs()
}
//Write the content into file
reportFile.write(content)
I am running into a bit of a strange error here when creating a new file object with a GStringImpl. If I create a new File (and then list files in that path) with a GStringImpl, I get an empty array, and no error, however if I just a normal string, I get a list of files... While that makes sense in a way I would think that there would be an error somewhere.
Example:
def thisIsAListOfFiles = new File("/absolute/fs/mount/point").listFiles()
def gString = "${StaticClass.propertyStringThatIsAnAbsoluteFilePath}"
def notAListOfFiles = new File(gString).listFiles()
Any thoughts on what is going on here? Is this the expected behavior?
More info:
Groovy Version: 2.1.3
Grails version: 2.2.2 (this is within a grails app of course)
Java version: OpenJDK Runtime Environment (IcedTea 2.3.9)
I start out with a properties file with a bunch of properties like this
com.mycompany.property = "/absolute/directory/path"
Because I cant easily inject grailsApplication into non grails classes (anything in /src/groovy for instance) I inject grailsApplication into bootstrap, and use groovy config slurper to read the properties file from the classpath and set then as static string values in a groovy class Config.groovy. That groovy class then has the static variables of all of the properties I need anywhere within the application.
Note: this is not an issue reading the properties files, or anything along those lines. I log the Config.filePathProperty right before the new File(var).listFiles() occurs and that value is set properly.
I'm pretty sure your static path is set incorrectly. I ran the following code as a test:
String path = '/etc/'
print "String ($path): "
println(new File(path).listFiles().size())
def gpath = "${path}"
print "GString ($gpath): "
println(new File(gpath).listFiles().size())
class Foo {
static String path = '/etc/'
}
print "GString static ($Foo.path): "
println(new File("${Foo.path}").listFiles().size())
And got this result (obviously your file count will vary):
String (/etc/): 122
GString (/etc/): 122
GString static (/etc/): 122
The only time I saw a null result was when the path was invalid, for example:
assert new File("does-not-exist").listFiles() == null
One thing you could do would be to eliminate the GString, which is unnecessary in your example:
def notAListOfFiles = new File(StaticClass.propertyStringThatIsAnAbsoluteFilePath).listFiles()
But I'm sure you'll find a typo in the variable or file path, or another similar issue.