如何执行承诺无耻

时间:2017-09-28 10:44:20

标签: javascript protractor

需要帮助执行承诺。 代码:

it('[TEST-1] Log in with invalid credentials-wrong email', function() {
    //LogIn
    browser.logger.info("[ENTRY] - Username: " + browser.params.Adenity.loginInfo[2].username);
    element(by.id('js-input-email')).sendKeys(browser.params.Adenity.loginInfo[2].username);
    expect(element(by.id('js-input-email')).getAttribute('value')).toEqual(browser.params.Adenity.loginInfo[2].username);
    element(by.id('js-input-email')).getAttribute('value').then(function(pom) {
        if (pom == browser.params.Adenity.loginInfo[2].username)
            browser.logger.info("Username populated succesfully");
        else
            browser.logger.info("Username not populated succesfully");
    });
    browser.logger.info("[ENTRY] - Password: " + browser.params.Adenity.loginInfo[2].password);
    element(by.id('js-input-password')).sendKeys(browser.params.Adenity.loginInfo[2].password);
    expect(element(by.id('js-input-password')).getAttribute('value')).toEqual(browser.params.Adenity.loginInfo[2].password);
    element(by.id('js-input-password')).getAttribute('value').then(function(pom) {
        if (pom == browser.params.Adenity.loginInfo[2].password)
            browser.logger.info("Password populated succesfully");
        else
            browser.logger.info("Password not populated succesfully");
    });
});

实际结果:

[INFO] protractorLog4js - [ENTRY] - Username: oiiuo@iuz.com
[INFO] protractorLog4js - [ENTRY] - Password: Test1234
[INFO] protractorLog4js - Username populated succesfully
[INFO] protractorLog4js - Password populated succesfully

我想要的结果:

[INFO] protractorLog4js - [ENTRY] - Username: oiiuo@iuz.com
[INFO] protractorLog4js - Username populated succesfully
[INFO] protractorLog4js - [ENTRY] - Password: Test1234
[INFO] protractorLog4js - Password populated succesfully

我不知道应该如何解决这个问题,所以我们非常感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

sendKeys还会返回一个承诺,以确保按照您希望代码看起来更像的顺序发生事情:

element(by.id('js-input-email')).sendKeys(browser.params.Adenity.loginInfo[2].username).then(function() {
    return element(by.id('js-input-email')).getAttribute('value');
}).then(function(pom) {
    if (pom == browser.params.Adenity.loginInfo[2].username)
        browser.logger.info("Username populated succesfully");
    else
        browser.logger.info("Username not populated succesfully");

    browser.logger.info("[ENTRY] - Password: " + browser.params.Adenity.loginInfo[2].password);
    return element(by.id('js-input-password')).sendKeys(browser.params.Adenity.loginInfo[2].password)
}).then(function() {
    expect(element(by.id('js-input-password')).getAttribute('value')).toEqual(browser.params.Adenity.loginInfo[2].password);
    return element(by.id('js-input-password')).getAttribute('value')
}).then(function(pom){
    if (pom == browser.params.Adenity.loginInfo[2].password)
        browser.logger.info("Password populated succesfully");
    else
        browser.logger.info("Password not populated succesfully");
});

请参阅 - https://javascript.info/promise-chaining