量角器:已解决的承诺仍然被拒绝

时间:2017-07-07 10:27:50

标签: javascript angularjs promise protractor automated-tests

我是使用Protractor的初学者,我正在使用它为Outlook中的加载项创建简单的测试自动化。
我目前正在测试这些步骤:

  • 打开电子邮件
  • 点击加载项按钮

问题是,在我的一个使用browser.wait的函数(openEmail)中,它返回的承诺在被解析后被拒绝。

utility.js

function waitElement(method, { container, selector, timer }) {
  let _element = container ?
    container.$(selector) : $(selector);

  return browser.wait(
    protractor.ExpectedConditions[method](_element),
    1000 * (timer || 1),
    'Unable to wait for the element'

  ).then((() => _element));
}

isClickable(options) {
  return waitElement('elementToBeClickable', options);
}

outlook.js

let _ = require('./utility.js');

function openEmail(email) {
  _.isClickable({
    selector: email,
    timer: 15

  }).then(() => {
    console.log('OPEN EMAIL - CLICKABLE');
    el.click();

  }).catch(() => {
    throw Error('unable to click email.');
  });
}

function signIn(credentials) {
  let usernameField = $('#cred_userid_inputtext');
  let passwordField = $('#cred_password_inputtext');

  usernameField.sendKeys(credentials.username);
  passwordField.sendKeys(credentials.password);

  _.isClickable({
    selector: '#cred_sign_in_button',
    timer: 5

  }).then(el => {
    console.log('SIGN IN - CLICKABLE');
    el.click();

  }).catch(() => {
    throw Error('unable to click sign in button.');
  });
}

test.js

let outlook = require('./outlook.js');

describe('log in', () => {

  beforeAll(() => {
    browser.ignoreSynchronization = true;
    browser.get('https://outlook.office.com');

    // credentials & cssSelector are somewhere above the file
    outlook.signIn(credentials);
    outlook.openEmail(cssSelector);
  });

  it('should display log in', () => {
    // some tests here
  });

});

在我的终端中,它会记录SIGN IN - CLICKABLEOPEN EMAIL - CLICKABLE,但它也会显示openEmail引发的错误 - unable to click email.我感到困惑,因为{{1} }返回一个承诺,AFAIK一个已解决的承诺不能被拒绝。

1 个答案:

答案 0 :(得分:1)

openEmail(email)方法中,您的el似乎丢失了。我认为你的代码应该是

 function openEmail(email) {
    _.isClickable({
selector: email,
timer: 15

}).then(el => {
console.log('OPEN EMAIL - CLICKABLE');
el.click();

}).catch(() => {
throw Error('unable to click email.');
});
}