1

We have the following problem, we want to store a value from the getAttribute function in a local variable. We try to resolve the promise but it won't work.

return element(by.id('foo')).getAttribute('value');

This will return a promise and we are trying to store the value like

let result = await element(by.id('foo')).getAttribute('value');

But this will only give another promise object back. I also tried to resolve it by chaining promises like this:

static async  getStreetNumberAsync() {
       this.getStreetNumber().then(function(value) {
            return new Promise.resolve(value);
       });
   }

And then again wait for the promise to be resolved but that didn't work either. Can someone explain what I'm doing wrong and how to handle this? The whole code:

static getStreetNumber(){
       return this.MPP().txtStreetNumber.getAttribute('value');
}

static async  getStreetNumberAsync() {
       let value = await this.getStreetNumber();
       return value;
}

static  editMyProfile(){
       let value = this.getStreetNumberAsync();
}

the value of 'value' will be a promise [object promise]

Thanks in advance!

John
  • 728
  • 2
  • 15
  • 37
  • What is the definition of `element` function? If it returns the element asynchronously you can try to wrap the await in bracelets `(await element(by.id('foo'))).getAttribute('value');` – codtex Jul 03 '19 at 06:18
  • Of course, you will get a promise. You need to embrace asynchrony: you must *keep* using `then` or `await` in the surrounding code as well. You cannot expect the promised (future) result to suddenly be available already *now*. That is the whole crux of asynchrony. – trincot Jul 03 '19 at 06:30
  • Value is still a promise, if i do let a = (await element(by.id('foo'))).getAttribute('value');. When I log it to the console I get a list of all functions that the promise contains – John Jul 03 '19 at 06:42

1 Answers1

0

You've already got an async function - use it:

static async getStreetNumberAsync() {
  let value = await this.getStreetNumber();
  return value;
}

Or if you want a Promise:

return new Promise(resolve => resolve(value));
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79