使用量角器选中一个复选框

时间:2016-01-25 11:48:00

标签: javascript angularjs protractor angularjs-e2e

我有一个简单的复选框,我想使用量角器(AngularJS)检查。

我试过了:

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

但它没有帮助。选择器正在工作(找到我的元素),但没有运气点击。我也尝试过:

<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>

但它不起作用(它正在进入'then'但是如果我暂停浏览器,我看到没有选中复选框)

我做错了什么?

编辑我的HTML代码:

@Component({
  selector: 'child-component',
  template: 'Child'
})
class ChildComponent {
}
@Component({
  selector: 'my-app',
  template: 'Parent (<child id="child"></child>)'
})
class MyApp {
  constructor(dcl: DynamicComponentLoader, injector: Injector) {
    dcl.loadAsRoot(ChildComponent, '#child', injector);
  }
}

1 个答案:

答案 0 :(得分:3)

首先,检查这是否是与选择器匹配的唯一元素,并且您实际上正在找到所需的复选框。此外,尝试移动到元素,然后点击:

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

或者,滚动到元素的视图

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

或者,点击通过JavaScript (尽管了解implications):

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