首页 文章

TypeError:无法读取未定义的属性'getText'

提问于
浏览
0

我坚持这个错误: TypeError: Cannot read property 'getText' of undefined 当我在Protractor v.4.0.14中运行我的测试时我理解的是getText()无法从表格单元格中获取文本,因为CSS选择器没有正确地选择它或者我使用错误的方法从单元格中检索文本?!

我的页面 object file's code

var HomePage = function(){

    //methods
    this.clearForm = function(){
        element(by.css("#nameInput")).clear();
        element(by.css("#surnameInput")).clear();
        element(by.css("#emailInput")).clear();
        element(by.css("#phoneInput")).clear();
    };

    this.fillForm = function(name, surname, email, phone){
        element(by.css("#nameInput")).sendKeys(name);
        element(by.css("#surnameInput")).sendKeys(surname);
        element(by.css("#emailInput")).sendKeys(email);
        element(by.css("#phoneInput")).sendKeys(phone);
    };

    this.clickSave = function(){
        element(by.css("#saveBTN")).click();
    };

    this.clickSearch = function(){
        element(by.css("#searchBTN")).click();
    };

    this.getResult = function(row, column){
        element(by.css("#result > tr:nth-child("+row+") > td:nth-   child("+column+")"));
    };

    this.clickEdit = function(row){ 
        element(by.css("input.button-primary:nth-child("+row+")")).click();
    };

    this.clickRemove = function(row){
        element(by.css("input.button:nth-child("+row+")")).click();
    };

    this.clickDeleteLocalStorage = function(){
        element(by.css("#delAllBTN")).click();
    };

};
module.exports = new HomePage();

这是 spec file's code

describe("Contact book", function(){

    var page = require('./page/home_page.js');

    //input data
    var name = 'Vladimir';
    var surname = 'Putin';
    var email = 'ilovekgb@ivan.ru';
    var phone = '+01 1234 567';

    beforeEach(function(){
        browser.ignoreSynchronization = true;
        browser.get("https://ddaawwiidd.github.io/contactbook/");
    });

    it("Should be able to save new contact", function(){
        page.fillForm(name, surname, email, phone);
        page.clickSave();
    });

    it("Should be able to search for saved contact", function(){
        page.clearForm();
        page.fillForm(name, surname, email, phone);
        page.clickSearch();
        var result = page.getResult(1,1).getText(); //that's the part causing the error
        expect(result).toContain('Vladimir');
    });

    it("Should be able to edit contact details", function(){
        page.fillForm(name, surname, email, phone);
        page.clickSearch();
        page.clickEdit(1);
        page.clearForm();
        page.fillForm('Barack', 'Obama', email, phone);
        page.clickSave();
    });

    it("Should be able to remove contact", function(){
        page.fillForm('Barack','','','');
        page.clickSearch();
        page.clickRemove(1);
    });

    it("Should be able to list all saved contacts", function(){
        page.clearForm();
        page.clickSearch();
    });

    xit("Should be able to delete localStorage", function(){
        page.clickDeleteLocalStorage(); 
        expect(page.getFirstRowResult().isDisplayed()).toBe(false);
    });


 });

这是HTML的截图:
HTML screenshot

这是 error message

失败:1)联系簿应该能够搜索已保存的联系人消息:失败:无法读取未定义的属性'getText'Stack:TypeError:无法在Object处读取未定义的属性'getText' . C:\ Users \ djankowski \ AppData \ Roaming \ npm \ node_modules \ protractor \ node_modules \ jasminewd2 index.js:94:23在新ManagedPromise(C:\用户\ djankowski \应用程序数据\漫游\ NPM \ node_modules \量角器\ node_modules \硒的webdriver \ lib中\ promise.js:1082:7)在controlFlowExecute(C:\用户\ djankowski \ AppData \ Roaming \ npm \ node_modules \ protractor \ node_modules \ jasminewd2 \ index.js:80:18)在TaskQueue.execute_(C:\ Users \ djankowski \ AppData \ Roaming \ npm \ node_modules \ protractor \ node_modules \硒的webdriver \ lib中\ promise.js:2913:14)在TaskQueue.executeNext_(C:\用户\ djankowski \应用程序数据\漫游\ NPM \ node_modules \量角器\ node_modules \硒的webdriver \ lib中\ promise.js:2896: 21)在asyncRun(C:\用户\ djankowski \应用程序数据\漫游\ NPM \ node_modules \量角器\ node_modules \硒的webdriver \ lib中\ promise.js:2820:25),在C:\用户\ djankowski \应用程序数据\漫游\ NPM \ node_modules \量角器\ node_modules \硒的webdriver \ LIB \ promise.js:639:7 at process.tickCallback(internal / process / next_tick.js:103:7)来自:任务:在对象的控制流中运行它(“应该能够搜索已保存的联系人”) . (C:\ Users \ djankowski \ AppData \ Roaming \ npm \ node_modules \ protractor \ node_modules \ jasminewd2 \ index.js:79:14)位于C:\ Users \ djankowski \ AppData \ Roaming \ npm \ node_modules \ protractor \ node_modules \ jasminewd2 \ index.js:16:5,位于TaskQueue的ManagedPromise.invokeCallback(C:\ Users \ djankowski \ AppData \ Roaming \ npm \ node_modules \ protractor \ node_modules \ selenium-webdriver \ lib \ promise.js:1379:14) . 在TaskQueue.executeNext_执行execute_(C:\ Users \ djankowski \ AppData \ Roaming \ npm \ node_modules \ protractor \ node_modules \ selenium-webdriver \ lib \ promise.js:2913:14)(C:\ Users \ djankowski \ AppData \ Roaming \ npm \ node_modules \ protractor \ node_modules \ selenium-webdriver \ lib \ promise.js:2896:21)atyncRun(C:\ Users \ djankowski \ AppData \ Roaming \ npm \ node_modules \ protractor \ node_modules \ selenium-webdriver \ lib \ promise.js:2775:27)从异步测试:Suite出错 . 在Object处的(C:\ Users \ djankowski \ AppData \ Roaming \ npm \ node_modules \ protractor \ example \ contactBook_spec.js:21:2) . (C:\用户\ djankowski \应用程序数据\漫游\ NPM \ node_modules \量角器\示例\ contactBook_spec.js:1:1)在Module._compile(module.js:570:32)在Object.Module._extensions..js (module.js:579:10)在Module.load(module.js:487:32)的tryModuleLoad(module.js:446:12)

我用Protractor做了4天,所以这可能是一个愚蠢的错误 .

关于代码的任何其他建议也是受欢迎的 .

谢谢 .

2 回答

  • 0

    你的 getResult() 不会返回一个 elementFinder 对象,因此会抛出 undefined 错误

    你需要返回 element . 请检查下面

    this.getResult = function(row, column){
            return element(by.css("#result > tr:nth-child("+row+") > td:nth-   child("+column+")"));
        };
    
  • 3

    除了@AdityaReddy的回答,还有几点要强调:

    getText() 返回一个承诺,您已经解析它以获取实际文本 . 基本上大多数 Protractor API methods 都会返回承诺 . 您也可以在 spec.js 中执行类似的操作 -

    it("Should be able to search for saved contact", function(){
        page.clearForm();
        page.fillForm(name, surname, email, phone);
        page.clickSearch();
    
        page.getResult(1,1).getText().then(function (text) { 
        expect(text).toContain('Vladimir');
     }); 
     // or you could directly use expect which resolves the promise internally.
    
        expect(page.getResult(1,1).getText()).toContain('Vladimir');
    });
    

相关问题