如何在量角器中选择具有相同Web元素的复选框

时间:2018-05-22 08:01:40

标签: angularjs jasmine protractor

我正在尝试使用量角器+ Jasmine为E2E测试编写自动脚本。但是,在选择一个复选框时会遇到困难,其中所有三个复选框的所有Web元素都相同。我想选择第二个复选框。

有人可以指导我完成这个问题吗?下面是您的类型参考的Angular JS代码片段。

即使是xpath和CSSSelector值也没有用,因为它们也一样。

CSSSelector:#checks xpath:// * [@ id ="检查"]

我看到的唯一标签是[class =" ng-binding"]。

段:

<tr x-ng-repeat="productOffering in allProductList" class="prod-input-select ng-scope">
<td>
<input ng-show="productOffering.added == 'N'" name="checks" id="checks" type="checkbox" ng-click="addTempProductToBeAdded($event,productOffering)" ng-model="productOffering.isSelected" class="ng-pristine ng-valid">
</td>
<td class="ng-binding">
Product1
</td>
</tr>
<tr x-ng-repeat="productOffering in allProductList" class="prod-input-select ng-scope">
<td>
<input ng-show="productOffering.added == 'N'" name="checks" id="checks" type="checkbox" ng-click="addTempProductToBeAdded($event,productOffering)" ng-model="productOffering.isSelected" class="ng-pristine ng-valid">
</td>
<td class="ng-binding">
Product2
</td>
</tr>
<tr x-ng-repeat="productOffering in allProductList" class="prod-input-select ng-scope">
<td>
<input ng-show="productOffering.added == 'N'" name="checks" id="checks" type="checkbox" ng-click="addTempProductToBeAdded($event,productOffering)" ng-model="productOffering.isSelected" class="ng-pristine ng-valid">
</td>
<td class="ng-binding">
Product3
</td>
</tr>

干杯, 阿希什

2 个答案:

答案 0 :(得分:0)

我认为您应该更改ID以区分您的支票

<tr x-ng-repeat="productOffering in allProductList" class="prod-input-select ng-scope">
    <td>
    <input ng-show="productOffering.added == 'N'" name="checks" id="checks-0-{{$index}}" type="checkbox" ng-click="addTempProductToBeAdded($event,productOffering)" ng-model="productOffering.isSelected" class="ng-pristine ng-valid">
    </td>
    <td class="ng-binding">
    Product1
    </td>
</tr>
<tr x-ng-repeat="productOffering in allProductList" class="prod-input-select ng-scope">
    <td>
    <input ng-show="productOffering.added == 'N'" name="checks" id="checks-1-{{$index}}" type="checkbox" ng-click="addTempProductToBeAdded($event,productOffering)" ng-model="productOffering.isSelected" class="ng-pristine ng-valid">
    </td>
    <td class="ng-binding">
    Product2
    </td>
</tr>
<tr x-ng-repeat="productOffering in allProductList" class="prod-input-select ng-scope">
    <td>
    <input ng-show="productOffering.added == 'N'" name="checks" id="checks-2-{{$index}}" type="checkbox" ng-click="addTempProductToBeAdded($event,productOffering)" ng-model="productOffering.isSelected" class="ng-pristine ng-valid">
    </td>
    <td class="ng-binding">
    Product3
    </td>
</tr>

也许您可以尝试使用 startsWith 选择器

CSSSelector:[id^="checks"]

xpath://*[starts-with(@id,'checks')][@type="checkbox" and (@checked)]

答案 1 :(得分:0)

正如Tony在评论中提到的那样,DOM中不能有多个具有相同[^;\d+\;\d+\.\d+;\W\d+\.\d+]的元素。 理想情况下,你需要解决这个问题。

现在让我们假设你手中的得到纠正,你仍然可以使用下面的选择器来选择第二个复选框

id 要么 element.all(by.css(input[name="checks"]))[1]

请检查哪一个适合您。