2

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>
Reporter
  • 3,897
  • 5
  • 33
  • 47
Yaniv Efraim
  • 6,633
  • 7
  • 53
  • 96

1 Answers1

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());
Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Thanks. 1. This is the only class (unique), and the selector is working (tested 'isPresent' and got true). 2. Trued moving it - did not work ): – Yaniv Efraim Jan 25 '16 at 13:05
  • @YanivEfraim okay, thanks, updated with couple more options. – alecxe Jan 25 '16 at 13:11
  • 1
    This worked for me , thanks for the solution.here is how I have implemented this PageOR.lblShowDisabledReq.isDisplayed().then(data => { if (data == true) { console.log("Show disable is present"); browser.actions().mouseMove(PageOR.chckShowDisabledReq).click().perform(); this.scrollpage.scrollDown(); console.log("Show disable is clicked"); } – Khyati Sehgal Jul 17 '19 at 13:44