0

Please advise what could be problem here.

userLogin is provided properly at the end of test but password has value 'undefined'. Seems that function takePasswordFromNotification() doesn't return relevant value from notification - why ? The flow is that after account creation the pop-up notification temporarily appears with assigned login and password and I want to take login and password (which is generated randomly) from notification in order to logout and login to system using login. The rest of code works fine apart of this function.

fit('should create new user and log in by his account', function () {
    var userId = Math.random().toString(36).substr(2, 20);
    havingTestUser(userId);
    var userLogin = givenAccountOfUser(userId);
    console.log(userLogin);
    var newPassword = takePasswordFromNotification();
    console.log(newPassword);
    welcomePage.logout('newly created (deleted) account');
    browser.driver.get(browser.params.rootUrl);
    browser.wait(EC.presenceOf(loginPage.userLogin), WAIT_TIMEOUT, 'login input field was not present');
    loginPage.userLogin.sendKeys(userLogin);
    loginPage.userPassword.sendKeys(newPassword);
    dv.sleep(10000);
    clickWithWait(loginPage.signInButton);
    expectVisible(welcomePage.usersButton, 'users button after login by new account');
});

    function givenAccountOfUser(userId) {
        userDetailsPage.goTo(userId);
        browser.executeScript('window.scrollTo(0,0);');
        goToLoginInput();
        var userLogin = Math.random().toString(36).substr(2, 20);
        userDetailsPage.loginInputField.sendKeys(userLogin);
        expectClickable(userDetailsPage.saveAccountButton, 'save account button');
        clickWithWait(userDetailsPage.saveAccountButton);
        expectVisible(userDetailsPage.successNotification, 'success notification');
        return userLogin;
    }

function takePasswordFromNotification() {
    userDetailsPage.successNotification.getText().then(function (text) {
        var expectedPasswordLength = 12;
        var newPassword = text.substring(text.length - expectedPasswordLength, text.length);
        return newPassword;
    });}
Michal
  • 563
  • 3
  • 14
  • 24
  • There is no `return` statement in `takePasswordFromNotification`. Functions without return statement implicitly return `undefined`. – Felix Kling Nov 02 '15 at 14:21
  • @FelixKling Return statement is at the end of promise , but maybe I did not understand you well, could you please correct code ? It will be the most efficient way to explain what's wrong. – Michal Nov 02 '15 at 14:23
  • The return statement is inside the callback you pass to `then`. This has no effect on the outer function. Here is a simplified example: `function foo() { function bar() { return 42; }; return 21; }`. If you call `foo()`, what value do you expect to be returned? The solution to your problem is described in the linked question. In short, you have to restructure your code so that it works asynchronously. – Felix Kling Nov 02 '15 at 14:27
  • Since you are already aware of what promises are, this should be not a problem for you. – Felix Kling Nov 02 '15 at 14:31
  • @FelixKling OK but variables of javascript functions are internal so in my case `newPassword` can't be called outside the `then` statement in that case. So how to return it outside the callback ? Again ... could you please provide code with solution ? – Michal Nov 02 '15 at 14:43
  • `takePasswordFromNotification` should return the promise, i.e. `return userDetailsPage(...).then(...);`, which the calling code can then use to get the value: `takePasswordFromNotification().then(function(newPassword) { /* here goes the code that needs newPassword */ });`, just as explained in the linked question. You might have to read up again on asynchronous tests in protractor (I'm not familiar with it). – Felix Kling Nov 02 '15 at 14:55
  • solved .... I've missed `return` before the first line of this function :) :) :) – Michal Nov 02 '15 at 15:28

0 Answers0