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?

Était-ce utile?

La 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);     
}

Autres conseils

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');
});
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top