-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands.js
More file actions
76 lines (65 loc) · 1.69 KB
/
commands.js
File metadata and controls
76 lines (65 loc) · 1.69 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import fs from "fs";
import cucumberJson from "wdio-cucumberjs-json-reporter";
export const commands = {
browser: {
$now: (fnOrSelector = () => undefined) => {
const timeouts = browser.getTimeouts();
let result, error;
try {
browser.setTimeout({ implicit: 0 });
result = fnOrSelector instanceof Function ? fnOrSelector() : $(fnOrSelector);
} catch (e) {
error = e;
} finally {
browser.setTimeout(timeouts);
}
if (error) throw error;
return result;
},
waitUntil$: function (
fnOrSelector = () => undefined,
options = undefined
) {
return browser.waitUntil(() => browser.$now(fnOrSelector), options);
},
saveScreenshot: function (origFn, filePath) {
origFn(filePath);
const bitmap = fs.readFileSync(filePath);
if (bitmap) {
const base64 = Buffer.from(bitmap).toString("base64");
cucumberJson.attach(base64, "image/png");
return base64;
}
}
},
element: {
click: function (
origClickFunction,
scrollIntoViewOptions = { behavior: "instant", block: "center" }
) {
this.scrollIntoView(scrollIntoViewOptions);
const { browserName } = browser.capabilities;
if (browserName.includes("afari")) {
return browser.execute("arguments[0].click();", this);
}
return origClickFunction();
}
}
};
const setCommands = () => {
[
[commands.browser, false],
[commands.element, true]
].forEach(([src, onElement]) =>
Object.entries(src).forEach(([name, fn]) => {
if (!onElement) {
browser[name]
? browser.overwriteCommand(name, fn, onElement)
: browser.addCommand(name, fn, onElement);
} else {
browser.overwriteCommand(name, fn, onElement);
}
})
);
};
export default setCommands;