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
2 Answers
Reset to default 3val()
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'
);