Вопрос

I'd like to use CasperJS to evaluate a variable equals a certain value.

I simplified my exemple as much as I could that way:

var testDate = "24/03/14";
casper.test.begin('TEST', 1, function suite(test) {
    casper.start('http://www.google.com/', function() {
        this.test.assertEval(function() {
            return testDate == "24/03/14";
        }, "testDate is 24/03/14" );
    });
    casper.run(function() {
        this.test.done();
    });
});

I don't know why it fails, here is what I get in my console:

Test file: tests.js
#TEST
FAIL testDate is 24/03/14
#      type: assertEval
#      file: tests.js:7
#      code: }, "testDate is 24/03/14" );
#      subject: null
#      fn: undefined
#      params: undefined
FAIL 1 test executed in 2.896s, 0 passed, 1 failed, 0 dubious, 0 skipped.

Details for the 1 failed test:

In tests.js:7
   TEST
     assertEval: testDate is 24/03/14

Any idea ?

UPDATE

I realised my simplified example was faulty, it didn't represent what I really needed.

Actually, what I want to achieve is to test if a variable from the current page DOM context equals a local variable.

Это было полезно?

Решение 2

Ok found the answer myself.

To test if a variable from the current page DOM context equals a local variable, I realised I could use a simple assertEvalEquals():

test.assertEvalEquals(function() {
    return variableFromPageDomContext;
}, localVariable);

Likewise, when testing if a variable from the current page DOM context matches a RegExp pattern, we have to use evaluate() to get the variable from the DOM as the first parameter of an assertMatch():

test.assertMatch(this.evaluate(function() { 
    return variableFromPageDomContext; 
}), RegExpPattern);

Hope that can help.

Другие советы

As per manual Asserteval:

Asserts that a code evaluation in remote DOM strictly resolves to a boolean true:

your testdate variable is local to the casperjs script and is not accessible in the remote dom. You would have to inject it to the window like described here.

As @Surreal answers its possible to use the assertEvalEquals() passing the function and the expected value.

However the original question wants to pass a variable from casperjs context to assertEval() function, you can simply do it as follows, passing to assertEval() three arguments: the function which receive the value, a message for the assert and the value:

var varPassToEval = 'someValue';   
test.assertEval(
        function(varFromCasperContext){
            return varFromPageDomContext === varFromCasperContext;
        },
        'Assert Eval to test', 
        varPassToEval
);

With the above example probably is clear to use assertEvalEquals() however could be useful for more complex cases, for example imagine that you want to check if a text appears in a some <li> inside <ul> in DOM which it's dynamic and can change but you don't know at first where your text is... for this case you can use:

var somePartOfText = 'blue';   
test.assertEval(
        function(varFromCasperContext){
            return document.getElementsByTagName("ul")[0].textContent.indexOf(varFromCasperContext) != -1;
        },
        'Assert Eval to test', 
        somePartOfText
);

Hope it helps,

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top