-
Notifications
You must be signed in to change notification settings - Fork 0
/
params.js
56 lines (49 loc) · 1.43 KB
/
params.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
Cypress.Commands.add("params", { prevSubject: "optional" }, (subject) => {
let subj = null;
let url = null;
if (Cypress._.isNil(subject)) {
subj = cy.location("search", { log: false });
} else if (
Cypress._.isObject(subject) &&
subject.hasOwnProperty("toString")
) {
subj = cy.wrap(subject.toString(), { log: false });
} else if (Cypress._.isObject(subject) && subject.hasOwnProperty("url")) {
subj = cy.wrap(subject.url, { log: false });
} else if (Cypress._.isString(subject)) {
subj = cy.wrap(subject, { log: false });
} else {
throw new Error(`Cannot get query parameter for ${subject}`);
}
return subj
.then((url) =>
{
const paramsEntries = new URLSearchParams(url.slice(url.indexOf("?")))
.entries();
return [...paramsEntries]
.reduce((previous, [name, value]) => {
if (name in previous) {
if (!Array.isArray(previous[name])) {
previous[name] = [previous[name]];
}
previous[name].push(value);
} else {
previous[name] = value;
}
return previous;
}, {});
}
)
.then((yielded) => {
const message = [yielded];
Cypress.log({
name: "params",
message: message,
consoleProps: () => ({
subject: url,
yielded,
}),
});
return yielded;
});
});