I have below casperjs script:
var casper = require('casper').create({
verbose: true,
logLevel: 'debug',//needed for debugging
pageSettings: {
webSecurityEnabled: false,
}
});
casper.start('http://toolsqa.com/automation-practice-form/',function() {
var path = 'C:/WINDOWS/TEMP';
this.download(casper.cli.get("url"),path + "/test.xls");
});
casper.run(function() {
this.echo('Done.').exit();
});
The script runs when executed through CLI and downloads the file in Temp folder. I am passing the url through CLI and command to execute the script is as below:
--C:\js examples>casperjs testfile path --url="url of file to be downloaded"
--C:\js examples>casperjs CaspetTest.js --url="http://20tvni1sjxyh352kld2lslvc.wpengine.netdna-cdn.com/wp-content/uploads/2016/09/Test-File-to-Download.xlsx"
Now I have two tasks:
to run this script from java: to do this i tried using below command:
Process process = Runtime.getRuntime().exec("C:/Program Files (x86)/PhantomJS/phantomjs-2.1.1-windows/bin/phantomjs download.js");
The command runs but no file is downloaded. What is the correct way to execute this script from java?
- To pass the url as an argument when executing in java something like what I am doing through CLI. How can we do that?
Edit: I tried using Script engine eval function:
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
engine.eval(new java.io.FileReader(System.getProperty("user.dir")+"/resources/CasperTest.js"));
But in this case i get below exception:
javax.script.ScriptException: ReferenceError: "require" is not defined in <eval> at line number 1
at jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:455)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:439)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:401)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:397)
at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:147)
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:249)
at loginpage.main(loginpage.java:91)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: <eval>:1 ReferenceError: "require" is not defined
at jdk.nashorn.internal.runtime.ECMAErrors.error(ECMAErrors.java:57)
at jdk.nashorn.internal.runtime.ECMAErrors.referenceError(ECMAErrors.java:319)
at jdk.nashorn.internal.runtime.ECMAErrors.referenceError(ECMAErrors.java:291)
at jdk.nashorn.internal.objects.Global.__noSuchProperty__(Global.java:914)
at jdk.nashorn.internal.scripts.Script$\^eval\_.:program(<eval>:1)
at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:636)
at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:229)
at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:387)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:437)
... 10 more
[INFO - 2017-05-12T07:27:05.869Z] ShutdownReqHand - _handle - About to shutdown