【问题标题】:Clicking a checkbox with protractor?单击带有量角器的复选框?
【发布时间】:2015-11-01 15:14:18
【问题描述】:

HTML 代码

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

我尝试过使用 jQuery

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

另外,我尝试使用 css

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

但它没有完成这项工作。我可以通过其他方式点击特定的复选框吗?

【问题讨论】:

    标签: javascript css angularjs protractor


    【解决方案1】:

    好的,我遇到了一个非常相似的问题,我无法单击复选框。原来我必须点击复选框标签。但是,如果您想检查复选框是否被选中,那么您必须检查实际的复选框。我最终制作了两个变量,一个用于标签,一个用于实际复选框。

    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();
    

    【讨论】:

      【解决方案2】:

      除了直接检查id之外,您还可以filter检查员工姓名所需的元素:

      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();
      

      【讨论】:

        【解决方案3】:

        你应该只需要使用 element(by.id('Jessie')) 来抓取你的对象。您可能遇到的问题是 click() 返回一个承诺,因此您需要使用 .then 来处理它。所以大致如下:

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

        【讨论】:

        • 但是如何将它识别为复选框元素?它可以很好地点击相应的文本?如果我错了,请纠正我。
        • 我的理解是它实际上并不是在页面上“点击”,它会向元素发送“您已被点击”消息,然后调用:toggleSelection(employee.name)。
        • API 的几个额外点:element(locator) 返回一个 ElementFinder,它可以在大多数情况下被视为 WebElement,特别是,您可以执行操作(即单击)和一个 ElementFinder在调用动作之前实际上不会尝试查找元素。 angular.github.io/protractor/#/api?view=ElementFinder
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-03
        • 1970-01-01
        • 2011-12-28
        相关资源
        最近更新 更多