【问题标题】:How to run a casperjs script from Java如何从 Java 运行 casperjs 脚本
【发布时间】:2017-10-15 09:46:47
【问题描述】:

我有以下 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");

命令运行但没有下载文件。 从java执行这个脚本的正确方法是什么?

  1. 在 java 中执行类似于我通过 CLI 执行的操作时,将 url 作为参数传递。我们该怎么做?

编辑: 我尝试使用脚本引擎 eval 函数:

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

【问题讨论】:

    标签: java phantomjs casperjs


    【解决方案1】:

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

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

    java YourClassWithMainMethod arg1 arg2 等

    public class YourClassWithMainMethod {
        public static void main (String[] args) {
            String firstArg = args[0];
            String scndArg = args[1];
            // etc
            // do your stuff with input
        }
    }
    

    【讨论】:

    • 使用脚本引擎类不起作用。如问题中所述,我收到 "ScriptException: ReferenceError: "require" is not defined"
    • 你很可能必须先加载它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多