CB-11162 Appium tests: retry spec on failure

This commit is contained in:
Alexander Sorokin 2016-04-27 18:02:48 +03:00
parent d4a55f20ec
commit 57b177f3fb

View File

@ -157,7 +157,7 @@ describe('Camera tests Android.', function () {
}) })
.fail(function (failure) { .fail(function (failure) {
console.log(failure); console.log(failure);
fail(failure); throw failure;
}); });
} }
@ -170,30 +170,20 @@ describe('Camera tests Android.', function () {
.executeAsync(cameraHelper.checkPicture, [getCurrentPromiseId()]) .executeAsync(cameraHelper.checkPicture, [getCurrentPromiseId()])
.then(function (result) { .then(function (result) {
if (shouldLoad) { if (shouldLoad) {
expect(result.length).toBeGreaterThan(0); if (result.length === 0) {
throw 'The result is an empty string.';
}
if (result.indexOf('ERROR') >= 0) { if (result.indexOf('ERROR') >= 0) {
return fail(result); throw result;
} }
} else { } else {
if (result.indexOf('ERROR') === -1) { if (result.indexOf('ERROR') === -1) {
return fail('Unexpected success callback with result: ' + result); throw 'Unexpected success callback with result: ' + result;
} }
expect(result.indexOf('ERROR')).toBe(0);
} }
}); });
} }
function runCombinedSpec(spec) {
return driver
.then(function () {
return getPicture(spec.options);
})
.then(function () {
return checkPicture(true);
})
.fail(saveScreenshotAndFail);
}
// deletes the latest image from the gallery // deletes the latest image from the gallery
function deleteImage() { function deleteImage() {
var holdTile = new wd.TouchAction(); var holdTile = new wd.TouchAction();
@ -229,6 +219,41 @@ describe('Camera tests Android.', function () {
}); });
} }
function recreateSession() {
return driver
.quit()
.finally(function () {
return getDriver();
});
}
function tryRunSpec(spec) {
return driver
.then(spec)
.fail(function () {
return recreateSession()
.then(spec)
.fail(function() {
return recreateSession()
.then(spec);
});
})
.fail(saveScreenshotAndFail);
}
function runCombinedSpec(s) {
var spec = function () {
return driver
.then(function () {
return getPicture(s.options);
})
.then(function () {
return checkPicture(true);
});
};
return tryRunSpec(spec);
}
it('camera.ui.util configuring driver and starting a session', function (done) { it('camera.ui.util configuring driver and starting a session', function (done) {
getDriver() getDriver()
.fail(fail) .fail(fail)
@ -253,128 +278,138 @@ describe('Camera tests Android.', function () {
describe('Specs.', function () { describe('Specs.', function () {
// getPicture() with saveToPhotoLibrary = true // getPicture() with saveToPhotoLibrary = true
it('camera.ui.spec.1 Saving the picture to photo library', function (done) { it('camera.ui.spec.1 Saving a picture to the photo library', function (done) {
var options = { var spec = function() {
quality: 50, var options = {
allowEdit: false, quality: 50,
sourceType: cameraConstants.PictureSourceType.CAMERA, allowEdit: false,
saveToPhotoAlbum: true sourceType: cameraConstants.PictureSourceType.CAMERA,
saveToPhotoAlbum: true
};
return driver
.then(function () {
return getPicture(options);
})
.then(function () {
isTestPictureSaved = true;
return checkPicture(true);
});
}; };
driver
.then(function () { return tryRunSpec(spec)
return getPicture(options);
})
.then(function () {
isTestPictureSaved = true;
return checkPicture(true);
})
.fail(saveScreenshotAndFail)
.done(done); .done(done);
}, 3 * MINUTE); }, 10 * MINUTE);
// getPicture() with mediaType: VIDEO, sourceType: PHOTOLIBRARY // getPicture() with mediaType: VIDEO, sourceType: PHOTOLIBRARY
it('camera.ui.spec.2 Selecting only videos', function (done) { it('camera.ui.spec.2 Selecting only videos', function (done) {
var options = { sourceType: cameraConstants.PictureSourceType.PHOTOLIBRARY, var spec = function () {
mediaType: cameraConstants.MediaType.VIDEO }; var options = { sourceType: cameraConstants.PictureSourceType.PHOTOLIBRARY,
driver mediaType: cameraConstants.MediaType.VIDEO };
.then(function () { return driver
return getPicture(options, true); .then(function () {
}) return getPicture(options, true);
.context('NATIVE_APP') })
.then(function () { .context('NATIVE_APP')
// try to find "Gallery" menu item .then(function () {
// if there's none, the gallery should be already opened // try to find "Gallery" menu item
return driver // if there's none, the gallery should be already opened
.elementByXPath('//android.widget.TextView[@text="Gallery"]') return driver
.then(function (element) { .elementByXPath('//android.widget.TextView[@text="Gallery"]')
return element.click(); .then(function (element) {
}, function () { return element.click();
return driver; }, function () {
}); return driver;
}) });
.then(function () { })
// if the gallery is opened on the videos page, .then(function () {
// there should be a "Choose video" caption // if the gallery is opened on the videos page,
return driver // there should be a "Choose video" caption
.elementByXPath('//*[@text="Choose video"]') return driver
.fail(function () { .elementByXPath('//*[@text="Choose video"]')
throw 'Couldn\'t find "Choose video" element.'; .fail(function () {
}); throw 'Couldn\'t find "Choose video" element.';
}) });
.deviceKeyEvent(BACK_BUTTON) })
.elementByXPath('//android.widget.TextView[@text="Gallery"]') .deviceKeyEvent(BACK_BUTTON)
.deviceKeyEvent(BACK_BUTTON) .elementByXPath('//android.widget.TextView[@text="Gallery"]')
.finally(function () { .deviceKeyEvent(BACK_BUTTON)
return driver .finally(function () {
.elementById('action_bar_title') return driver
.then(function () { .elementById('action_bar_title')
// success means we're still in native app .then(function () {
return driver // success means we're still in native app
.deviceKeyEvent(BACK_BUTTON); return driver
}, function () { .deviceKeyEvent(BACK_BUTTON);
// error means we're already in webview }, function () {
return driver; // error means we're already in webview
}); return driver;
}) });
.fail(saveScreenshotAndFail) });
};
return tryRunSpec(spec)
.done(done); .done(done);
}, 3 * MINUTE); }, 10 * MINUTE);
// getPicture(), then dismiss // getPicture(), then dismiss
// wait for the error callback to be called // wait for the error callback to be called
it('camera.ui.spec.3 Dismissing the camera', function (done) { it('camera.ui.spec.3 Dismissing the camera', function (done) {
var options = { quality: 50, var spec = function () {
allowEdit: true, var options = { quality: 50,
sourceType: cameraConstants.PictureSourceType.CAMERA, allowEdit: true,
destinationType: cameraConstants.DestinationType.FILE_URI }; sourceType: cameraConstants.PictureSourceType.CAMERA,
driver destinationType: cameraConstants.DestinationType.FILE_URI };
.then(function () { return driver
return getPicture(options, true); .then(function () {
}) return getPicture(options, true);
.context("NATIVE_APP") })
.waitForElementByXPath('//android.widget.ImageView[contains(@resource-id,\'cancel\')]', MINUTE / 2) .context("NATIVE_APP")
.click() .waitForElementByXPath('//android.widget.ImageView[contains(@resource-id,\'cancel\')]', MINUTE / 2)
.then(function () { .click()
return checkPicture(false); .then(function () {
}) return checkPicture(false);
.fail(saveScreenshotAndFail) });
};
return tryRunSpec(spec)
.done(done); .done(done);
}, 3 * MINUTE); }, 10 * MINUTE);
// getPicture(), then take picture but dismiss the edit // getPicture(), then take picture but dismiss the edit
// wait for the error callback to be called // wait for the error callback to be called
it('camera.ui.spec.4 Dismissing the edit', function (done) { it('camera.ui.spec.4 Dismissing the edit', function (done) {
var options = { quality: 50, var spec = function () {
allowEdit: true, var options = { quality: 50,
sourceType: cameraConstants.PictureSourceType.CAMERA, allowEdit: true,
destinationType: cameraConstants.DestinationType.FILE_URI }; sourceType: cameraConstants.PictureSourceType.CAMERA,
driver destinationType: cameraConstants.DestinationType.FILE_URI };
.then(function () { return driver
return getPicture(options, true); .then(function () {
}) return getPicture(options, true);
.context('NATIVE_APP') })
.waitForElementByXPath('//android.widget.ImageView[contains(@resource-id,\'shutter\')]', MINUTE / 2) .context('NATIVE_APP')
.click() .waitForElementByXPath('//android.widget.ImageView[contains(@resource-id,\'shutter\')]', MINUTE / 2)
.waitForElementByXPath('//android.widget.ImageView[contains(@resource-id,\'done\')]', MINUTE / 2) .click()
.click() .waitForElementByXPath('//android.widget.ImageView[contains(@resource-id,\'done\')]', MINUTE / 2)
.waitForElementByXPath('//*[contains(@resource-id,\'discard\')]', MINUTE / 2) .click()
.click() .waitForElementByXPath('//*[contains(@resource-id,\'discard\')]', MINUTE / 2)
.then(function () { .click()
return checkPicture(false); .then(function () {
}) return checkPicture(false);
.fail(saveScreenshotAndFail) });
};
return tryRunSpec(spec)
.done(done); .done(done);
}, 3 * MINUTE); }, 10 * MINUTE);
// combine various options for getPicture() // combine various options for getPicture()
generateSpecs().forEach(function (spec) { generateSpecs().forEach(function (spec) {
it('camera.ui.spec.5.' + spec.id + ' Combining options. ' + spec.description, function (done) { it('camera.ui.spec.5.' + spec.id + ' Combining options. ' + spec.description, function (done) {
runCombinedSpec(spec) runCombinedSpec(spec)
.done(done); .done(done);
}, 3 * MINUTE); }, 10 * MINUTE);
}); });
it('camera.ui.util Delete test image from device library', function (done) { it('camera.ui.util Delete test image from device library', function (done) {
if (!isTestPictureSaved) { if (!isTestPictureSaved) {
// couldn't save test picture earlier, so nothing to delete here // couldn't save test picture earlier, so nothing to delete here