Skip to content

Commit

Permalink
[FEATURE] Add option to provide URL parameters for each testpage (#109)
Browse files Browse the repository at this point in the history
Resolves #105
  • Loading branch information
RandomByte authored and matz3 committed Sep 5, 2019
1 parent 482d646 commit 7722d4a
Show file tree
Hide file tree
Showing 14 changed files with 452 additions and 103 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ ui5: {

Specific config options:
- [testpage](#testpage)
- [urlParameters](#urlParameters)

#### script

Expand Down Expand Up @@ -249,6 +250,23 @@ ui5: {
}
```

### urlParameters
Type: `Array`
Specific to ["html" mode](#html)

URL parameters to append to every testpage.

Example:
```js
ui5: {
mode: "html",
urlParameters: [{
key: "hidepassed",
value: true
}]
}
```

### config
Type: `object`
Specific to ["script" mode](#script)
Expand Down
19 changes: 18 additions & 1 deletion lib/client/browser.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require("url-polyfill");
const istanbulLibCoverage = require("istanbul-lib-coverage");
require("./discovery.js");

Expand Down Expand Up @@ -115,6 +116,21 @@ require("./discovery.js");
});
}

function addUrlParameters(testpageUrl) {
if (!config.urlParameters) {
return testpageUrl;
}

const url = new URL(testpageUrl, document.location.href);
config.urlParameters.forEach(function(urlParameter) {
url.searchParams.append(urlParameter.key, urlParameter.value);
});
// Sort params for consistency between browsers (probably caused by polyfill)
url.searchParams.sort();

return url.toString();
}

function runTests(testpages) {
let totalNumberOfTest = 0;
let coverageMap;
Expand All @@ -130,7 +146,8 @@ require("./discovery.js");
}

function runTestPage(i) {
const qunitHtmlFile = testpages[i];
const qunitHtmlFile = addUrlParameters(testpages[i]);

windowUtil(qunitHtmlFile, function(testWindow) {
let timer = null;
let testResult = {};
Expand Down
32 changes: 32 additions & 0 deletions lib/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,38 @@ module.exports = function(config) {
}
});
};`,
urlParametersConfigInNonHtmlMode: (mode, urlParameters) => `error 14:
The urlParameters configuration can only be used in "html" mode
module.exports = function(config) {
config.set({
ui5: {
mode: "${mode}"
// Cannot be used in combination with mode "${mode}":
urlParameters: ${JSON.stringify(urlParameters, null, "\t").split("\n").join("\n\t\t\t")}
}
});
};`,
urlParametersNotAnArray: (urlParameters) => `error 15:
The urlParameters configuration must be an array but is of type "${typeof urlParameters}".
module.exports = function(config) {
config.set({
ui5: {
// Must be an array:
urlParameters: ${JSON.stringify(urlParameters, null, "\t").split("\n").join("\n\t\t\t")}
}
});
};`,
urlParameterNotObject: (urlParameter) => `error 16:
The urlParameters configuration must be an array of objects.
But entry ${JSON.stringify(urlParameter)} is of type "${typeof urlParameter}".`,

urlParameterMissingKeyOrValue: (urlParameter) => `error 17:
Every urlParameters configuration entry must have properties "key" and "value":
${JSON.stringify(urlParameter)}
`,
failure: () => "ui5.framework failed. See error message above"

}
Expand Down
31 changes: 29 additions & 2 deletions lib/framework.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ class Framework {
throw new Error(ErrorMessage.failure());
}

if (config.frameworks && config.frameworks.length > 1 && this.config.ui5.mode === "html") {
this.logger.log("error", ErrorMessage.multipleFrameworks(config.frameworks) );
if (this.config.frameworks && this.config.frameworks.length > 1 && this.config.ui5.mode === "html") {
this.logger.log("error", ErrorMessage.multipleFrameworks(this.config.frameworks) );
throw new Error(ErrorMessage.failure());
}

Expand All @@ -149,6 +149,30 @@ class Framework {
throw new Error(ErrorMessage.failure());
}

if (this.config.ui5.mode !== "html" && this.config.ui5.urlParameters) {
this.logger.log("error", ErrorMessage.urlParametersConfigInNonHtmlMode(this.config.ui5.mode,
this.config.ui5.urlParameters));
throw new Error(ErrorMessage.failure());
}

if (this.config.ui5.urlParameters !== undefined && !Array.isArray(this.config.ui5.urlParameters)) {
this.logger.log("error", ErrorMessage.urlParametersNotAnArray(this.config.ui5.urlParameters));
throw new Error(ErrorMessage.failure());
}

if (this.config.ui5.urlParameters) {
this.config.ui5.urlParameters.forEach((urlParameter) => {
if (typeof urlParameter !== "object") {
this.logger.log("error", ErrorMessage.urlParameterNotObject(urlParameter));
throw new Error(ErrorMessage.failure());
}
if (urlParameter.key === undefined || urlParameter.value === undefined) {
this.logger.log("error", ErrorMessage.urlParameterMissingKeyOrValue(urlParameter));
throw new Error(ErrorMessage.failure());
}
});
}

this.config.ui5.paths = this.config.ui5.paths || {
webapp: "webapp",
src: "src",
Expand All @@ -167,6 +191,9 @@ class Framework {
// Make testpage url available to the client
this.config.client.ui5.testpage = this.config.ui5.testpage;

// Pass configured urlParameters to client
this.config.client.ui5.urlParameters = this.config.ui5.urlParameters;


if (this.config.ui5.type === "application") {
const webappFolder = this.config.ui5.paths.webapp;
Expand Down
Loading

0 comments on commit 7722d4a

Please sign in to comment.