量角器 - 选择相关元素的最佳方式

时间:2014-11-28 12:30:47

标签: javascript angularjs protractor

我刚刚开始使用量角器,并希望能够选择子元素包含特定文本的子元素的父元素。所以鉴于下面的HTML

<ul class="content">
   <li ng-if="case.taskStatus.outstanding &gt; 0" has-permission="task.get" class="task-list ng-scope" ng-repeat="case in cases">
    <h3 class="ng-binding">Case #7000-0000-1060</h3>
    <ul>
      <li error-msg="task.errorMessages.status" error-handler="" ng-if="task.status != 'Completed'" class="task-list-task ng-scope" ng-repeat="task in case.tasks">
        <h4 class="ng-binding">Create physical case file</h4>
        <h4 class="ng-binding"></h4>
        <p>
            <span ng-show="task.assignee" class="ng-binding">Assigned to case viewer</span>
            |
            <span ng-show="task.dueDate" class="ng-binding">To be completed by 28 November 2014</span>
        </p>

        <ul class="task-actions">
            <li has-permission="task-assign.put" class="icon-button">
                <a confirmation-message="task.name" confirmation-type="'task'" opg-click-confirmation="markAsComplete(case.caseId, task, case.tasks, cases)" class="opg-icon ng-isolate-scope" href="">Complete <span class="accessible-text ng-binding">Create physical case file</span></a>
            </li>
            <li has-permission="task-unassign.put" class="icon-button">
                <a ng-click="selectForAllocation(case.caseId, task, case.tasks)" ng-class="{ active: task.selected }" class="opg-icon" href="">Allocate <span class="accessible-text ng-binding">Create physical case file</span></a>
            </li>
        </ul>
    </li>
    </ul>
</li><
</ul>

我希望能够使用文本&#34;创建物理案例文件&#34;来选择其中包含跨度的链接。看起来这应该可以通过量角器的addLocator方法来编写自定义函数来返回该元素。 http://angular.github.io/protractor/#/api?view=ProtractorBy.prototype.addLocator

是否有另一种方法可以做到或者我应该使用addLocator方法。

1 个答案:

答案 0 :(得分:0)

在我的项目中,我不想使用xpath,但这可以解决您的问题:

// Select an element by css + text. Then get the parent.
element(by.cssContainingText('h4', 'Create physical case file'))
  .element(by.xpath('..'))
  .element(by.XXX) // get anything below the parent