当元素未显示/存在时,量角器无法执行我的代码的其他部分

时间:2016-08-12 06:42:51

标签: protractor

我总是使用isDisplayed / isPresent在屏幕上标识元素,然后添加if / else以执行进一步测试。但是,屏幕的一部分永远不会被执行并得到错误,例如"失败:索引越界。尝试访问index:0处的元素,但只有0个元素匹配定位器By(css selector,.noga-selected-summary.list.ng-scope.layout-row)"在控制台日志上。

Test_PO.js如下,

var ProfilePO = function(){
this.Setting = element.all(by.css('.md-icon-button.md-button.md-dark-theme.md-ink-ripple')).get(1);
this.SettingSubMenus = element.all(by.css('.md-tab.ng-scope.ng-isolate-scope.md-ink-ripple'));
//this.ReqProductLabel = element(by.css('[ng-show="ngModel && ngModel.length > 0"]'));
this.BusinessPage = element(by.css('[ng-model="required_categories"]'));
this.AddProductButton = element(by.css('[ng-click="addCategory()"]'));
this.AddedProdCat = element.all(by.css('.noga-selected-summary.list.ng-scope.layout-row'));
this.DeleteAddedProd = element.all(by.css('[ng-click="removeCategory(category)"]'));};
 module.exports = ProfilePO;

test_spec.js如下,

    it('Verify user can add required products on business screen using Find Product or Service Dropdown', function() {
    Profile.AddedProdCat.get(0).isDisplayed().then(function(AddedProdCatIsDisplayed){
        console.log('Added Prod Cat Is Displayed: ' + AddedProdCatIsDisplayed);
        if (AddedProdCatIsDisplayed) {
            Profile.AddedProdCat.count().then(function(count){
                var Count1 = count;
                var C1 = Count1-1;
                console.log('Product list has ' + count + ' products');
                for (var i=0, j=0; i <= C1 ; i++) {
                    Profile.DeleteAddedProd.get(j).click();
                    console.log('deleted ' + (i+1) + ' product');
                    browser.sleep(2000);
                }
            });
        } else {
            FuncLib.NogaList.isDisplayed().then(function(NogaListIsDisplayed) {
                console.log('Find Product or Service Dropdown Is Displayed: ' + NogaListIsDisplayed);
                if (NogaListIsDisplayed) {
                    FuncLib.SltNogaCat("A011100"); //select Noga
                    Profile.AddProductButton.isDisplayed().then(function (AddProdButtonDisplayed){
                        console.log('Add product button is displayed: ' + AddProdButtonDisplayed);
                        Profile.AddProductButton.click();
                        browser.sleep(3000);
                        Profile.AddedProdCat.isDisplayed().then(function(AddedProdCatIsDisplayed){
                            console.log('Added Prod Cat Is Displayed: ' + AddedProdCatIsDisplayed);
                            expect(Profile.AddedProdCat.getText()).toEqual('A011100');
                        });
                    });
                } else
                    console.log('Noga Catagory dropdown is not displayed');
            });
        }
    });
});

当&#34;添加产品类别列表&#34;在屏幕上可用,这个脚本很好用,但如果我没有添加产品类别列表,它会返回上面提到的错误。我尝试使用isPresent而不是isDisplayed,但我仍然得到相同的错误。请告诉我,我需要做些什么来处理这个错误?

2 个答案:

答案 0 :(得分:0)

我认为你的this.AddedProdCat需要一些时间才能显示出来。

您可以使用Expected Conditions等待该元素,就像这样 -

it('Verify user can add required products on business screen using Find Product or Service Dropdown', function() {

var EC = protractor.ExpectedConditions;
browser.wait(EC.visibilityOf(Profile.AddedProdCat.get(0)), 5000);
Profile.AddedProdCat.get(0).then(function(firstElement){
     console.log('Added Prod Cat firstElement: ' + firstElement);
     if (firstElement) {
        Profile.AddedProdCat.count().then(function(count){
            var Count1 = count;
            var C1 = Count1-1;
            console.log('Product list has ' + count + ' products');
            for (var i=0, j=0; i <= C1 ; i++) {
                Profile.DeleteAddedProd.get(j).click();
                console.log('deleted ' + (i+1) + ' product');
                browser.sleep(2000);
            }
        });
    }
}); 

答案 1 :(得分:0)

您使用的是什么版本的量角器?这应该从3.3.0 - https://github.com/angular/protractor/commit/bd78dfc79b1435d124c994482df6879066079a4d

开始修复