-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.js
96 lines (88 loc) · 2.91 KB
/
commands.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// const driveByErrorEnvVarName = 'driveByErrors';
// export function getDriveByErrors() {
// Cypress.env(driveByErrorEnvVarName);
// }
// export function initDriveByErrors() {
// debugger;
// Cypress.env(driveByErrorEnvVarName, []);
// }
// export function addDriveByError(obj) {
// getDriveByErrors().push(obj);
// }
Cypress.Commands.add('verifyText', { type: 'assertion', prevSubject: ['element'] }, (subject, expected) => {
expect(subject).to.have.textTrimmed(expected);
});
Cypress.Commands.add('verifyText2', { type: 'assertion', prevSubject: ['element'] }, (subject, expected) => {
return Cypress.Promise.try(() => subject).then((subject) => {
expect(subject).to.have.textTrimmed(expected);
});
});
chai.use((chai, _utils) => {
chai.Assertion.addMethod('textTrimmed', function (expected) {
// eslint-disable-next-line no-underscore-dangle
const $element = this._obj;
new chai.Assertion($element).to.exist; // NOSONAR
const actual = getElementValueOrText($element)
.replace(/\u00A0/g, ' ') // replace non-breaking space
.replace(/\u200B/g, '') // replace zero width space
.replace(/\s/g, ' ') // replace new lines, tabs with normal space
.trim();
let isEqual;
if (expected instanceof RegExp) {
isEqual = !!actual.match(expected);
} else {
isEqual = actual === `${expected}`;
}
const actualTextEscaped = escape(actual);
const explanation = `but the TRIMMED text was #{act}. Actual text escaped: '${actualTextEscaped}'`;
console.log(actualTextEscaped, explanation);
this.assert(
isEqual,
`expected #{this} to have text #{exp}, ${explanation}`,
`expected #{this} not to have text #{exp}, ${explanation}`,
expected,
actual,
true
);
});
});
function getElementValueOrText(element) {
if (typeof element === 'string') {
return element;
}
return element.prop('tagName') === 'INPUT' ? `${element.val()}` : element.text();
}
export function ifElseVisible(cyChainable, ifFn, elseFn) {
return ifElse(cyChainable, (el) => Cypress.dom.isElement(el) && Cypress.dom.isVisible(el), ifFn, elseFn);
}
export function ifElse(cyChainable, conditionCallback, ifFn, elseFn) {
cyChainable()
.should((_) => {})
.then(($el) => {
const result = conditionCallback($el);
Cypress.log({
name: 'ifElse',
message: `conditionCallback returned ${result}, calling ${result ? 'ifFn' : 'elseFn'}`,
type: 'parent',
consoleProps: () => {
return {
conditionCallback,
};
},
});
if (result) {
ifFn(cyChainable);
} else {
if (elseFn) {
elseFn(cyChainable);
}
}
});
return cyChainable;
}
export function visitStaticWebPage(webpageContent) {
cy.intercept({ url: '/staticMockedWebPage', method: 'GET' }, (req) => {
req.reply(200, webpageContent);
});
cy.visit('/staticMockedWebPage');
}