Home Ask Login Register

Developers Planet

Your answer is one click away!

zalow517 February 2016

Return Promise Value from Page Object using TypeScript

I'm trying to return a value from a WebDriver promise within a Protractor solution using TypeScript, but I'm getting undefined as response.

get nameInput(): string {
    var value: string;
    this.nameElement.getAttribute('value').then(v => value = v);
    return value;

In the above case it seems like the function is not waiting for the promise to return, therefore I tried moving away from the getter style and declared the return type as WebDriver's promise:

getNameInput(): webdriver.promise.Promise<string> {

    var nameElement = element(by.id('name'));
    return nameElement.getText().then(v => { return v });


But I'm getting Function as the return instead of the value of v. Seems like the promise is not being unwrapped by Jasmine's expect, as it happens when running it in JS.

I know I can run the promise directly within the expect, but ideally I would create all the function's logic outside of any expectations, so that I can then feed the expectation the function call with any (if any) parameters only - instead of polluting the test case with promise logic.

Any ideas?


alecxe February 2016

You don't need to resolve the promises, just return them:

getNameInput(): webdriver.promise.Promise<string> {
    var nameElement = element(by.id('name'));
    return nameElement.getText();

Then you need to have a real value returned from the getNameInput() - resolve it in your test:

getNameInput().then(v => { console.log(v) });

Note that, you also let expect() resolve it implicitly:

expect(getNameInput()).toEqual("Expected Value");

Post Status

Asked in February 2016
Viewed 3,695 times
Voted 7
Answered 1 times


Leave an answer

Quote of the day: live life