expect-webdriverio 4.2.8 → 4.3.0

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.
package/lib/utils.js CHANGED
@@ -67,7 +67,7 @@ const compareNumbers = (actual, options = {}) => {
67
67
  }
68
68
  return false;
69
69
  };
70
- export const compareText = (actual, expected, { ignoreCase = false, trim = true, containing = false }) => {
70
+ export const compareText = (actual, expected, { ignoreCase = false, trim = true, containing = false, atStart = false, atEnd = false, atIndex, replace }) => {
71
71
  if (typeof actual !== 'string') {
72
72
  return {
73
73
  value: actual,
@@ -77,6 +77,9 @@ export const compareText = (actual, expected, { ignoreCase = false, trim = true,
77
77
  if (trim) {
78
78
  actual = actual.trim();
79
79
  }
80
+ if (Array.isArray(replace)) {
81
+ actual = replaceActual(replace, actual);
82
+ }
80
83
  if (ignoreCase) {
81
84
  actual = actual.toLowerCase();
82
85
  if (!(expected instanceof RegExp)) {
@@ -95,12 +98,30 @@ export const compareText = (actual, expected, { ignoreCase = false, trim = true,
95
98
  result: actual.includes(expected)
96
99
  };
97
100
  }
101
+ if (atStart) {
102
+ return {
103
+ value: actual,
104
+ result: actual.startsWith(expected)
105
+ };
106
+ }
107
+ if (atEnd) {
108
+ return {
109
+ value: actual,
110
+ result: actual.endsWith(expected)
111
+ };
112
+ }
113
+ if (atIndex) {
114
+ return {
115
+ value: actual,
116
+ result: actual.substring(atIndex, actual.length).startsWith(expected)
117
+ };
118
+ }
98
119
  return {
99
120
  value: actual,
100
121
  result: actual === expected
101
122
  };
102
123
  };
103
- export const compareTextWithArray = (actual, expectedArray, { ignoreCase = false, trim = false, containing = false }) => {
124
+ export const compareTextWithArray = (actual, expectedArray, { ignoreCase = false, trim = false, containing = false, atStart = false, atEnd = false, atIndex, replace }) => {
104
125
  if (typeof actual !== 'string') {
105
126
  return {
106
127
  value: actual,
@@ -110,12 +131,30 @@ export const compareTextWithArray = (actual, expectedArray, { ignoreCase = false
110
131
  if (trim) {
111
132
  actual = actual.trim();
112
133
  }
134
+ if (Array.isArray(replace)) {
135
+ actual = replaceActual(replace, actual);
136
+ }
113
137
  if (ignoreCase) {
114
138
  actual = actual.toLowerCase();
115
139
  expectedArray = expectedArray.map(item => (item instanceof RegExp) ? item : item.toLowerCase());
116
140
  }
117
141
  const textInArray = expectedArray.some((expected) => {
118
- return expected instanceof RegExp ? !!actual.match(expected) : containing ? actual.includes(expected) : actual === expected;
142
+ if (expected instanceof RegExp) {
143
+ return !!actual.match(expected);
144
+ }
145
+ if (containing) {
146
+ return actual.includes(expected);
147
+ }
148
+ if (atStart) {
149
+ return actual.startsWith(expected);
150
+ }
151
+ if (atEnd) {
152
+ return actual.endsWith(expected);
153
+ }
154
+ if (atIndex) {
155
+ return actual.substring(atIndex, actual.length).startsWith(expected);
156
+ }
157
+ return actual === expected;
119
158
  });
120
159
  return {
121
160
  value: actual,
@@ -151,3 +190,17 @@ function aliasFn(fn, { verb, expectation } = {}, ...args) {
151
190
  return fn.apply(this, args);
152
191
  }
153
192
  export { updateElementsArray, wrapExpectedWithArray, enhanceError, numberError, executeCommand, executeCommandBe, waitUntil, compareNumbers, aliasFn };
193
+ function replaceActual(replace, actual) {
194
+ const hasMultipleReplacers = replace.every(r => Array.isArray(r));
195
+ const replacers = hasMultipleReplacers
196
+ ? replace
197
+ : [replace];
198
+ if (replacers.some(r => Array.isArray(r) && r.length !== 2)) {
199
+ throw new Error('Replacers need to have a searchValue and a replaceValue');
200
+ }
201
+ for (const replacer of replacers) {
202
+ const [searchValue, replaceValue] = replacer;
203
+ actual = actual.replace(searchValue, replaceValue);
204
+ }
205
+ return actual;
206
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "expect-webdriverio",
3
- "version": "4.2.8",
3
+ "version": "4.3.0",
4
4
  "author": "Mykola Grybyk <mykola.grybyk@gmail.com>",
5
5
  "description": "WebdriverIO Assertion Library",
6
6
  "license": "MIT",
@@ -75,7 +75,7 @@
75
75
  "rimraf": "^5.0.1",
76
76
  "shelljs": "^0.8.5",
77
77
  "typescript": "^5.2.2",
78
- "vitest": "0.34.4",
78
+ "vitest": "0.34.5",
79
79
  "webdriverio": "^8.16.7"
80
80
  },
81
81
  "optionalDependencies": {
@@ -1,11 +1,23 @@
1
+ type searchValue = string | RegExp
2
+ type replaceValue = string | ((substring: string, ...args: any[]) => string)
3
+ type Replacer = [searchValue, replaceValue]
4
+
1
5
  declare namespace ExpectWebdriverIO {
2
- function expect<T = unknown, R extends void | Promise<void> = void | Promise<void>>(actual: T): Matchers<R, T>
6
+ function expect<T = unknown, R extends void | Promise<void> = void | Promise<void>>(
7
+ actual: T
8
+ ): Matchers<R, T>
3
9
  function setOptions(options: DefaultOptions): void
4
10
  function getConfig(): any
5
- const matchers: Record<string, ((actual: any, ...expected: any[]) => Promise<{
6
- pass: boolean;
7
- message(): string;
8
- }>)>
11
+ const matchers: Record<
12
+ string,
13
+ (
14
+ actual: any,
15
+ ...expected: any[]
16
+ ) => Promise<{
17
+ pass: boolean
18
+ message(): string
19
+ }>
20
+ >
9
21
 
10
22
  interface DefaultOptions {
11
23
  /**
@@ -43,6 +55,30 @@ declare namespace ExpectWebdriverIO {
43
55
  */
44
56
  containing?: boolean
45
57
 
58
+ /**
59
+ * expect actual value to start with the expected value
60
+ * Otherwise strict equal
61
+ */
62
+ atStart?: boolean
63
+
64
+ /**
65
+ * expect actual value to end with the expected value
66
+ * Otherwise strict equal
67
+ */
68
+ atEnd?: boolean
69
+
70
+ /**
71
+ * expect actual value to have the expected value at the given index (index starts at 0 not 1)
72
+ * Otherwise strict equal
73
+ */
74
+ atIndex?: number
75
+
76
+ /**
77
+ * replace the actual value (example: strip newlines from the value) and expect it to match the expected value
78
+ * Otherwise strict equal
79
+ */
80
+ replace?: Replacer | Replacer[]
81
+
46
82
  /**
47
83
  * might be helpful to force converting property value to string
48
84
  */
@@ -89,7 +125,11 @@ declare namespace ExpectWebdriverIO {
89
125
  /**
90
126
  * `WebdriverIO.Element` -> `getAttribute`
91
127
  */
92
- toHaveAttribute(attribute: string, value?: string | RegExp, options?: ExpectWebdriverIO.StringOptions): R
128
+ toHaveAttribute(
129
+ attribute: string,
130
+ value?: string | RegExp,
131
+ options?: ExpectWebdriverIO.StringOptions
132
+ ): R
93
133
  /**
94
134
  * `WebdriverIO.Element` -> `getAttribute`
95
135
  */
@@ -141,7 +181,11 @@ declare namespace ExpectWebdriverIO {
141
181
  /**
142
182
  * `WebdriverIO.Element` -> `getProperty`
143
183
  */
144
- toHaveElementProperty(property: string | RegExp, value?: any, options?: ExpectWebdriverIO.StringOptions): R
184
+ toHaveElementProperty(
185
+ property: string | RegExp,
186
+ value?: any,
187
+ options?: ExpectWebdriverIO.StringOptions
188
+ ): R
145
189
 
146
190
  /**
147
191
  * `WebdriverIO.Element` -> `getProperty` value
@@ -225,17 +269,23 @@ declare namespace ExpectWebdriverIO {
225
269
  /**
226
270
  * `WebdriverIO.Element` -> `getText`
227
271
  */
228
- toHaveText(text: string | RegExp | Array<string | RegExp>, options?: ExpectWebdriverIO.StringOptions): R
272
+ toHaveText(
273
+ text: string | RegExp | Array<string | RegExp>,
274
+ options?: ExpectWebdriverIO.StringOptions
275
+ ): R
229
276
  /**
230
277
  * `WebdriverIO.Element` -> `getText`
231
278
  * Element's text includes the text provided
232
279
  */
233
- toHaveTextContaining(text: string | RegExp | Array<string | RegExp>, options?: ExpectWebdriverIO.StringOptions): R
280
+ toHaveTextContaining(
281
+ text: string | RegExp | Array<string | RegExp>,
282
+ options?: ExpectWebdriverIO.StringOptions
283
+ ): R
234
284
 
235
285
  /**
236
- * `WebdriverIO.Element` -> `getAttribute("style")`
237
- */
238
- toHaveStyle(style: { [key: string]: string; }, options?: ExpectWebdriverIO.StringOptions): R
286
+ * `WebdriverIO.Element` -> `getAttribute("style")`
287
+ */
288
+ toHaveStyle(style: { [key: string]: string }, options?: ExpectWebdriverIO.StringOptions): R
239
289
 
240
290
  // ===== browser only =====
241
291
  /**