How to get Casper JS to return an exit code that indicates test success status?

你说的曾经没有我的故事 提交于 2019-12-21 13:07:09

问题


I want to be able to have a set of Casper JS tests and get an exit code back of 0 on success and non-zero on error or test failure (I want to run the casper command from java and determine if a test passed).

The problem I am having is that an exit code of 0 is always returned. Here is an example test where this happens:

var casper = require('casper').create();

casper.start('http://www.google.com', function() {
    this.test.assertEquals(true, casper.cli.options['value']);
});

casper.run(function() {
        casper.test.done(1);
});

All of the following commands result in an exit code of 0:

C:/casperjs/bin/casperjs test --value=true C:/Temp/simpletest.js
C:/casperjs/bin/casperjs test --value=false C:/Temp/simpletest.js
C:/casperjs/bin/casperjs --value=true C:/Temp/simpletest.js
C:/casperjs/bin/casperjs --value=false C:/Temp/simpletest.js

How can I invoke Casper and determine whether the tests succeeded or failed/errored from Java?


回答1:


First, you cannot overwrite the casper instance in test mode, see http://docs.casperjs.org/en/latest/testing.html#test-command-args-and-options

Remove

var casper = require('casper').create();

from your code.

Then try

casper.start('http://www.google.com', function(test) {
    test.assertEquals(true, casper.cli.options['value']);
});

Start casperjs with

--fail-fast

so that each test will exit with code 1.

Then in Java

String[] args = {"/bin/sh", "-c", "casperjs test --fail-fast simpletest.js"};

Process proc = Runtime.getRuntime().exec(args);

logger.log(Level.INFO, IOUtils.toString(proc.getInputStream()));
String warnings = IOUtils.toString(proc.getErrorStream());

if (StringUtils.isNotBlank(warnings)) {
    logger.log(Level.WARNING, warnings);
}

int exitValue = proc.exitValue();

assertEquals(0, exitValue);

Of course you need to change the paths to suit your environment.

Hope that helps!




回答2:


The problem I am having is that an exit code of 0 is always returned.

Your casper test should be like that:

var casper = require('casper').create();
var system = require('system');
var param;

casper.start('http://www.google.com', function() {

    //get parameter from command line
    system.args.forEach(function (arg, i) {
        if(arg.search(/--value=/i) != -1){
            param = arg.replace(/--value=/g, "");
        }
    });    

    this.test.assertEquals(true, Boolean(param));
});

casper.run(function() {
        this.test.done(1); 
        this.test.renderResults(true);
});

To run:

casperjs simpletest.js --value=true

How can I invoke Casper and determine whether the tests succeeded or failed/errored from Java?

You should look this answer:

CasperJS passing data back to PHP




回答3:


Return the Predefined code for failure (for eg for us we gave 99 (random))

//Capture all fails
casper.test.on("fail", function () {
    casper.exit(99);
});

Similarly you can define different code for other issues at high level

eg: to get the retry logic in place we can use onWaitTimeout codes

casper.options.onWaitTimeout = function () {
    casper.screenCapture('POETerror', true);
    this.exit(98);
};


来源:https://stackoverflow.com/questions/17176452/how-to-get-casper-js-to-return-an-exit-code-that-indicates-test-success-status

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!