How to set test case fail and pass messages in Casper js?

816 views Asked by At

How I can set two different test messages for fail and pass test cases and after running all test cases how I can get list of all fail and pass messages which I can print in log file or xml file.

What is better way to store all messages and get in correct format.

Here is my Sample Home page.js

    HomePage = function () {

    var PageTestParams = TestParams.Modules.HomePage,
        PageLangText = Lang.Modules.HomePage;

    SearchResult = function(test){

    },
    SearchTextField = function(test){
        test.assertExists(PageTestParams.SearchFormSelector, PageLangText.SuccessMsg["SearchForm"]);
        SearchResult(test);
    },
    NavigationCount = function(test){
         test.assertElementCount(PageTestParams.NavigationSelector, PageTestParams.NavigationCount,PageLangText.SuccessMsg["NavigationCount"]);
         SearchTextField(test);
    },
    CheckTitle = function(test){
        test.assertTitle(PageTestParams.Title, PageLangText.SuccessMsg["TitleText"]);
        casper.test.pass("main fail ho gaya");
        NavigationCount(test);
    },
    this.init = function(test){
        CheckTitle(test);
    }
};

I am passing this JS for test assertion if any test case fail or pass the same message is getting printed for same scenario. I have searched and got below syntax but its printing same message which I have setted in test assertion.

                        casper.test.on("fail", function(failure) {
                            casper.echo("FAIL " + failure.message);
                        });
2

There are 2 answers

0
Rippo On

You can do something like this (same logic for success or fail):-

var successes = [];

casper.test.on("success", function(success) {
    successes.push(success);
});

Then on casper.run you can do a couple of things:-

1) To see the full dump of all successes (you can see all the properties) you can do this:-

casper.run(function () {
    test.done();
    require('utils').dump(successes);
});

2) to output the array of successes one by one (so you can format the output) you can do:-

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

    successes.forEach(function(item){
        casper.echo(item.standard);
    });
});

3) I assume you know you can also output the full test assertions to xunit by passing in --xunit=output.xml

0
Twtheo On

To get two different messages when using a single test is something I have personally not come across yet. If you want a way to work around it you could do something like:

var x=4;
if(x>2){
casper.test.pass("PASS Message.");
}else{
casper.test.fail("FAIL Message.");
}

You could manipulate the if statement to have a boolean or whatever else you may like, but this seems like the most obvious way I would do it. As for getting a list of all passing and failing messages, the extent I know about that is that there are the getFailures(); and a getPasses(); methods, these may or may not be what your looking for, but maybe they can help get you started! Good Luck!