0

This function returns the undefined instead of the size of an element. I couldn't write it in another way since I'm not that familiar with promises. How do I rewrite it so it doesn't return undefined but a resolved promise?

console: Expected undefined to be less than undefined.

var s1 = utilitiesPageObject.getElemSize("css", "#main-content > div > div > div.col-xs-12.col-md-6.align-left");
driver.findElement(By.css('#main-content > div > div > div.col-xs-12.col-md-6.align-left > div.slide-down-container > div.slide-down-btn.btn')).click()
var s2 = utilitiesPageObject.getElemSize("css", "#main-content > div > div > div.col-xs-12.col-md-6.align-left")
expect(s1).toBeLessThan(s2);

and from utilitiesPageObject the function getElemSize

getElemSize(css, elem){
    switch(css) {
        case 'css':
            this.driver.findElement(By.css(elem)).getSize().getHeight.then(s => {return s});
            break;
        case 'xpath':
            this.driver.findElement(By.xpath(elem)).getSize().getHeight.then(s => {return s});
            break;
        default:
            return null;
    }
}
Fenrir
  • 231
  • 2
  • 18
  • Could you post the HTML, where you are looking for the elements `"#main-content > div > div > div.col-xs-12.col-md-6.align-left`? – DomeTune Jan 31 '17 at 10:30
  • Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Andreas Jan 31 '17 at 10:32
  • @Andreas Where you found an asynchronous call in the question ? – DomeTune Jan 31 '17 at 10:32
  • @DomeTune `.then(s => {return s})` + "Promise" in the title – Andreas Jan 31 '17 at 10:39

1 Answers1

1

You forget return statement in your getElemSize function:

getElemSize(css, elem){
switch(css) {
    case 'css':
        return this.driver.findElement(By.css(elem)).getSize().getHeight.then(s => {return s});
        break;
    case 'xpath':
        return  this.driver.findElement(By.xpath(elem)).getSize().getHeight.then(s => {return s});
        break;
    default:
        return Promise.reject();
 }
}

So now getElemSize returns a promise.

Faouzi Oudouh
  • 800
  • 3
  • 15
  • thanks this was actually it. getHeight is still not a function tho, but thats another question I might now be able to resolve – Fenrir Jan 31 '17 at 10:36
  • I dont think that the function `getElemSize` hits the default case, because the function hits the case "css" and than breaks. – DomeTune Jan 31 '17 at 10:38
  • just a little typo I made, I changed it to "selector" and i also have to resolve the promise now – Fenrir Jan 31 '17 at 10:39
  • for the getHeight: I just used the height attribute of the object i get from getSize(); for some reason getHeight; is not a function anymore(?) – Fenrir Jan 31 '17 at 10:47
  • because you used `getHeight` as an object, try `..getHeight().then..` – Faouzi Oudouh Jan 31 '17 at 17:36