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

javascript - Check a checkbox using protractor - Stack Overflow

programmeradmin2浏览0评论

I have a simple checkbox which I would like to check, using protractor (AngularJS).

I tried:

$('.checkbox-class-name').click();

But it does not help. The selector is working (finding my element), but no luck with the click. I also tried:

it('should test something if checkbox is checked', function (done) {
   myPage.navigate();
   $('.checkbox-class-name').click().then(function() {
     //expect something here
     done();   
   })
});

But it does not work (it is getting inside the 'then' but if I pause the browser I see that the checkbox is not checked)

What am I doing wrong?

EDIT my HTML code:

<div ng-if="vm._getTermsAndConditionsFlag()">
   <input class="checkbox-class-name" id="checkbox-name"
          ng-model="vm.termsAndConditionsChecked" type="checkbox">
   <label for="checkbox-name" >I agree to the terms & conditions</label>
</div>

I have a simple checkbox which I would like to check, using protractor (AngularJS).

I tried:

$('.checkbox-class-name').click();

But it does not help. The selector is working (finding my element), but no luck with the click. I also tried:

it('should test something if checkbox is checked', function (done) {
   myPage.navigate();
   $('.checkbox-class-name').click().then(function() {
     //expect something here
     done();   
   })
});

But it does not work (it is getting inside the 'then' but if I pause the browser I see that the checkbox is not checked)

What am I doing wrong?

EDIT my HTML code:

<div ng-if="vm._getTermsAndConditionsFlag()">
   <input class="checkbox-class-name" id="checkbox-name"
          ng-model="vm.termsAndConditionsChecked" type="checkbox">
   <label for="checkbox-name" >I agree to the terms & conditions</label>
</div>
Share Improve this question edited Jan 25, 2016 at 12:55 Reporter 3,9365 gold badges35 silver badges49 bronze badges asked Jan 25, 2016 at 11:48 Yaniv EfraimYaniv Efraim 6,7118 gold badges54 silver badges98 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 6

First of all, check if this is the only element matching the selector and you are actually locating the desired checkbox. Also, try moving to the element and then making a click:

var checkbox = $('.checkbox-class-name');
browser.actions().mouseMove(checkbox).click().perform();

Or, scroll into element's view:

browser.executeScript("arguments[0].scrollIntoView();", checkbox.getWebElement());
checkbox.click();

Or, click via JavaScript (do understand the implications though):

browser.executeScript("arguments[0].click();", checkbox.getWebElement());
发布评论

评论列表(0)

  1. 暂无评论