Commit 731ca7f3 authored by Jérome Perrin's avatar Jérome Perrin

ui_test_core: display promise rejection reasons on setFile

Sometimes test fail we don't have any information about what the problem
was.

Using the same technique of turning a promise into "a function called
several times until it returns true or false", we can get rejection
failures in the selenium error message.
parent 0ed3d3d6
...@@ -14,27 +14,53 @@ Selenium.prototype.doSetFile = function(locator, url_and_filename) { ...@@ -14,27 +14,53 @@ Selenium.prototype.doSetFile = function(locator, url_and_filename) {
var tmpArray = url_and_filename.split(' ', 2); var tmpArray = url_and_filename.split(' ', 2);
var url = tmpArray[0]; var url = tmpArray[0];
var fileName = tmpArray[1]; var fileName = tmpArray[1];
var rejectionValue,
promiseState;
// same technique as doVerifyImageMatchSnapshot below
var assertFileSet = () => {
if (promiseState === 'pending') {
return false;
}
if (promiseState === 'resolved') {
return true;
}
if (promiseState === 'rejected') {
Assert.fail(rejectionValue);
}
promiseState = 'pending';
if (!fileName) { if (!fileName) {
throw new Error('file name must not be empty.'); throw new Error('file name must not be empty.');
} }
var fileField = this.page().findElement(locator); var fileField = this.page().findElement(locator);
fetch(url) fetch(url)
.then(function (response){ .then(function(response) {
if (!response.ok) { if (!response.ok) {
throw new Error('HTTP error, status = ' + response.status); throw new Error('HTTP error, status = ' + response.status);
} }
return response.blob(); return response.blob();
}) })
.then(function (blob){ .then(function(blob) {
var dT = new ClipboardEvent('').clipboardData || // Firefox < 62 workaround exploiting https://bugzilla.mozilla.org/show_bug.cgi?id=1422655 var dT =
new DataTransfer(); // specs compliant (as of March 2018 only Chrome) /* Firefox < 62 workaround exploiting https://bugzilla.mozilla.org/show_bug.cgi?id=1422655 */
new ClipboardEvent('').clipboardData ||
/* specs compliant (as of March 2018 only Chrome) */
new DataTransfer();
dT.items.add(new File([blob], fileName)); dT.items.add(new File([blob], fileName));
fileField.files = dT.files; fileField.files = dT.files;
}); })
return Selenium.decorateFunctionWithTimeout(function () { .then(
var window = selenium.browserbot.getCurrentWindow(); function() {
return (fileField.value || "").endsWith(fileName); promiseState = 'resolved';
}, 60000); },
function(error) {
console.error(error);
promiseState = 'rejected';
rejectionValue = 'Error setting file ' + error;
}
);
}
return assertFileSet;
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment