Javascript 无法读取量角器中元素的未定义属性“click”

Javascript 无法读取量角器中元素的未定义属性“click”,javascript,jasmine,protractor,Javascript,Jasmine,Protractor,我已经编写了一个使用量角器登录应用程序的函数 this.login = function(userName, password){ return this.emailAddressInput.sendKeys(userName).then(function(){ return this.nextButton.click().then(function(){ return this.passwordInput.sendKeys(password).t

我已经编写了一个使用量角器登录应用程序的函数

this.login = function(userName, password){
    return this.emailAddressInput.sendKeys(userName).then(function(){
        return this.nextButton.click().then(function(){
            return this.passwordInput.sendKeys(password).then(function(){
                 this.tibcoLoginButton.click().then(function(){
                     return require('./tce.apps.js') 
                 })

            })
        })
我正在调用我的一个spec文件中的函数

但我得到的错误是:

失败:无法读取未定义的属性“单击”

这是下一个按钮的if。为什么会发生这种情况?

this.nextButton未定义,因为在.then方法中的“this”在词汇上指的是承诺对象。

试试这个

this.login = function(userName, password){
    var self = this;
    return self.emailAddressInput.sendKeys(userName).then(function(){
        return self.nextButton.click().then(function(){
            return self.passwordInput.sendKeys(password).then(function(){
                 return self.tibcoLoginButton.click().then(function(){
                     return require('./tce.apps.js') 
                 })
            })
        })
    })
}
然后就不需要使用嵌套的,但要按照下面的步骤来做,以提高可读性

this.login = function(userName, password) {
  var self = this;
  self.emailAddressInput.sendKeys(userName);
  self.nextButton.click();
  self.passwordInput.sendKeys(password);

  return self.tibcoLoginButton.click().then(function() {
    return require('./tce.apps.js')
  });
}
您不需要使用then函数来编写登录函数。您可以尝试创建登录页面对象页面,如下所示:

var EC = protractor.ExpectedConditions;

this.getUserInput = function () {
        return user_input;
    };
this.getPasswordInput = function () {
        return password_input;
    };
this.setUserInput = function (text) {
        user_input.clear().sendKeys(text);
    };
this.setPasswordInput = function (text) {
        password_input.clear().sendKeys(text);
    };
this.getLoginButton = function () {
        return login_button;
    };
    this.clickLogin = function () {
        login_button.click();
    };
this.login = function(username, password) {
    browser.wait(EC.presenceOf(this.getUserInput()));
    browser.wait(EC.presenceOf(this.getPasswordInput()));
    this.getUserInput().click();
    this.setUserInput(username);
    this.getPasswordInput().click();
    this.setPasswordInput(password);
    browser.wait(EC.elementToBeClickable(this.getLoginButton()));
    this.clickLogin();
  };

这不是你所想的。抱歉..我没有得到..console.logthis,this.nextButton函数正在更改它。闭包的基本概念。是的,是的,我知道了。有办法处理吗?是的,我现在知道了。有没有办法处理这件事。?