如何从Java运行casperjs脚本

时间:2017-05-16 10:45:41

标签: java phantomjs casperjs

我有以下casperjs脚本:

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();
});

脚本在通过CLI执行时运行,并将文件下载到Temp文件夹中。我通过CLI传递url并执行脚本命令如下:

--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"

现在我有两个任务:

  1. 从java运行此脚本:为此我尝试使用以下命令:

    处理进程= Runtime.getRuntime()。exec(" C:/ Program Files(x86)/PhantomJS/phantomjs-2.1.1-windows/bin/phantomjs download.js");

  2. 该命令运行但未下载任何文件。 从java执行此脚本的正确方法是什么?

    1. 在java中执行时,将url作为参数传递,类似于我通过CLI执行的操作。我们怎么能这样做?
    2. 编辑: 我尝试使用脚本引擎评估功能:

      ScriptEngineManager manager = new ScriptEngineManager();
      ScriptEngine engine = manager.getEngineByName("JavaScript");
      engine.eval(new java.io.FileReader(System.getProperty("user.dir")+"/resources/CasperTest.js"));
      

      但在这种情况下,我得到以下异常:

      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
      

1 个答案:

答案 0 :(得分:0)

查看How to run javascript in java programming以及调用外部javascript文件的示例https://gist.github.com/UnquietCode/5614860

您可以按如下方式传递参数:

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
    }
}