【问题标题】:Protractor click element by repeater中继器的量角器单击元素
【发布时间】:2015-06-30 22:43:21
【问题描述】:

我正在尝试通过转发器单击列表 <li> 中的第一个元素 我的 HTML 看起来像:

<ul class="list-unstyled" ng-show="axCarSearch.found.length">
                    <!-- ngRepeat: car in axCarSearch.found --><li ng-repeat="car in axCarSearch.found" viewport-check="$index==axCarSearch.current" class="ng-scope">
                        <a class="clearfix current" ng-mousedown="axCarSearch.select(car)" ng-class="{current: $index==axCarSearch.current}">
                            <!-- ngRepeat: m in car.match track by $index --><!-- ngIf: $odd -->
                            <!-- ngIf: $even --><span ng-repeat-end="" ng-if="$even" ng-bind="m" class="ng-binding ng-scope">VW</span><!-- end ngIf: $even --><!-- end ngRepeat: m in car.match track by $index --><!-- ngIf: $odd --><strong ng-repeat-start="m in car.match track by $index" ng-if="$odd" class="text-primary ng-binding ng-scope" ng-bind="m">Passat</strong><!-- end ngIf: $odd -->
                            <!-- ngIf: $even --><!-- end ngRepeat: m in car.match track by $index --><!-- ngIf: $odd -->
                            <!-- ngIf: $even --><span ng-repeat-end="" ng-if="$even" ng-bind="m" class="ng-binding ng-scope">05/07-10/10 (3C)</span><!-- end ngIf: $even --><!-- end ngRepeat: m in car.match track by $index -->
                            <!-- ngIf: !car.match -->
                            <small class="pull-right text-muted ng-binding">Typ</small>
                        </a>
                    </li><!-- end ngRepeat: car in axCarSearch.found --><li ng-repeat="car in axCarSearch.found" viewport-check="$index==axCarSearch.current" class="ng-scope">
                        <a class="clearfix" ng-mousedown="axCarSearch.select(car)" ng-class="{current: $index==axCarSearch.current}">
                            <!-- ngRepeat: m in car.match track by $index --><!-- ngIf: $odd -->
                            <!-- ngIf: $even --><span ng-repeat-end="" ng-if="$even" ng-bind="m" class="ng-binding ng-scope">VW</span><!-- end ngIf: $even --><!-- end ngRepeat: m in car.match track by $index --><!-- ngIf: $odd --><strong ng-repeat-start="m in car.match track by $index" ng-if="$odd" class="text-primary ng-binding ng-scope" ng-bind="m">Passat</strong><!-- end ngIf: $odd -->
                            <!-- ngIf: $even --><!-- end ngRepeat: m in car.match track by $index --><!-- ngIf: $odd -->
                            <!-- ngIf: $even --><span ng-repeat-end="" ng-if="$even" ng-bind="m" class="ng-binding ng-scope">10/00-05/05 (3B3/3B6)</span><!-- end ngIf: $even --><!-- end ngRepeat: m in car.match track by $index -->
                            <!-- ngIf: !car.match -->
                            <small class="pull-right text-muted ng-binding">Typ</small>
                        </a>
                    </li><!-- end ngRepeat: car in axCarSearch.found --><li ng-repeat="car in axCarSearch.found" viewport-check="$index==axCarSearch.current" class="ng-scope">
                        <a class="clearfix" ng-mousedown="axCarSearch.select(car)" ng-class="{current: $index==axCarSearch.current}">
                            <!-- ngRepeat: m in car.match track by $index --><!-- ngIf: $odd -->
                            <!-- ngIf: $even --><span ng-repeat-end="" ng-if="$even" ng-bind="m" class="ng-binding ng-scope">VW</span><!-- end ngIf: $even --><!-- end ngRepeat: m in car.match track by $index --><!-- ngIf: $odd --><strong ng-repeat-start="m in car.match track by $index" ng-if="$odd" class="text-primary ng-binding ng-scope" ng-bind="m">Passat</strong><!-- end ngIf: $odd -->
                            <!-- ngIf: $even --><!-- end ngRepeat: m in car.match track by $index --><!-- ngIf: $odd -->
                            <!-- ngIf: $even --><span ng-repeat-end="" ng-if="$even" ng-bind="m" class="ng-binding ng-scope">10/96-09/00 (3B2/3B5)</span><!-- end ngIf: $even --><!-- end ngRepeat: m in car.match track by $index -->
                            <!-- ngIf: !car.match -->
                            <small class="pull-right text-muted ng-binding">Typ</small>
                        </a>
                    </li>
                </ul>

您可以看到它的某种汽车列表,其中每个&lt;li&gt; 代表其中一辆。我基本上尝试点击一个特定的(从第一个开始)

我试着写这样的东西:

element(by.repeater('car in axCarSearch.found').row(0)).element(by.css('[ng-mousedown="axCarSearch.select(car)"]')).click();

很遗憾,我收到一个错误:

使用定位器找不到元素:by.repeater(car in axCarSearch.found").row("0")"

有没有人可以帮我找到点击&lt;li&gt;的方法?

【问题讨论】:

  • 您可以尝试添加以下element.all(by.repeater('car in axCarSearch.found')).count().then(function (count) { console.log(count); });,运行测试并查看控制台上的输出吗?谢谢。
  • 嗨@alecxe它返回正确的选项数量,在这种情况下它是3

标签: testing protractor end-to-end


【解决方案1】:

请尝试在ElementArrayFinder 上调用first():

var rows = element.all(by.repeater("car in axCarSearch.found"));

rows.first().element(by.tagName("a")).click();

【讨论】:

  • 感谢alecxe的回答,像这样可以调用其他东西然后“第一个”和“最后一个”,我找到了一种方法如何通过:元素(by.css(' [viewport-check="$index==axCarSearch.current"]')).click();这是一些特定的页面不是一个好的解决方案。
  • @Andurit 是的,你可以使用get(index),而不是first() 和last()。我同意,您使用的定位器不是很可靠和可读。提出的by.tagName() 有效吗?谢谢。
  • 完美运行,只是弹出效果有些问题,所以必须添加短睡眠 browser.sleep(1000);感谢您的建议
猜你喜欢
  • 2020-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-11
  • 1970-01-01
相关资源
最近更新 更多