Question

I want to click on a checkbox only if it is already checked.

The following code doesn't seem to work:

if(this.test.assert(this.evaluate(function () {return document.getElementById('myID').checked;}))) 
{           
    this.click('#myID');     
}

What's the correct syntax?

Was it helpful?

Solution

The test.assert* functions are there for asserting a certain condition. Remove it because you want to branch the execution. Otherwise is your code correct.

test.assertExists("#myID");
// this function will not be executed if #myID wasn't there
var id = 'myID';
if(this.evaluate(function (pageCtxId) {return document.getElementById(pageCtxId).checked;}, id))
{           
    this.click('#'+id);     
}

OTHER TIPS

You might want to do it that way :

casper.waitFor(function checked() {
    return this.evaluate(function() {
        return document.getElementById('myID').checked === true;
    });
}, function then() {
    this.click('#myID');
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top