Protractor 量角器页面对象定义未按预期工作

Protractor 量角器页面对象定义未按预期工作,protractor,pageobjects,Protractor,Pageobjects,我为这个略显含糊的标题道歉,我不知道该如何准确地表达 我有一个页面对象,除了一个例外,它工作得非常好。以下是摘录: module.exports = function(){ this.facilityList = element(by.name('facility')).all(by.tagName('option')); this.randomFacility = element(by.name('facility')).all(by.tagName('option')).count().th

我为这个略显含糊的标题道歉,我不知道该如何准确地表达

我有一个页面对象,除了一个例外,它工作得非常好。以下是摘录:

module.exports = function(){
this.facilityList = element(by.name('facility')).all(by.tagName('option'));
this.randomFacility = element(by.name('facility')).all(by.tagName('option')).count().then(function(numberOfItems) {
    var rnum = parseInt(Math.random() * numberOfItems);
    return rnum;
}).then(function(randomNumber) {
    element(by.name('facility')).all(by.tagName('option')).get(randomNumber)
});
}

我可以访问并使用
facilityList
很好。但后来我意识到,我几乎总是在
facilityList
上做同样的事情,所以我为什么不创建另一行,让它随机选择一行呢。因此,我使用主conf.js中的代码创建了
randomFacility

它不起作用。我看到显示的错误是:

Failed: Error while waiting for Protractor to sync with the page: "both angularJS testability and angular testability are undefined.  This could be either because this is a non-angular page or because your test involves client-side navigation, which can interfere with Protractor's bootstrapping.  See http://git.io/v4gXM for details"

我很困惑。这是说我不能在page对象中进行所有的处理来获得随机元素,还是说我只需要操纵conf.js中的facilityList并完成它?

您需要了解量角器如何查找元素的机制。当调用量角器的操作时,量角器仅从页面开始查找元素,如
getText()
单击()
计数()

所以,当您定义变量来表示页面上的特定元素时,当NodeJ执行这一行时,量角器不会从页面开始查找元素:

// page object login.page.js
module.exports = function LoginPage(){
   this.sumbitButton = element(by.css('#submit'));
   this.countName = element.all(by.css('.username')).count();
}

// use page object in conf.js
var LoginPage = require('./login.page.js');
var loginPage = new Loginpage();
当Nodejs执行line
var loginPage=new loginPage()时,将执行函数
登录页
中的所有行

当执行第一行时,量角器不从当前打开的页面中查找元素

当执行第二行时,量角器将从当前打开的页面中查找元素,但此时,量角器可以启动带有空白页面的浏览器,目标页面尚未打开或导航到

要解决您的问题,您需要将
随机设施
定义为类的
方法
,而不是
属性

module.exports = function() {
    this.facilityList = element(by.name('facility')).all(by.tagName('option'));

    this.randomFacility = function() {
        return element(by.name('facility'))
            .all(by.tagName('option')).count()
            .then(function(numberOfItems) {
                console.log('count: '+numberOfItems);
                var rnum = parseInt(Math.random() * numberOfItems);
                console.log('random index: '+rnum);
                return rnum;
            })
            .then(function(randomNumber) {
                console.log('argument randomNumber: '+randomNumber);                
                return element(by.name('facility'))
                    .all(by.tagName('option')).get(randomNumber)
            });
    }
};
// how to use
pageObject.randomFacility().then(function(ele){
   return ele.click();
});

这个错误意味着量角器无法找到要同步的
Angular
,但为什么只有在我从这个.randomFacility开始添加行时才会显示它,而当我去掉该行时,整个过程运行得非常完美?请显示您的conf.js,我们需要知道如何调用
randomFacility
函数。我尝试了这个,它说randomFacility()。click()不是一个函数。通过测试日志记录,它进入函数本身,但从未进入
var rnum
行。我在
如何使用
中犯了一个错误,我更新并增强了日志。请再试一次。