最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Checking a radio button in Protractor + AngularJS - Stack Overflow

programmeradmin1浏览0评论

I have a radio button on my html page and I'd like to test the value of the current selected option

        <div>
            <input type="radio" name="radio1" value="enabled" checked/>
            <label for="radio1">Yes</label>
        </div>
        <div>
            <input type="radio" name="radio1" value="disabled" />
            <label for="radio1">No</label>
        </div>
        <br />

I'm using this code on the page object I use to test

var radio = $("input[type='radio'][name='radio1']:checked").val();

Unfortunately I get

val() is undefined

How can I return "enabled" or "disabled" based on the current status of the radio button?

I have a radio button on my html page and I'd like to test the value of the current selected option

        <div>
            <input type="radio" name="radio1" value="enabled" checked/>
            <label for="radio1">Yes</label>
        </div>
        <div>
            <input type="radio" name="radio1" value="disabled" />
            <label for="radio1">No</label>
        </div>
        <br />

I'm using this code on the page object I use to test

var radio = $("input[type='radio'][name='radio1']:checked").val();

Unfortunately I get

val() is undefined

How can I return "enabled" or "disabled" based on the current status of the radio button?

Share Improve this question asked Oct 28, 2016 at 10:24 Gianluca GhettiniGianluca Ghettini 11.7k24 gold badges99 silver badges168 bronze badges 2
  • Same result unfortunately – Gianluca Ghettini Commented Oct 28, 2016 at 10:32
  • @Maximus val() is a jQuery function, he's using Protractor. – Gunderson Commented Oct 28, 2016 at 13:10
Add a ment  | 

2 Answers 2

Reset to default 3

val() is a jQuery function which you do not inherently have access to unless you setup Protractor that way. Use getAttribute('value') instead, which returns a promise - see the getAttribute() reference

So if you are using it in an assertion, you can let expect resolve the promise itself:

var radio = $("input[type='radio']:checked")
expect(radio.getAttribute('value')).toEqual('enabled');

Or if you want to access the value and use it elsewhere, resolve the promise yourself:

radio.getAttribute('value').then(function (val) {
    if(val === 'enabled') {
        // code
    }
});

Instead of 'enabled' the value = 1

// using TS

static myElement = element
    .all(by.css('input[type="radio"]:checked'))
    .get(0);

expect(this.myElement.getAttribute('value')).toEqual(
      '1'
    );
发布评论

评论列表(0)

  1. 暂无评论