0

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:

  1. 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?

  1. 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
Community
  • 1
  • 1
aditi mittal
  • 1
  • 1
  • 5

1 Answers1

0

Have a look at How to run javascript in java programming and example that calls an external javascript file https://gist.github.com/UnquietCode/5614860

And you can pass arguments as follows:

java YourClassWithMainMethod arg1 arg2 etc

public class YourClassWithMainMethod {
    public static void main (String[] args) {
        String firstArg = args[0];
        String scndArg = args[1];
        // etc
        // do your stuff with input
    }
}
Community
  • 1
  • 1
ipper
  • 624
  • 4
  • 13
  • using script engine class did not worked. I am getting "ScriptException: ReferenceError: "require" is not defined" , as mentioned in the question – aditi mittal May 17 '17 at 08:46
  • You most likely have to load that first. – ipper May 17 '17 at 09:42
  • have a look at http://stackoverflow.com/questions/32607089/how-should-i-run-nodejs-from-a-java-application#32746674 – ipper May 17 '17 at 10:09