Eric27710
Eric27710

Reputation: 137

Return a value from a promise within function to the test spec

I'm new to Protractor and I am having issues getting a text value from an Angular form text field and then trying to use the value to perform actions later on within the same test spec.

Here is the test spec code:

it('Demo Test, () => {

        // Submit the form and get the transaction number
        var submitButton = element(by.id('submit'));
        submitButton.click(); 

        // get the generated transaction number from the screen
        let transactionNum = confirmPage.getTransactionNum();


        // would like to use the transActionNum variable to sendkeys into a 
        // text element
        var searchInput = element(by.id('search'));
        searchInput.sendkeys(transactionNum);                
});

Here is some of the code from class that has the function 'getTransactionNum'

var transactionNumValue = element(by.id('transactionNumber'));

public getTransactionNum(): any {

    this.transactionNumValue.get().getText().then((transactionValue: string) 
 => {
    return transactionValue;
});

When I run the test spec and the tests tries to type the value in variable 'transactionNum', I'm getting 'transactionNum is undefined'

I would like to return the transaction number to the test spec as a text value as oppose to a promise.

Thanks for help

Upvotes: 0

Views: 71

Answers (2)

Oleksii
Oleksii

Reputation: 1643

You miss return before this.transactionNumValue, and the get() before get().getText() should be removed.

var transactionNumValue = element(by.id('transactionNumber'));

public getTransactionNum(): any {

    return this.transactionNumValue.getText()
               .then((transactionValue: string)=>{
                      return transactionValue;
               });
}

Upvotes: 1

Bharath Kumar S
Bharath Kumar S

Reputation: 1408

var transactionNumValue = element(by.id('transactionNumber'));



public getTransactionNum(): any { return transactionNumValue.getText().then((transactionValue: string) => { return transactionValue; });

In your class constructor define the locotors for type ElementFinder from protractor.

If it is going to return a array of elements may be $$ or elements.all use type ElementArrayFinder from protractor

Upvotes: 0

Related Questions