量角器:由转发器元素所有返回的大小为0

时间:2018-08-31 04:32:17

标签: angularjs jasmine protractor

我对量角器来说还很陌生,尝试学习自动化角度主页(https://angularjs.org/)。我在 JavaScript项目部分中遇到了麻烦。

可行的步骤:

it ('should click on the plus sign', function() {
        $('.icon-plus-sign').click();
            expect(element(by.model('editProject.project.name')).isPresent()).toBe(true);
});

it ('should fill up the form', function() {
            element(by.model('editProject.project.name')).sendKeys('Test Name');
            element(by.model('editProject.project.site')).sendKeys('https://www.testsite.com');
            element(by.model('editProject.project.description')).sendKeys('Test Description');
            expect(element(by.buttonText('Save')).getAttribute('disabled')).toBe(null);
});

失败的步骤:

it ('should click on save button', function() {
            element(by.buttonText('Save')).click();
            // $$('tr[class="ng-scope"]')
            element.all(by.repeater("project in projectList.projects | filter:projectList.search | orderBy:'name'")).then(function(trElements) {
                console.log(trElements.length);
                for (var i = trElements.length - 1; i >= 0; i--) {
                    console.log('outside if' + i);
                    if (trElements[i].all(by.tagName('td')).first().element(by.tagName('a')).getText() == 'Test Name') {
                        console.log('inside if' + i);
                        expect(trElements[i].all(by.tagName('td')).first().element(by.tagName('a')).getAttribute('ng-href')).toContain('www.testsite.com');
                        expect(trElements[i].all(by.tagName('td')).get(1).getText()).toBe('Test Description');
                        break;
                    }
                }
            });


            browser.sleep(5000);
});

trElements.length 返回 0 ,但是元素肯定存在于DOM中,并在Chrome DevTools的“元素”选项卡中突出显示。

请帮助我。

谢谢!

2 个答案:

答案 0 :(得分:1)

单击“保存”后,也许您应该等待列表被加载。测试可能比加载元素更快,在这种情况下,我们使用期望条件并等待列表可见(您在https://www.protractortest.org/#/api?view=ProtractorExpectedConditions处有完整的API文档):

var EC = protractor.ExpectedConditions;
element(by.buttonText('Save')).click().then(function() {
        // $$('tr[class="ng-scope"]')
   browser.wait(EC.visibilityOf(element(by.repeater("project in projectList.projects | filter:projectList.search | orderBy:'name'"))), 30000, "Project list is not displayed");
        element.all(by.repeater("project in projectList.projects | filter:projectList.search | orderBy:'name'")).then(function(trElements) {
            console.log(trElements.length);
            for (var i = trElements.length - 1; i >= 0; i--) {
                console.log('outside if' + i);
                if (trElements[i].all(by.tagName('td')).first().element(by.tagName('a')).getText() == 'Test Name') {
                    console.log('inside if' + i);
                    expect(trElements[i].all(by.tagName('td')).first().element(by.tagName('a')).getAttribute('ng-href')).toContain('www.testsite.com');
                    expect(trElements[i].all(by.tagName('td')).get(1).getText()).toBe('Test Description');
                    break;
                }
            }
        });
});

答案 1 :(得分:0)

在单击“ trElements”之前,单击“保存”启动的操作尚未完成。在单击“保存”按钮时,请等待返回的承诺。即 element(By.buttonText("Save")).click() .then(() => { element.all(By.repeater(... });