expect-webdriverio 2.0.0 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/README.md +1 -1
  2. package/lib/addMatchers.js +48 -0
  3. package/lib/index.js +6 -0
  4. package/lib/jasmineUtils.js +205 -0
  5. package/lib/matchers/browser/toHaveTitle.js +26 -0
  6. package/lib/matchers/browser/toHaveTitleContaining.js +15 -0
  7. package/lib/matchers/browser/toHaveUrl.js +26 -0
  8. package/lib/matchers/browser/toHaveUrlContaining.js +15 -0
  9. package/lib/matchers/element/toBeClickable.js +23 -0
  10. package/lib/matchers/element/toBeDisabled.js +16 -0
  11. package/lib/matchers/element/toBeDisplayed.js +27 -0
  12. package/lib/matchers/element/toBeDisplayedInViewport.js +27 -0
  13. package/lib/matchers/element/toBeEnabled.js +16 -0
  14. package/lib/matchers/element/toBeExisting.js +32 -0
  15. package/lib/matchers/element/toBeFocused.js +16 -0
  16. package/lib/matchers/element/toBeSelected.js +22 -0
  17. package/lib/matchers/element/toHaveAttribute.js +48 -0
  18. package/lib/matchers/element/toHaveAttributeContaining.js +16 -0
  19. package/lib/matchers/element/toHaveChildren.js +47 -0
  20. package/lib/matchers/element/toHaveElementClass.js +60 -0
  21. package/lib/matchers/element/toHaveElementClassContaining.js +20 -0
  22. package/lib/matchers/element/toHaveElementProperty.js +52 -0
  23. package/lib/matchers/element/toHaveHref.js +14 -0
  24. package/lib/matchers/element/toHaveHrefContaining.js +16 -0
  25. package/lib/matchers/element/toHaveId.js +13 -0
  26. package/lib/matchers/element/toHaveText.js +37 -0
  27. package/lib/matchers/element/toHaveTextContaining.js +15 -0
  28. package/lib/matchers/element/toHaveValue.js +13 -0
  29. package/lib/matchers/element/toHaveValueContaining.js +15 -0
  30. package/lib/matchers/elements/toBeElementsArrayOfSize.js +41 -0
  31. package/lib/matchers/mock/toBeRequested.js +12 -0
  32. package/lib/matchers/mock/toBeRequestedTimes.js +37 -0
  33. package/lib/matchers/mock/toBeRequestedWith.js +218 -0
  34. package/lib/matchers/mock/toBeRequestedWithResponse.js +13 -0
  35. package/lib/matchers.js +40 -0
  36. package/lib/options.js +33 -0
  37. package/lib/util/elementsUtil.js +26 -0
  38. package/lib/util/executeCommand.js +31 -0
  39. package/lib/util/expectAdapter.js +39 -0
  40. package/lib/util/formatMessage.js +87 -0
  41. package/lib/util/refetchElements.js +15 -0
  42. package/lib/utils.js +131 -0
  43. package/package.json +1 -1
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.toHaveClass = exports.toHaveElementClass = void 0;
4
+ const utils_1 = require("../../utils");
5
+ const expectAdapter_1 = require("../../util/expectAdapter");
6
+ async function condition(el, attribute, value, options) {
7
+ const { ignoreCase = false, trim = false, containing = false } = options;
8
+ let attr = await el.getAttribute(attribute);
9
+ if (typeof attr !== 'string') {
10
+ return { result: false };
11
+ }
12
+ if (trim) {
13
+ attr = attr.trim();
14
+ }
15
+ if (ignoreCase) {
16
+ attr = attr.toLowerCase();
17
+ value = value.toLowerCase();
18
+ }
19
+ if (containing) {
20
+ return {
21
+ result: attr.includes(value),
22
+ value: attr
23
+ };
24
+ }
25
+ const classes = attr.split(' ');
26
+ return {
27
+ result: classes.includes(value),
28
+ value: attr
29
+ };
30
+ }
31
+ function toHaveElementClassFn(received, className, options = {}) {
32
+ const isNot = this.isNot;
33
+ const { expectation = 'class', verb = 'have' } = this;
34
+ const attribute = 'class';
35
+ return browser.call(async () => {
36
+ let el = await received;
37
+ let attr;
38
+ const pass = await utils_1.waitUntil(async () => {
39
+ const result = await utils_1.executeCommand.call(this, el, condition, options, [attribute, className, options]);
40
+ el = result.el;
41
+ attr = result.values;
42
+ return result.success;
43
+ }, isNot, options);
44
+ utils_1.updateElementsArray(pass, received, el);
45
+ const message = utils_1.enhanceError(el, utils_1.wrapExpectedWithArray(el, attr, className), attr, this, verb, expectation, '', options);
46
+ return {
47
+ pass,
48
+ message: () => message
49
+ };
50
+ });
51
+ }
52
+ function toHaveElementClass(...args) {
53
+ return expectAdapter_1.runExpect.call(this, toHaveElementClassFn, args);
54
+ }
55
+ exports.toHaveElementClass = toHaveElementClass;
56
+ function toHaveClass(...args) {
57
+ console.warn('expect(...).toHaveClass is deprecated and will be removed in next release. Use toHaveElementClass instead.');
58
+ return expectAdapter_1.runExpect.call(this, toHaveElementClassFn, args);
59
+ }
60
+ exports.toHaveClass = toHaveClass;
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.toHaveClassContaining = exports.toHaveElementClassContaining = void 0;
4
+ const expectAdapter_1 = require("../../util/expectAdapter");
5
+ const toHaveAttribute_1 = require("./toHaveAttribute");
6
+ function toHaveElementClassContainingFn(el, className, options = {}) {
7
+ return toHaveAttribute_1.toHaveAttributeFn.call(this, el, 'class', className, {
8
+ ...options,
9
+ containing: true
10
+ });
11
+ }
12
+ function toHaveElementClassContaining(...args) {
13
+ return expectAdapter_1.runExpect.call(this, toHaveElementClassContainingFn, args);
14
+ }
15
+ exports.toHaveElementClassContaining = toHaveElementClassContaining;
16
+ function toHaveClassContaining(...args) {
17
+ console.warn('expect(...).toHaveClassContaining is deprecated and will be removed in next release. Use toHaveElementClassContaining instead.');
18
+ return expectAdapter_1.runExpect.call(this, toHaveElementClassContainingFn, args);
19
+ }
20
+ exports.toHaveClassContaining = toHaveClassContaining;
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.toHaveElementProperty = exports.toHaveElementPropertyFn = void 0;
4
+ const utils_1 = require("../../utils");
5
+ const expectAdapter_1 = require("../../util/expectAdapter");
6
+ async function condition(el, property, value, options = {}) {
7
+ const { asString = false } = options;
8
+ let prop = await el.getProperty(property);
9
+ if (prop === null || prop === undefined) {
10
+ return { result: false, value: prop };
11
+ }
12
+ if (value === null) {
13
+ return { result: true, value: prop };
14
+ }
15
+ if (typeof value !== 'string' || (typeof prop !== 'string' && !asString)) {
16
+ return { result: prop === value, value: prop };
17
+ }
18
+ prop = prop.toString();
19
+ return utils_1.compareText(prop, value, options);
20
+ }
21
+ function toHaveElementPropertyFn(received, property, value, options = {}) {
22
+ const isNot = this.isNot;
23
+ const { expectation = 'property', verb = 'have' } = this;
24
+ return browser.call(async () => {
25
+ let el = await received;
26
+ let prop;
27
+ const pass = await utils_1.waitUntil(async () => {
28
+ const result = await utils_1.executeCommand.call(this, el, condition, options, [property, value]);
29
+ el = result.el;
30
+ prop = result.values;
31
+ return result.success;
32
+ }, isNot, options);
33
+ utils_1.updateElementsArray(pass, received, el);
34
+ let message;
35
+ if (value === undefined) {
36
+ message = utils_1.enhanceError(el, !isNot, pass, this, verb, expectation, property, options);
37
+ }
38
+ else {
39
+ const expected = utils_1.wrapExpectedWithArray(el, prop, value);
40
+ message = utils_1.enhanceError(el, expected, prop, this, verb, expectation, property, options);
41
+ }
42
+ return {
43
+ pass,
44
+ message: () => message,
45
+ };
46
+ });
47
+ }
48
+ exports.toHaveElementPropertyFn = toHaveElementPropertyFn;
49
+ function toHaveElementProperty(...args) {
50
+ return expectAdapter_1.runExpect.call(this, toHaveElementPropertyFn, args);
51
+ }
52
+ exports.toHaveElementProperty = toHaveElementProperty;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.toHaveLink = exports.toHaveHref = exports.toHaveHrefFn = void 0;
4
+ const expectAdapter_1 = require("../../util/expectAdapter");
5
+ const toHaveAttribute_1 = require("./toHaveAttribute");
6
+ function toHaveHrefFn(el, href, options = {}) {
7
+ return toHaveAttribute_1.toHaveAttributeFn.call(this, el, 'href', href, options);
8
+ }
9
+ exports.toHaveHrefFn = toHaveHrefFn;
10
+ function toHaveHref(...args) {
11
+ return expectAdapter_1.runExpect.call(this, toHaveHrefFn, args);
12
+ }
13
+ exports.toHaveHref = toHaveHref;
14
+ exports.toHaveLink = toHaveHref;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.toHaveLinkContaining = exports.toHaveHrefContaining = void 0;
4
+ const expectAdapter_1 = require("../../util/expectAdapter");
5
+ const toHaveHref_1 = require("./toHaveHref");
6
+ function toHaveHrefContainingFn(el, href, options = {}) {
7
+ return toHaveHref_1.toHaveHrefFn.call(this, el, href, {
8
+ ...options,
9
+ containing: true
10
+ });
11
+ }
12
+ function toHaveHrefContaining(...args) {
13
+ return expectAdapter_1.runExpect.call(this, toHaveHrefContainingFn, args);
14
+ }
15
+ exports.toHaveHrefContaining = toHaveHrefContaining;
16
+ exports.toHaveLinkContaining = toHaveHrefContaining;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.toHaveId = exports.toHaveIdFn = void 0;
4
+ const expectAdapter_1 = require("../../util/expectAdapter");
5
+ const toHaveAttribute_1 = require("./toHaveAttribute");
6
+ function toHaveIdFn(el, id, options = {}) {
7
+ return toHaveAttribute_1.toHaveAttributeFn.call(this, el, 'id', id, options);
8
+ }
9
+ exports.toHaveIdFn = toHaveIdFn;
10
+ function toHaveId(...args) {
11
+ return expectAdapter_1.runExpect.call(this, toHaveIdFn, args);
12
+ }
13
+ exports.toHaveId = toHaveId;
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.toHaveText = exports.toHaveTextFn = void 0;
4
+ const utils_1 = require("../../utils");
5
+ const expectAdapter_1 = require("../../util/expectAdapter");
6
+ async function condition(el, text, options) {
7
+ const actualText = await el.getText();
8
+ if (Array.isArray(text)) {
9
+ return utils_1.compareTextWithArray(actualText, text, options);
10
+ }
11
+ return utils_1.compareText(actualText, text, options);
12
+ }
13
+ function toHaveTextFn(received, text, options = {}) {
14
+ const isNot = this.isNot;
15
+ const { expectation = 'text', verb = 'have' } = this;
16
+ return browser.call(async () => {
17
+ let el = await received;
18
+ let actualText;
19
+ const pass = await utils_1.waitUntil(async () => {
20
+ const result = await utils_1.executeCommand.call(this, el, condition, options, [text, options]);
21
+ el = result.el;
22
+ actualText = result.values;
23
+ return result.success;
24
+ }, isNot, options);
25
+ utils_1.updateElementsArray(pass, received, el);
26
+ const message = utils_1.enhanceError(el, utils_1.wrapExpectedWithArray(el, actualText, text), actualText, this, verb, expectation, '', options);
27
+ return {
28
+ pass,
29
+ message: () => message
30
+ };
31
+ });
32
+ }
33
+ exports.toHaveTextFn = toHaveTextFn;
34
+ function toHaveText(...args) {
35
+ return expectAdapter_1.runExpect.call(this, toHaveTextFn, args);
36
+ }
37
+ exports.toHaveText = toHaveText;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.toHaveTextContaining = void 0;
4
+ const expectAdapter_1 = require("../../util/expectAdapter");
5
+ const toHaveText_1 = require("./toHaveText");
6
+ function toHaveTextContainingFn(el, text, options = {}) {
7
+ return toHaveText_1.toHaveTextFn.call(this, el, text, {
8
+ ...options,
9
+ containing: true
10
+ });
11
+ }
12
+ function toHaveTextContaining(...args) {
13
+ return expectAdapter_1.runExpect.call(this, toHaveTextContainingFn, args);
14
+ }
15
+ exports.toHaveTextContaining = toHaveTextContaining;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.toHaveValue = exports.toHaveValueFn = void 0;
4
+ const expectAdapter_1 = require("../../util/expectAdapter");
5
+ const toHaveElementProperty_1 = require("./toHaveElementProperty");
6
+ function toHaveValueFn(el, value, options = {}) {
7
+ return toHaveElementProperty_1.toHaveElementPropertyFn.call(this, el, 'value', value, options);
8
+ }
9
+ exports.toHaveValueFn = toHaveValueFn;
10
+ function toHaveValue(...args) {
11
+ return expectAdapter_1.runExpect.call(this, toHaveValueFn, args);
12
+ }
13
+ exports.toHaveValue = toHaveValue;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.toHaveValueContaining = void 0;
4
+ const expectAdapter_1 = require("../../util/expectAdapter");
5
+ const toHaveValue_1 = require("./toHaveValue");
6
+ function toHaveValueContainingFn(el, value, options = {}) {
7
+ return toHaveValue_1.toHaveValueFn.call(this, el, value, {
8
+ ...options,
9
+ containing: true
10
+ });
11
+ }
12
+ function toHaveValueContaining(...args) {
13
+ return expectAdapter_1.runExpect.call(this, toHaveValueContainingFn, args);
14
+ }
15
+ exports.toHaveValueContaining = toHaveValueContaining;
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.toBeElementsArrayOfSize = void 0;
4
+ const utils_1 = require("../../utils");
5
+ const refetchElements_1 = require("../../util/refetchElements");
6
+ const expectAdapter_1 = require("../../util/expectAdapter");
7
+ function toBeElementsArrayOfSizeFn(received, size, options = {}) {
8
+ const isNot = this.isNot;
9
+ const { expectation = 'elements array of size', verb = 'be' } = this;
10
+ if (typeof size === 'number') {
11
+ options.eq = size;
12
+ }
13
+ else if (!size || (typeof size.eq !== 'number' && typeof size.gte !== 'number' && typeof size.lte !== 'number')) {
14
+ throw new Error('Invalid params passed to toBeElementsArrayOfSize.');
15
+ }
16
+ else {
17
+ options = size;
18
+ }
19
+ const eq = options.eq;
20
+ const gte = options.gte || 1;
21
+ const lte = options.lte || 0;
22
+ return browser.call(async () => {
23
+ let elements = await received;
24
+ const arrLength = elements.length;
25
+ const pass = await utils_1.waitUntil(async () => {
26
+ elements = await refetchElements_1.refetchElements(elements, options.wait, true);
27
+ return utils_1.compareNumbers(elements.length, gte, lte, eq);
28
+ }, isNot, options);
29
+ utils_1.updateElementsArray(pass, received, elements, true);
30
+ const error = utils_1.numberError(gte, lte, eq);
31
+ const message = utils_1.enhanceError(elements, error, arrLength, this, verb, expectation, '', options);
32
+ return {
33
+ pass,
34
+ message: () => message
35
+ };
36
+ });
37
+ }
38
+ function toBeElementsArrayOfSize(...args) {
39
+ return expectAdapter_1.runExpect.call(this, toBeElementsArrayOfSizeFn, args);
40
+ }
41
+ exports.toBeElementsArrayOfSize = toBeElementsArrayOfSize;
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.toBeRequested = void 0;
4
+ const toBeRequestedTimes_1 = require("./toBeRequestedTimes");
5
+ const expectAdapter_1 = require("../../util/expectAdapter");
6
+ function toBeRequestedFn(received, options = {}) {
7
+ return toBeRequestedTimes_1.toBeRequestedTimes.call({ ...(this || {}), expectation: 'called' }, received, { ...options, gte: 1 });
8
+ }
9
+ function toBeRequested(...args) {
10
+ return expectAdapter_1.runExpect.call(this, toBeRequestedFn, args);
11
+ }
12
+ exports.toBeRequested = toBeRequested;
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.toBeRequestedTimes = exports.toBeRequestedTimesFn = void 0;
4
+ const utils_1 = require("../../utils");
5
+ const expectAdapter_1 = require("../../util/expectAdapter");
6
+ const formatMessage_1 = require("../../util/formatMessage");
7
+ function toBeRequestedTimesFn(received, times = {}, options = {}) {
8
+ const isNot = this.isNot || false;
9
+ const { expectation = `called${typeof times === 'number' ? ' ' + times : ''} time${times !== 1 ? 's' : ''}`, verb = 'be' } = this;
10
+ if (typeof times === 'number') {
11
+ options.eq = times;
12
+ }
13
+ else if (typeof times === 'object') {
14
+ options = { ...options, ...times };
15
+ }
16
+ const eq = options.eq;
17
+ const gte = options.gte || 1;
18
+ const lte = options.lte || 0;
19
+ return browser.call(async () => {
20
+ let actual;
21
+ const pass = await utils_1.waitUntil(async () => {
22
+ actual = received.calls.length;
23
+ return utils_1.compareNumbers(actual, gte, lte, eq);
24
+ }, isNot, { ...options, wait: isNot ? 0 : options.wait });
25
+ const error = formatMessage_1.numberError(gte, lte, eq);
26
+ const message = utils_1.enhanceError('mock', error, actual, this, verb, expectation, '', options);
27
+ return {
28
+ pass,
29
+ message: () => message
30
+ };
31
+ });
32
+ }
33
+ exports.toBeRequestedTimesFn = toBeRequestedTimesFn;
34
+ function toBeRequestedTimes(...args) {
35
+ return expectAdapter_1.runExpect.call(this, toBeRequestedTimesFn, args);
36
+ }
37
+ exports.toBeRequestedTimes = toBeRequestedTimes;
@@ -0,0 +1,218 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.toBeRequestedWith = exports.toBeRequestedWithFn = void 0;
4
+ const utils_1 = require("../../utils");
5
+ const expectAdapter_1 = require("../../util/expectAdapter");
6
+ const jasmineUtils_1 = require("../../jasmineUtils");
7
+ const STR_LIMIT = 80;
8
+ const KEY_LIMIT = 12;
9
+ function toBeRequestedWithFn(received, requestedWith = {}, options = {}) {
10
+ const isNot = this.isNot || false;
11
+ const { expectation = 'called with', verb = 'be' } = this;
12
+ return browser.call(async () => {
13
+ let actual;
14
+ const pass = await utils_1.waitUntil(async () => {
15
+ for (const call of received.calls) {
16
+ actual = call;
17
+ if (methodMatcher(call.method, requestedWith.method) &&
18
+ statusCodeMatcher(call.statusCode, requestedWith.statusCode) &&
19
+ urlMatcher(call.url, requestedWith.url) &&
20
+ headersMatcher(call.headers, requestedWith.requestHeaders) &&
21
+ headersMatcher(call.responseHeaders, requestedWith.responseHeaders) &&
22
+ bodyMatcher(call.postData, requestedWith.postData) &&
23
+ bodyMatcher(call.body, requestedWith.response)) {
24
+ return true;
25
+ }
26
+ }
27
+ return false;
28
+ }, isNot, { ...options, wait: isNot ? 0 : options.wait });
29
+ const message = utils_1.enhanceError('mock', minifyRequestedWith(requestedWith), minifyRequestMock(actual, requestedWith) || 'was not called', this, verb, expectation, '', options);
30
+ return {
31
+ pass,
32
+ message: () => message,
33
+ };
34
+ });
35
+ }
36
+ exports.toBeRequestedWithFn = toBeRequestedWithFn;
37
+ const methodMatcher = (method, expected) => {
38
+ if (typeof expected === 'undefined') {
39
+ return true;
40
+ }
41
+ if (!Array.isArray(expected)) {
42
+ expected = [expected];
43
+ }
44
+ return expected
45
+ .map((m) => {
46
+ if (typeof m !== 'string') {
47
+ return console.error('expect.toBeRequestedWith: unsupported value passed to method ' + m);
48
+ }
49
+ return m.toUpperCase();
50
+ })
51
+ .includes(method);
52
+ };
53
+ const statusCodeMatcher = (statusCode, expected) => {
54
+ if (typeof expected === 'undefined') {
55
+ return true;
56
+ }
57
+ if (!Array.isArray(expected)) {
58
+ expected = [expected];
59
+ }
60
+ return expected.includes(statusCode);
61
+ };
62
+ const urlMatcher = (url, expected) => {
63
+ if (typeof expected === 'undefined') {
64
+ return true;
65
+ }
66
+ if (typeof expected === 'function') {
67
+ return expected(url);
68
+ }
69
+ return jasmineUtils_1.equals(url, expected);
70
+ };
71
+ const headersMatcher = (headers, expected) => {
72
+ if (typeof expected === 'undefined') {
73
+ return true;
74
+ }
75
+ if (typeof expected === 'function') {
76
+ return expected(headers);
77
+ }
78
+ return jasmineUtils_1.equals(headers, expected);
79
+ };
80
+ const bodyMatcher = (body, expected) => {
81
+ if (typeof expected === 'undefined') {
82
+ return true;
83
+ }
84
+ if (typeof expected === 'function') {
85
+ return expected(body);
86
+ }
87
+ if (typeof body === 'undefined') {
88
+ return false;
89
+ }
90
+ let parsedBody = body;
91
+ if (body instanceof Buffer) {
92
+ parsedBody = body.toString();
93
+ }
94
+ if (typeof (body) === 'string' && isExpectedJsonLike(expected)) {
95
+ parsedBody = tryParseBody(body);
96
+ if (parsedBody === null) {
97
+ return false;
98
+ }
99
+ }
100
+ return jasmineUtils_1.equals(parsedBody, expected);
101
+ };
102
+ const isExpectedJsonLike = (expected) => {
103
+ if (typeof expected === 'undefined') {
104
+ return false;
105
+ }
106
+ const actualSample = isMatcher(expected)
107
+ ? expected.sample
108
+ : expected;
109
+ return (Array.isArray(actualSample) ||
110
+ (typeof actualSample === 'object' &&
111
+ actualSample !== null &&
112
+ actualSample instanceof RegExp === false));
113
+ };
114
+ const isMatcher = (filter) => {
115
+ var _a;
116
+ return typeof ((_a = filter.__proto__) === null || _a === void 0 ? void 0 : _a.asymmetricMatch) === 'function';
117
+ };
118
+ const tryParseBody = (jsonString, fallback = null) => {
119
+ try {
120
+ return typeof jsonString === 'undefined' ? fallback : JSON.parse(jsonString);
121
+ }
122
+ catch (_a) {
123
+ return fallback;
124
+ }
125
+ };
126
+ const minifyRequestMock = (requestMock, requestedWith) => {
127
+ if (typeof requestMock === 'undefined') {
128
+ return requestMock;
129
+ }
130
+ const r = {
131
+ url: requestMock.url,
132
+ method: requestMock.method,
133
+ requestHeaders: requestMock.headers,
134
+ responseHeaders: requestMock.responseHeaders,
135
+ postData: typeof requestMock.postData === 'string' && isExpectedJsonLike(requestedWith.postData)
136
+ ? tryParseBody(requestMock.postData, requestMock.postData)
137
+ : requestMock.postData,
138
+ response: typeof requestMock.body === 'string' && isExpectedJsonLike(requestedWith.response)
139
+ ? tryParseBody(requestMock.body, requestMock.body)
140
+ : requestMock.body,
141
+ };
142
+ deleteUndefinedValues(r, requestedWith);
143
+ return minifyRequestedWith(r);
144
+ };
145
+ const minifyRequestedWith = (r) => {
146
+ const result = {
147
+ url: requestedWithParamToString(r.url),
148
+ method: r.method,
149
+ requestHeaders: requestedWithParamToString(r.requestHeaders, shortenJson),
150
+ responseHeaders: requestedWithParamToString(r.responseHeaders, shortenJson),
151
+ postData: requestedWithParamToString(r.postData, shortenJson),
152
+ response: requestedWithParamToString(r.response, shortenJson),
153
+ };
154
+ deleteUndefinedValues(result);
155
+ return result;
156
+ };
157
+ const requestedWithParamToString = (param, transformFn) => {
158
+ if (typeof param === 'undefined') {
159
+ return;
160
+ }
161
+ if (typeof param === 'function') {
162
+ param = param.toString();
163
+ }
164
+ else if (isMatcher(param)) {
165
+ return (param.constructor.name +
166
+ ' ' +
167
+ (JSON.stringify(param.sample) || ''));
168
+ }
169
+ else if (transformFn && typeof param === 'object' && param !== null) {
170
+ param = transformFn(param);
171
+ }
172
+ if (typeof param === 'string') {
173
+ param = shortenString(param);
174
+ }
175
+ return param;
176
+ };
177
+ const shortenJson = (obj, lengthLimit = STR_LIMIT * 2, keyLimit = KEY_LIMIT) => {
178
+ if (JSON.stringify(obj).length < lengthLimit) {
179
+ return obj;
180
+ }
181
+ if (Array.isArray(obj)) {
182
+ const firstItem = typeof obj[0] === 'object' && obj[0] !== null
183
+ ? shortenJson(obj[0], lengthLimit / 2, keyLimit / 4)
184
+ : shortenString(JSON.stringify(obj[0]));
185
+ return [firstItem, `... ${obj.length - 1} more items`];
186
+ }
187
+ const minifiedObject = {};
188
+ const entries = Object.entries(obj);
189
+ if (keyLimit >= 4) {
190
+ entries.slice(0, keyLimit).forEach(([k, v]) => {
191
+ if (typeof v === 'object' && v !== null) {
192
+ v = shortenJson(v, lengthLimit / 2, keyLimit / 4);
193
+ }
194
+ else if (typeof v === 'string') {
195
+ v = shortenString(v, 16);
196
+ }
197
+ minifiedObject[shortenString(k, 24)] = v;
198
+ });
199
+ }
200
+ if (entries.length > keyLimit) {
201
+ minifiedObject['...'] = `${entries.length} items in total`;
202
+ }
203
+ return minifiedObject;
204
+ };
205
+ const shortenString = (str, limit = STR_LIMIT) => {
206
+ return str.length > limit ? str.substring(0, limit / 2 - 1) + '..' + str.substr(1 - limit / 2) : str;
207
+ };
208
+ const deleteUndefinedValues = (obj, baseline = obj) => {
209
+ Object.keys(obj).forEach((k) => {
210
+ if (typeof baseline[k] === 'undefined') {
211
+ delete obj[k];
212
+ }
213
+ });
214
+ };
215
+ function toBeRequestedWith(...args) {
216
+ return expectAdapter_1.runExpect.call(this, toBeRequestedWithFn, args);
217
+ }
218
+ exports.toBeRequestedWith = toBeRequestedWith;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.toBeRequestedWithResponse = void 0;
4
+ const toBeRequestedWith_1 = require("./toBeRequestedWith");
5
+ const expectAdapter_1 = require("../../util/expectAdapter");
6
+ function toBeRequestedWithResponseFn(received, response, options = {}) {
7
+ return toBeRequestedWith_1.toBeRequestedWithFn.call(this, received, { response }, options);
8
+ }
9
+ function toBeRequestedWithResponse(...args) {
10
+ console.warn('expect(...).toBeRequestedWithResponse is deprecated and will be removed in next release. Use toBeRequestedWith instead.');
11
+ return expectAdapter_1.runExpect.call(this, toBeRequestedWithResponseFn, args);
12
+ }
13
+ exports.toBeRequestedWithResponse = toBeRequestedWithResponse;
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const matchers = {
4
+ ...require('./matchers/browser/toHaveTitle'),
5
+ ...require('./matchers/browser/toHaveTitleContaining'),
6
+ ...require('./matchers/browser/toHaveUrl'),
7
+ ...require('./matchers/browser/toHaveUrlContaining'),
8
+ ...require('./matchers/elements/toBeElementsArrayOfSize'),
9
+ ...require('./matchers/element/toBeClickable'),
10
+ ...require('./matchers/element/toBeDisabled'),
11
+ ...require('./matchers/element/toBeDisplayed'),
12
+ ...require('./matchers/element/toBeDisplayedInViewport'),
13
+ ...require('./matchers/element/toBeEnabled'),
14
+ ...require('./matchers/element/toBeExisting'),
15
+ ...require('./matchers/element/toBeFocused'),
16
+ ...require('./matchers/element/toBeSelected'),
17
+ ...require('./matchers/element/toHaveAttribute'),
18
+ ...require('./matchers/element/toHaveAttributeContaining'),
19
+ ...require('./matchers/element/toHaveChildren'),
20
+ ...require('./matchers/element/toHaveElementClass'),
21
+ ...require('./matchers/element/toHaveElementClassContaining'),
22
+ ...require('./matchers/element/toHaveHref'),
23
+ ...require('./matchers/element/toHaveHrefContaining'),
24
+ ...require('./matchers/element/toHaveId'),
25
+ ...require('./matchers/element/toHaveElementProperty'),
26
+ ...require('./matchers/element/toHaveText'),
27
+ ...require('./matchers/element/toHaveTextContaining'),
28
+ ...require('./matchers/element/toHaveValue'),
29
+ ...require('./matchers/element/toHaveValueContaining'),
30
+ ...require('./matchers/mock/toBeRequested'),
31
+ ...require('./matchers/mock/toBeRequestedTimes'),
32
+ ...require('./matchers/mock/toBeRequestedWith'),
33
+ ...require('./matchers/mock/toBeRequestedWithResponse'),
34
+ };
35
+ Object.keys(matchers).forEach(key => {
36
+ if (key.endsWith('Fn')) {
37
+ delete matchers[key];
38
+ }
39
+ });
40
+ exports.default = matchers;
package/lib/options.js ADDED
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ var _a;
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.setDefaultOptions = exports.getConfig = void 0;
5
+ const DEFAULT_OPTIONS = {
6
+ wait: 2000,
7
+ interval: 100,
8
+ };
9
+ let mode = 'jest';
10
+ let expectLib;
11
+ if (global.expectWdio) {
12
+ expectLib = global.expectWdio;
13
+ }
14
+ else if (global.jasmine && !((_a = global.expect) === null || _a === void 0 ? void 0 : _a.extend)) {
15
+ expectLib = global.jasmine;
16
+ mode = 'jasmine';
17
+ }
18
+ else {
19
+ expectLib = global.expect;
20
+ }
21
+ if (!expectLib._expectWebdriverio) {
22
+ expectLib._expectWebdriverio = { options: { ...DEFAULT_OPTIONS }, mode };
23
+ }
24
+ const getConfig = () => expectLib._expectWebdriverio;
25
+ exports.getConfig = getConfig;
26
+ const setDefaultOptions = (options = {}) => {
27
+ Object.entries(options).forEach(([key, value]) => {
28
+ if (key in DEFAULT_OPTIONS) {
29
+ expectLib._expectWebdriverio.options[key] = value;
30
+ }
31
+ });
32
+ };
33
+ exports.setDefaultOptions = setDefaultOptions;