Skip to content
This repository has been archived by the owner on Apr 25, 2023. It is now read-only.

Commit

Permalink
Get source, screenshot and windowSize concurrently (#916)
Browse files Browse the repository at this point in the history
  • Loading branch information
dpgraham authored Apr 2, 2019
1 parent be727c3 commit dd2fb93
Showing 1 changed file with 27 additions and 26 deletions.
53 changes: 27 additions & 26 deletions app/main/appium-method-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,36 +166,37 @@ export default class AppiumMethodHandler {
}

async _getSourceAndScreenshot () {
let source, sourceError, screenshot, screenshotError, windowSize, windowSizeError;
try {
source = await this.driver.source();
} catch (e) {
if (e.status === 6) {
throw e;
}
sourceError = e;
}

try {
screenshot = await this.driver.takeScreenshot();
} catch (e) {
if (e.status === 6) {
throw e;
}
screenshotError = e;
}
/* eslint-disable promise/catch-or-return */
return await new Bluebird((resolve) => {
let res = {};

// Resolve when we have source/sourceError, screenshot/screenshotError and windowSize/windowSizeError
// NOTE: Couldn't use Promise.all here because Promise.all fails when it encounters just one error. In this
// case we need it to finish all of the promises and get either the response or the error for each
const checkShouldResolve = () => {
if (
(res.source || res.sourceError) &&
(res.screenshot || res.screenshotError) &&
(res.windowSize || res.windowSizeError)
) {
resolve(res);
}
};

try {
windowSize = await this.driver.getWindowSize();
this.driver.source()
.then((source) => (res.source = source) && checkShouldResolve())
.catch((sourceError) => (res.sourceError = sourceError) && checkShouldResolve());

} catch (e) {
if (e.status === 6) {
throw e;
}
windowSizeError = e;
}
this.driver.takeScreenshot()
.then((screenshot) => (res.screenshot = screenshot) && checkShouldResolve())
.catch((screenshotError) => (res.screenshotError = screenshotError) && checkShouldResolve());

return {source, sourceError, screenshot, screenshotError, windowSize, windowSizeError};
this.driver.getWindowSize()
.then((windowSize) => (res.windowSize = windowSize) && checkShouldResolve())
.catch((windowSizeError) => (res.windowSizeError = windowSizeError) && checkShouldResolve());
});
/* eslint-enable promise/catch-or-return */
}

restart () {
Expand Down

0 comments on commit dd2fb93

Please sign in to comment.