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

javascript - Clicking a checkbox with protractor? - Stack Overflow

programmeradmin1浏览0评论

HTML code

<div class="check-box-panel">
<!--
 ngRepeat: employee in employees 
-->
<div class="ng-scope" ng-repeat="employee in employees">
    <div class="action-checkbox ng-binding">
        <input id="John" type="checkbox" ng-click="toggleSelection(employee.name)" 
                   ng-checked="selection.indexOf(employee.name) > -1" value="John"></input>

    <label for="John">
        ::before
    </label>
<!--
 John               
 end ngRepeat: employee in employees 
-->

<div class="ng-scope" ng-repeat="employee in employees">
<div class="action-checkbox ng-binding">
    <input id="Jessie" type="checkbox" ng-click="toggleSelection(employee.name)" 
               ng-checked="selection.indexOf(employee.name) > -1" value="Jessie"></input>

    <label for="Jessie"></label>

I tried using jQuery

element(by.repeater('employee in employees')).element(by.id('Jessie')).click();

also,I tried using css

element(by.repeater('employee in employees')).$('[value="Jessie"]').click();

But it didn't do the job. Any other way I can click on the particular Checkbox?

HTML code

<div class="check-box-panel">
<!--
 ngRepeat: employee in employees 
-->
<div class="ng-scope" ng-repeat="employee in employees">
    <div class="action-checkbox ng-binding">
        <input id="John" type="checkbox" ng-click="toggleSelection(employee.name)" 
                   ng-checked="selection.indexOf(employee.name) > -1" value="John"></input>

    <label for="John">
        ::before
    </label>
<!--
 John               
 end ngRepeat: employee in employees 
-->

<div class="ng-scope" ng-repeat="employee in employees">
<div class="action-checkbox ng-binding">
    <input id="Jessie" type="checkbox" ng-click="toggleSelection(employee.name)" 
               ng-checked="selection.indexOf(employee.name) > -1" value="Jessie"></input>

    <label for="Jessie"></label>

I tried using jQuery

element(by.repeater('employee in employees')).element(by.id('Jessie')).click();

also,I tried using css

element(by.repeater('employee in employees')).$('[value="Jessie"]').click();

But it didn't do the job. Any other way I can click on the particular Checkbox?

Share Improve this question edited Aug 11, 2015 at 19:04 Bhavin Solanki 4,8183 gold badges27 silver badges47 bronze badges asked Aug 9, 2015 at 17:51 CombospiritCombospirit 3131 gold badge2 silver badges11 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 9

Alright I had a very similar issue where I couldn't click the checkbox. It turned out I had to click the checkbox label. However if you want to do any checks on if the checkbox is selected then you have to check the actual checkbox. I ended up making two variables, one for the label and one for the actual checkbox.

JessieChkbxLabel = element(by.css("label[for='Jessie']"));
JohnChkbxLabel   = element(by.css("label[for='John']"));

//Click the Jessie checkbox
JessieChkbxLabel.click();

//Click the John checkbox
JohnChkbxLabel.click();

You should just need to use element(by.id('Jessie')) to grab your object. The issue you might be having is that click() returns a promise, so you need to handle that with a .then. So something along the lines of:

element(by.id('Jessie')).click().then(function(value) {
   // fulfillment
   }, function(reason) {
   // rejection
});

Aside from checking the id directly, you can also filter the desired element checking the employee name:

var employee = element.all(by.repeater('employee in employees')).filter(function (elm) {
    return elm.evaluate("employee.name").then(function (name) {
        return name === "Jessie";
    });
}).first();
employee.element(by.css("input[type=checkbox]")).click();
发布评论

评论列表(0)

  1. 暂无评论