简体   繁体   English

量角器-TypeError:未定义不是函数

[英]Protractor - TypeError: undefined is not a function

This question serves two purposes, to try to solve this specific error and to try to understand protractor best practices. 这个问题有两个目的,一个是试图解决这个特定的错误,另一个是了解量角器的最佳实践。 So, I have the following failing code for the test: 因此,我有以下测试失败的代码:

var newRow = element.all(by.repeater("employee in ec.employees")).last();

newRow.then(function(row) {
    row.findElements(by.tagName("td")).then(function(cells) {
        expect(cells.get(0).getText()).toBe("9988776655000");
        expect(cells.get(1).getText()).toBe("Test name");
        expect(cells.get(2).getText()).toBe("Test surname");
    });
});

The reason: 原因:

TypeError: Undefined is not a function TypeError:未定义不是函数

and it's pointing to the 2nd row of code newRow.then(...) . 它指向代码newRow.then(...)的第二行。

My relevant HTML: 我相关的HTML:

<tr ng-repeat="employee in ec.employees">
    <td>{{employee.jmbg}}</td>
    <td>{{employee.name}}</td>
    <td>{{employee.surname}}</td>
</tr>

Now, I've already seen dozens of posts for this error on SO, but all are specific and as far as I could find there wasn't one similar to my case. 现在,我已经看到数十篇关于SO的错误的文章,但是所有文章都是特定的,据我所知,没有一个与我的案例相似。 So, other than the question of why my code doesn't work, I wanted to ask was it better to use ElementFinder.findElement/s function, or is there an alternative? 因此,除了为什么我的代码不起作用的问题外,我想问的是使用ElementFinder.findElement/s函数更好还是有其他选择?

The element.all(...).last() returns an ElementFinder , so you cannot use it like a promise. element.all(...).last()返回ElementFinder ,因此您不能像promise那样使用它。 Change your code to find the elements directly on the newRow variable. 更改代码以直接在newRow变量上查找元素。 Here's how - 这是如何做 -

var newRow = element.all(by.repeater("employee in ec.employees")).last();

newRow.findElements(by.tagName("td")).then(function(cells) {
    expect(cells.get(0).getText()).toBe("9988776655000");
    expect(cells.get(1).getText()).toBe("Test name");
    expect(cells.get(2).getText()).toBe("Test surname");
});

Hope this helps. 希望这可以帮助。

Since you are using bindings in the tags I would recommend looking for those instead of grabbing the rows themselves. 由于您在标记中使用绑定,因此建议您查找那些而不是自己获取这些行。 This way if you change the order of the rows in the future or add additional ones the tests are less likely to break. 这样,如果您将来更改行的顺序或添加其他行,则测试中断的可能性较小。 Try something like the following: 尝试如下操作:

var newRow = element.all(by.repeater("employee in ec.employees")).last();

newRow.then(function(row) {
    expect(row.element(by.binding('employee.jmbg'))).toBe('9988776655000');
    expect(row.element(by.binding('employee.name'))).toBe('Test name');
    expect(row.element(by.binding('employee.surname'))).toBe('Test surname');
});

If you have an Angular directive, binding, repeater, model or a specific css path that you can target you should try and utilize them in your selectors to make your tests more reliable. 如果您具有可以定向的Angular指令,绑定,转发器,模型或特定的CSS路径,则应尝试在选择器中利用它们,以使测试更加可靠。 I would also look into creating PageObjects for your tests to reduce the amount of repeated code that can appear in your tests. 我还将考虑为测试创建PageObjects,以减少测试中可能出现的重复代码量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM