tetutato
tetutato

Reputation: 648

Retrieving the checked property of an element in casperjs

At the moment I'm using the casper.evaluate function to do something like this (Using CoffeeScript)

bool = casper.evaluate ->
        document.querySelector('button selector').checked

This seems to work fine but I'm wondering if there's a built in casper method that I could use to retrieve the checked property of a checkbox/radio element? I've tried using getElementAttribute() but it won't detect 'checked' as an attribute. Also it is not listed in the JSON object retrieved from getElementInfo().

Upvotes: 2

Views: 513

Answers (1)

Artjom B.
Artjom B.

Reputation: 61892

No, CasperJS doesn't provide a function that gives you the checked property of an element, but you can easily create your own:

casper.isChecked = function(selector){
    var result = this.evaluate(function(selector){
        var el = document.querySelector(selector);
        return el ? el.checked : null;
    }, selector);
    if (result === null) {
        throw new CasperError("Selector not found");
    }
    return result;
};

The reason getElementAttribute() and getElementInfo() don't provide this is because checked is a property of the HTML element and not an attribute. Attributes are usually static and don't change even when the property changes.

Upvotes: 5

Related Questions