0

need help with executing promises. Code:

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");
    });
});

Actual result:

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

The result that i want:

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

I'm not sure how should i solve this, so any help is appreciated.

  • [`.resolve`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/resolve)? – Karl Reid Sep 28 '17 at 10:46
  • 1
    You **can't** make asynchronous code magically synchronous. See the answers to [this question](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) which talk about doing a series of asynchronous actions. In your case, you're looking for an answer about doing them in series (not parallel). You'll also want to read up on how your testing framework handles asynchronous tests. – T.J. Crowder Sep 28 '17 at 10:47
  • As amazing as Javascript is, it can't do time travel :p – Lennholm Sep 28 '17 at 11:04

1 Answers1

1

sendKeys also returns a promise so to ensure things are happening in the order you expect your code would have to look more like:

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");
});

see - https://javascript.info/promise-chaining

Ross Johnson
  • 172
  • 8