使用量角器从数组测试中获取数据

时间:2015-10-08 06:48:26

标签: javascript angularjs ionic-framework ionic protractor

我用对象创建了一个数组,它有不同的值,我需要测试特定数组的内容,所以我用了

var child  =   element.all(by.css('.col.col-top.col-67')),element.all(by.css('.ng-binding'));

expect(child.get(1)).toEqual('HAL 9000'); 

这会在我的终端发送错误消息"致命错误:CALL_AND_RETRY_LAST分配失败 - 处理内存不足 中止陷阱:6" 实际上我需要检查html页面中是否显示了hal 9000

<a class="item-content" ng-href="#/lead/details/1/" target="_self" href="#/lead/details/1/">

                <div class="row" style="height: 35px; width: 100%; margin-left:-10px; margin-top: -10px; margin-right: 0px; padding: 0px"> 
                    <div class="col col-top col-67">
                        <h2 class="ng-binding">HAL 9000</h2>
                        <br>
                        <h4 style="font-weight: normal; margin-top: -15px" class="ng-binding">Jupiter &nbsp; Feb 10, 2025 </h4>
                    </div>

                    <div class="col col-center col-10 col-offset-25" style="margin-right: -10px">
                        <a href="tel:9876543210" ng-click="$event.stopPropagation()">
                            <i class="icon ion-ios-telephone-outline" style="font-size: 36px"></i>                       
                        </a>
                    </div>
                    <div class="col col-center col-20" style="margin-left: 15px">
                        <a href="mailto:hai@spaceodyssey.com?Subject=Hi;" ng-click="$event.stopPropagation()">
                            <i class="icon ion-ios-email-outline" style="font-size: 36px;"></i>
                        </a>
                    </div>
                </div>



            </a>

2 个答案:

答案 0 :(得分:0)

element.all()无法附加到element.all()的实例。此外,您使用,代替.尝试将.ng-binding划分为一种方式,以便您无需使用数组即可获得它。如果必须获取.col.col-top.col-67下的所有元素,请使用.each()函数获取该类的每个div,然后使用每个div获取.ng-binding的所有元素。这是怎样的 -

element.all(by.css('.col.col-top.col-67')).each(function(eachDiv){ //get each div with the specified class
    var child = eachDiv.$$('.ng-binding'); //get all the elements with ng-binding under each div
    expect(child.get(1)).toEqual('HAL 9000'); //add your expectations
});

注意:当您使用each()时,它使用该元素定位器循环遍历所有元素,就像for循环一样。假设有许多元素包含类.col.col-top.col-67,并且其下有许多元素,类为.ng-binding

如果只有一个元素,那么您不必在上面的代码中使用element.all()。希望它有所帮助

答案 1 :(得分:0)

感谢您的帮助,我稍微修改了Girish Sortur提供的上述代码

            var lItems = element.all(by.css('.col.col-top.col-67'));
            var child = lItems.get(1).$$('.ng-binding'); //get all the elements with ng-binding under each div
            expect(child.get(0).getText()).toEqual('HAL 9000'); //add your expectations