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

时间:2017-01-03 08:56:15

标签: css-selectors protractor

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

我的页面目标文件的代码

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文件的代码

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

这是错误消息

失败: 1)联系簿应该能够搜索已保存的联系人   信息:     失败:无法读取属性' getText'未定义的   堆:     TypeError:无法读取属性' getText'未定义的         在对象。 (C:\用户\ djankowski \应用程序数据\漫游\ NPM \ node_modules \量角器\示例\ contactBook_spec.js:25:35)         在C:\ Users \ djankowski \ AppData \ Roaming \ npm \ node_modules \ protractor \ node_modules \ jasminewd2 \ index.js:94:23         在新的ManagedPromise(C:\ Users \ djankowski \ AppData \ Roaming \ npm \ node_modules \ protractor \ node_modules \ selenium-webdriver \ lib \ promise.js:1082:7)         在controlFlowExecute(C:\ Users \ 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 \ selenium-webdriver \ lib \ promise.js:2913:14)         在TaskQueue.executeNext_(C:\ Users \ djankowski \ AppData \ Roaming \ npm \ node_modules \ protractor \ node_modules \ selenium-webdriver \ lib \ promise.js:2896:21)         在asyncRun(C:\ Users \ djankowski \ AppData \ Roaming \ npm \ node_modules \ protractor \ node_modules \ selenium-webdriver \ lib \ promise.js:2820:25)         在C:\ Users \ djankowski \ AppData \ Roaming \ npm \ node_modules \ protractor \ node_modules \ selenium-webdriver \ lib \ promise.js:639:7         at process._tickCallback(internal / process / next_tick.js:103:7)     来自:任务:在控制流程中运行它("应该能够搜索已保存的联系人")         在对象。 (C:\用户\ djankowski \应用程序数据\漫游\ NPM \ node_modules \量角器\ node_modules \ jasminewd2 \ index.js:79:14)         在C:\ Users \ djankowski \ AppData \ Roaming \ npm \ node_modules \ protractor \ node_modules \ jasminewd2 \ index.js:16:5         在ManagedPromise.invokeCallback_(C:\ Users \ djankowski \ AppData \ Roaming \ npm \ node_modules \ protractor \ node_modules \ selenium-webdriver \ lib \ promise.js:1379:14)         在TaskQueue.execute_(C:\ Users \ djankowski \ AppData \ Roaming \ npm \ node_modules \ protractor \ node_modules \ selenium-webdriver \ lib \ promise.js:2913:14)         在TaskQueue.executeNext_(C:\ Users \ djankowski \ AppData \ Roaming \ npm \ node_modules \ protractor \ node_modules \ selenium-webdriver \ lib \ promise.js:2896:21)         at asyncRun(C:\ Users \ djankowski \ AppData \ Roaming \ npm \ node_modules \ protractor \ node_modules \ selenium-webdriver \ lib \ promise.js:2775:27)     从异步测试:     错误         在套房。 (C:\用户\ djankowski \应用程序数据\漫游\ NPM \ node_modules \量角器\示例\ 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)

我用量角器新鲜了4天,所以这可能是一个愚蠢的错误。

欢迎任何有关代码的其他建议。

谢谢。

2 个答案:

答案 0 :(得分:3)

您的getResult()没有返回elementFinder个对象,因此会引发undefined错误

您需要返回element。请查看以下

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

答案 1 :(得分:0)

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

getText()会返回一个承诺,您已经解决它以获取实际文本。基本上大多数 Protractor API方法都会返回promises.You也可以在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');
});