How to check if the checkbox is selected for protractor e2e testing

angularjs-e2echeckboxprotractor

I am trying to check if a checkbox has been selected and if not check it. No matter what, I can never check it since it never reaches to the click(). Please see code below.

This is the code in the protractor test:

//find item
var useSslInput = $('#useSsl');
if(newDirectory.useSsl == true){
       if(!useSslInput.isSelected()){
            useSslInput.click();
         }
         var sslInput = $('#sslCertificate');
         sslInput.sendKeys(newDirectory.sslCertificate);
} else {
         if(!useSslInput.isSelected()){
           useSslInput.click();
         }
}

The html is below:

<div class="field">
 <label class="field-label" for="useSsl">{{::'directory.page.add.label.useSsl' |i18n}}</label>
<div class="field-input"><input id="useSsl" name="useSsl" type="checkbox" ng-model="addEditDirectory.directory.useSsl" ng-checked="addEditDirectory.directory.useSsl"></div>
</div>

Am I doing something wrong here?

Best Solution

Read up about using promises: http://www.html5rocks.com/en/tutorials/es6/promises/

In your particular case, you want something like:

var useSslInput = $('#useSsl');
if(newDirectory.useSsl == true) {
  useSslInput.isSelected().then(function(selected) {
    if (!selected){
       useSslInput.click();
    }
    var sslInput = $('#sslCertificate');
    sslInput.sendKeys(newDirectory.sslCertificate);
  });
} else {
  useSslInput.isSelected().then(function(selected) {
    if (!selected) {
      useSslInput.click();
    }
  });
}