why am i getting TypeError: Cannot read property 'getText' of undefined error even though my web element reference is correct?

why am i getting TypeError: Cannot read property 'getText' of undefined error even though my web element reference is correct?

我在 运行 量角器测试时收到错误消息。

这是对 directories.page.js 文件中网络元素的引用:

get importErrorsList(){
    return element.all(by.css('[ng-if="error.error.detailMessage"]'));
}

这是网络元素的屏幕截图及其源信息: 最后这是我用来引用网络元素的步骤,它返回错误:

Then(/^The list of import errors contains this error message: "([^"]*)"$/, function (errorText, callback) {
        browser.wait(EC.visibilityOf(importPageObj.alertMsg), timeouts.EC_TIMEOUT).then(function(){
            browser.wait(() => {  
//the next line causes the error 
expect(directoriesPageObj.importErrorsList.getText()).to.eventually.contain(errorText).and.notify(callback);
                }, timeouts.EC_TIMEOUT).then(() => {
                browser.wait(EC.and(EC.visibilityOf(importPageObj.headerDropDownInWebView), EC.elementToBeClickable(importPageObj.headerDropDownInWebView)), timeouts.EC_TIMEOUT).then(() => {
                    callback();
                });
            });
        });
    });

谁能帮我弄清楚为什么这不起作用?

您没有初始化 directoriesPageObj。所以,在使用它之前你应该做:

const directoriesPageObj = new DirectoriesPageObj()

之后你可以使用 directoriesPageObj 变量。例如:

Then(/^The list of import errors contains this error message: "([^"]*)"$/, function (errorText, callback) {
        browser.wait(EC.visibilityOf(importPageObj.alertMsg), timeouts.EC_TIMEOUT).then(function(){
            browser.wait(() => {  
const directoriesPageObj = new DirectoriesPageObj();
expect(directoriesPageObj.importErrorsList.getText()).to.eventually.contain(errorText).and.notify(callback);
                }, timeouts.EC_TIMEOUT).then(() => {
                browser.wait(EC.and(EC.visibilityOf(importPageObj.headerDropDownInWebView), EC.elementToBeClickable(importPageObj.headerDropDownInWebView)), timeouts.EC_TIMEOUT).then(() => {
                    callback();
                });
            });
        });
    });