         handler.focusWindow?()
         if handler.takeScreenshot
             startTime = Date.now()
+            handler.takeScreenshot opts
             maxDelay = handler.TAKE_SCREENSHOT_DELAY_MS ? CUSTOM_SCREENSHOT_MAX_DELAY_MS
-            itertion = 0
-            while Date.now() < startTime + maxDelay
-                if itertion++ % 10 is 0
-                    handler.takeScreenshot opts
-                try
-                    stats = fs.statSync opts.path
-                    if stats?.size
-                        break
-                childProcess.execSync "sleep 0.1"
-            if stats?.size
+            if waitForScreenshot(opts.path, maxDelay)
                 return
             else
                 log.warn """

        Expected: #{expectedUri}
    """

takeScreenshot = (opts) ->
    # use target-specified screenshot function
    if opts.env


    # take the whole screen screenshot
    driver.takeScreenshot opts
    waitForScreenshot(opts.path, CUSTOM_SCREENSHOT_MAX_DELAY_MS)
    return

tryTakeScreenshot = (opts, isOk) ->
