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

getting data from an array test using protractor

本文关键字:获取 数据 测试 数组 量角器      更新时间:2023-09-26

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

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"实际上,我需要检查哈尔9000是否显示在HTML页面

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

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()。希望能有所帮助

谢谢你的帮助,我只是稍微修改了上面的代码由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