【问题标题】:Protractor: Resolved promise is still being rejected量角器:已解决的承诺仍被拒绝
【发布时间】:2017-07-08 00:04:56
【问题描述】:

我是使用 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. 我很困惑,因为browser.wait 返回了一个承诺,而 AFAIK 一个已解决的承诺不能被拒绝。

【问题讨论】:

    标签: javascript angularjs promise protractor automated-tests


    【解决方案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.');
    });
    }
    

    【讨论】:

    • 啊,是的,这就是原因,只是一个愚蠢的错误。谢谢
    猜你喜欢
    • 2023-03-26
    • 2015-09-27
    • 1970-01-01
    • 2015-09-04
    • 2013-06-22
    • 1970-01-01
    • 1970-01-01
    • 2018-04-02
    • 1970-01-01
    相关资源
    最近更新 更多