0

I know that I'm not the only one to ask help on this kind of stuff but I didn't successfully found a solution for my case. Im' pretty new in NodeJS

I have a method which make md5 screenshot comparaison, it returns if the 2 images are exactly the same of not. But on iOS, I have to crop the top of the screenshot and for this I'm using a crop method method from jimp package, which is async.

So in my method I'm doing this :

export function compareImage() {
  const hashReference = md5File.sync(screenshot/reference/image.png)
  browser.saveScreenshot(screenshotPathScreen)
  if (browser.isIOS) {
    cropScreenshotTop(screenshot/screen/image.png)
  }
  const hashScreen = md5File.sync(screenshot/screen/image.png)
  return hashReference === hashScreen
}

// crop the top of the image and re-write it
function cropScreenshotTop(imagePath: string) {
  var dimensions = sizeOf(imagePath)
  jimp.read(imagePath, function (err, image) {
    if (err) {
      throw err
    }
    image.crop(0, 100, dimensions.width, dimensions.height - 100).write(imagePath)
  })
}

So when I'm doing this, the crop is made after the return so we check the bad file. I'm trying to wait that the crop is done before the 2 last lines of my function are execute (in the case of browser.isIOS = true). How can I do that ? Right now I've add a browser.pause(1500) in the "if" part, which is pretty ugly. Thanks a lot

(I know it can be tagged as duplicate but I've haven't find a solution for my example)

  • What's `browser.pause`? Edit: Looks like it's from Protractor (which explains the Node.js tag). Added the Protractor tag. – T.J. Crowder Jun 27 '18 at 13:45
  • If `compareImage` has to call a function that does its work asynchronously, it **cannot** return a value based on the completed version of that work. Instead, you'll have to make it provide that value asynchronously (via a callback, promise, etc.). – T.J. Crowder Jun 27 '18 at 13:45
  • browser.pause() is just a wait in the code. I'm doing WebdriverIO testing. – Eric Lerouge Jun 27 '18 at 13:51
  • " Instead, you'll have to make it provide that value asynchronously (via a callback, promise, etc.)" : That's my question, how can I do this ? I've try all day to have something working... I've read a lot of topic about promise and callback but I didn't successfully apply them to my example. Thanks – Eric Lerouge Jun 27 '18 at 13:53
  • The linked question's answers should help. [This may too](http://stackoverflow.com/questions/22707475/how-to-make-a-promise-from-settimeout/22707551#22707551), since it looks like Jimp uses an old-style callback like `setTimeout` does. – T.J. Crowder Jun 27 '18 at 13:55
  • Thanks for the answer but I still don't get how to do my thing, it's to complex for me :( – Eric Lerouge Jun 27 '18 at 14:15

0 Answers0