expect-webdriverio 1.3.1 → 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.
package/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # expect-webdriverio
2
2
 
3
- [![Build Status](https://travis-ci.org/webdriverio/expect-webdriverio.svg?branch=master)](https://travis-ci.org/webdriverio/expect-webdriverio) [![codecov](https://codecov.io/gh/webdriverio/expect-webdriverio/branch/master/graph/badge.svg)](https://codecov.io/gh/webdriverio/expect-webdriverio)
3
+ [![Test](https://github.com/webdriverio/expect-webdriverio/actions/workflows/test.yml/badge.svg)](https://github.com/webdriverio/expect-webdriverio/actions/workflows/test.yml)
4
4
 
5
- ###### [TypeScript / JS Autocomplete](/docs/Types.md) | [Examples](docs/Examples.md) | [Extending Matchers](/docs/Extend.md)
5
+ ###### [API](docs/API.md) | [TypeScript / JS Autocomplete](/docs/Types.md) | [Examples](docs/Examples.md) | [Extending Matchers](/docs/Extend.md)
6
6
 
7
7
  > [WebdriverIO](https://webdriver.io/) Assertion library inspired by [expect](https://www.npmjs.com/package/expect)
8
8
 
@@ -24,7 +24,7 @@ NOTE: [WebdriverIO](https://github.com/webdriverio/webdriverio) `v5.16.11` or hi
24
24
 
25
25
  In your `wdio.conf.js`
26
26
  ```js
27
- beforeSession () { // before hook works as well
27
+ before () { // not needed in WebdriverIO v6
28
28
  require('expect-webdriverio')
29
29
  },
30
30
  ```
@@ -39,464 +39,7 @@ See more [Examples](docs/Examples.md)
39
39
 
40
40
  ## API
41
41
 
42
- See available [Matchers](https://github.com/webdriverio/expect-webdriverio/blob/master/types/expect-webdriverio.d.ts#L64)
43
-
44
- Besides of the `expect-webdriverio` matchers you can use builtin Jest's [expect](https://jestjs.io/docs/en/expect) assertions or [expect/expectAsync](https://jasmine.github.io/api/3.5/global.html#expect) for Jasmine.
45
-
46
- ## Default Options
47
-
48
- ```js
49
- {
50
- wait: 2000, // ms to wait for expectation to succeed
51
- interval: 100, // interval between attempts
52
- }
53
- ```
54
-
55
- Set options like this:
56
- ```js
57
- beforeSession () { // before hook works as well
58
- require('expect-webdriverio').setOptions({ wait: 5000 })
59
- },
60
- ```
61
-
62
- ### Matcher Options
63
-
64
- Every matcher can take several options that allows you to modify the assertion:
65
-
66
- ##### Command Options
67
-
68
- | Name | Type | Details |
69
- | ---- | ---- | ------- |
70
- | <code><var>wait</var></code> | number | time in ms to wait for expectation to succeed. Default: `3000` |
71
- | <code><var>interval</var></code> | number | interval between attempts. Default: `100` |
72
- | <code><var>message</var></code> | string | user message to prepend before assertion error |
73
-
74
- ##### String Options
75
-
76
- This options can be applied in addition to the command options when strings are being asserted.
77
-
78
- | Name | Type | Details |
79
- | ---- | ---- | ------- |
80
- | <code><var>ignoreCase</var></code> | boolean | apply `toLowerCase` to both actual and expected values |
81
- | <code><var>trim</var></code> | boolean | apply `trim` to actual value |
82
- | <code><var>containing</var></code> | boolean | expect actual value to contain expected value, otherwise strict equal. |
83
- | <code><var>asString</var></code> | boolean | might be helpful to force converting property value to string |
84
-
85
- ##### Number Options
86
-
87
- This options can be applied in addition to the command options when numbers are being asserted.
88
-
89
- | Name | Type | Details |
90
- | ---- | ---- | ------- |
91
- | <code><var>eq</var></code> | number | equals |
92
- | <code><var>lte</var></code> | number | less then equals |
93
- | <code><var>gte</var></code> | number | greater than or equals |
94
-
95
- ## Browser Matchers
96
-
97
- ### toHaveUrl
98
-
99
- Checks if browser is on a specific page.
100
-
101
- ##### Usage
102
-
103
- ```js
104
- browser.url('https://webdriver.io/')
105
- expect(browser).toHaveUrl('https://webdriver.io')
106
- ```
107
-
108
- ### toHaveUrlContaining
109
-
110
- Checks if browser is on a specific page and contains the given value in URL.
111
-
112
- ##### Usage
113
-
114
- ```js
115
- browser.url('https://webdriver.io/')
116
- expect(browser).toHaveUrlContaining('webdriver.io')
117
- ```
118
-
119
- ### toHaveTitle
120
-
121
- Checks if website has a specific title.
122
-
123
- ##### Usage
124
-
125
- ```js
126
- browser.url('https://webdriver.io/')
127
- expect(browser).toHaveTitle('WebdriverIO · Next-gen browser and mobile automation test framework for Node.js')
128
- ```
129
-
130
- ### toHaveTitleContaining
131
-
132
- Checks if website has a specific title that contains the given value.
133
-
134
- ##### Usage
135
-
136
- ```js
137
- browser.url('https://webdriver.io/')
138
- expect(browser).toHaveTitleContaining('Next-gen browser and mobile automation test framework')
139
- ```
140
-
141
- ## Element Matchers
142
-
143
- ### toBeDisplayed
144
-
145
- Calls [`isDisplayed`](/docs/api/element/isDisplayed.html) on given element.
146
-
147
- ##### Usage
148
-
149
- ```js
150
- const elem = $('#someElem')
151
- expect(elem).toBeDisplayed()
152
- ```
153
-
154
- ### toBeVisible
155
-
156
- Same as `toBeDisplayed`.
157
-
158
- ##### Usage
159
-
160
- ```js
161
- const elem = $('#someElem')
162
- expect(elem).toBeVisible()
163
- ```
164
-
165
- ### toExist
166
-
167
- Calls [`isExisting`](/docs/api/element/isExisting.html) on given element.
168
-
169
- ##### Usage
170
-
171
- ```js
172
- const elem = $('#someElem')
173
- expect(elem).toExist()
174
- ```
175
-
176
- ### toBePresent
177
-
178
- Same as `toExist`.
179
-
180
- ##### Usage
181
-
182
- ```js
183
- const elem = $('#someElem')
184
- expect(elem).toBePresent()
185
- ```
186
-
187
- ### toBeExisting
188
-
189
- Same as `toExist`.
190
-
191
- ##### Usage
192
-
193
- ```js
194
- const elem = $('#someElem')
195
- expect(elem).toBeExisting()
196
- ```
197
-
198
- ### toBeFocused
199
-
200
- Checks if element has focus. This assertion only works in a web context.
201
-
202
- ##### Usage
203
-
204
- ```js
205
- const elem = $('#someElem')
206
- expect(elem).toBeFocused()
207
- ```
208
-
209
- ### toHaveAttribute
210
-
211
- Checks if an element has a certain attribute with a specific value.
212
-
213
- ##### Usage
214
-
215
- ```js
216
- const myInput = $('input')
217
- expect(myInput).toHaveAttribute('class', 'form-control')
218
- ```
219
-
220
- ### toHaveAttr
221
-
222
- Same as `toHaveAttribute`.
223
-
224
- ##### Usage
225
-
226
- ```js
227
- const myInput = $('input')
228
- expect(myInput).toHaveAttr('class', 'form-control')
229
- ```
230
-
231
- ### toHaveAttributeContaining
232
-
233
- Checks if an element has a certain attribute that contains a value.
234
-
235
- ##### Usage
236
-
237
- ```js
238
- const myInput = $('input')
239
- expect(myInput).toHaveAttributeContaining('class', 'form')
240
- ```
241
-
242
- ### toHaveAttrContaining
243
-
244
- Same as `toHaveAttributeContaining`.
245
-
246
- ##### Usage
247
-
248
- ```js
249
- const myInput = $('input')
250
- expect(myInput).toHaveAttrContaining('class', 'form')
251
- ```
252
-
253
- ### toHaveElementClass
254
-
255
- Checks if an element has a certain class name.
256
-
257
- ##### Usage
258
-
259
- ```js
260
- const myInput = $('input')
261
- expect(myInput).toHaveElementClass('form-control', { message: 'Not a form control!', })
262
- ```
263
-
264
- ### toHaveElementClassContaining
265
-
266
- Checks if an element has a certain class name that contains provided value.
267
-
268
- ##### Usage
269
-
270
- ```js
271
- const myInput = $('input')
272
- expect(myInput).toHaveElementClassContaining('form')
273
- ```
274
-
275
- ### toHaveElementProperty
276
-
277
- Checks if an element has a certain property.
278
-
279
- ##### Usage
280
-
281
- ```js
282
- const elem = $('#elem')
283
- expect(elem).toHaveElementProperty('height', 23)
284
- expect(elem).not.toHaveElementProperty('height', 0)
285
- ```
286
-
287
- ### toHaveValue
288
-
289
- Checks if an input element has a certain value.
290
-
291
- ##### Usage
292
-
293
- ```js
294
- const myInput = $('input')
295
- expect(myInput).toHaveValue('user', { ignoreCase: true })
296
- ```
297
-
298
- ### toHaveValueContaining
299
-
300
- Checks if an input element contains a certain value.
301
-
302
- ##### Usage
303
-
304
- ```js
305
- const myInput = $('input')
306
- expect(myInput).toHaveValueContaining('us')
307
- ```
308
-
309
- ### toBeClickable
310
-
311
- Checks if an element can be clicked by calling [`isClickable`](/docs/api/element/isClickable.html) on the element.
312
-
313
- ##### Usage
314
-
315
- ```js
316
- const elem = $('#elem')
317
- expect(elem).toBeClickable()
318
- ```
319
-
320
- ### toBeDisabled
321
-
322
- Checks if an element is disabled by calling [`isEnabled`](/docs/api/element/isEnabled.html) on the element.
323
-
324
- ##### Usage
325
-
326
- ```js
327
- const elem = $('#elem')
328
- expect(elem).toBeDisabled()
329
- // same as
330
- expect(elem).not.toBeEnabled()
331
- ```
332
-
333
- ### toBeEnabled
334
-
335
- Checks if an element is enabled by calling [`isEnabled`](/docs/api/element/isEnabled.html) on the element.
336
-
337
- ##### Usage
338
-
339
- ```js
340
- const elem = $('#elem')
341
- expect(elem).toBeEnabled()
342
- // same as
343
- expect(elem).not.toBeDisabled()
344
- ```
345
-
346
- ### toBeSelected
347
-
348
- Checks if an element is enabled by calling [`isSelected`](/docs/api/element/isSelected.html) on the element.
349
-
350
- ##### Usage
351
-
352
- ```js
353
- const elem = $('#elem')
354
- expect(elem).toBeSelected()
355
- ```
356
-
357
- ### toBeChecked
358
-
359
- Same as `toBeSelected`.
360
-
361
- ##### Usage
362
-
363
- ```js
364
- const elem = $('#elem')
365
- expect(elem).toBeChecked()
366
- ```
367
-
368
- ### toHaveHref
369
-
370
- Checks if link element has a specific link target.
371
-
372
- ##### Usage
373
-
374
- ```js
375
- const link = $('a')
376
- expect(link).toHaveHref('https://webdriver.io')
377
- ```
378
-
379
- ### toHaveLink
380
-
381
- Same as `toHaveHref`.
382
-
383
- ##### Usage
384
-
385
- ```js
386
- const link = $('a')
387
- expect(link).toHaveLink('https://webdriver.io')
388
- ```
389
-
390
- ### toHaveHrefContaining
391
-
392
- Checks if link element contains a specific link target.
393
-
394
- ##### Usage
395
-
396
- ```js
397
- const link = $('a')
398
- expect(link).toHaveHrefContaining('webdriver.io')
399
- ```
400
-
401
- ### toHaveLinkContaining
402
-
403
- Same as `toHaveHrefContaining`.
404
-
405
- ##### Usage
406
-
407
- ```js
408
- const link = $('a')
409
- expect(link).toHaveLinkContaining('webdriver.io')
410
- ```
411
-
412
- ### toHaveId
413
-
414
- Checks if element has a specific `id` attribute.
415
-
416
- ##### Usage
417
-
418
- ```js
419
- const elem = $('#elem')
420
- expect(elem).toHaveId('elem')
421
- ```
422
-
423
- ### toHaveText
424
-
425
- Checks if element has a specific text.
426
-
427
- ##### Usage
428
-
429
- ```js
430
- browser.url('https://webdriver.io/')
431
- const elem = $('.tagline')
432
- expect(elem).toHaveText('Next-gen browser and mobile automation test framework for Node.js')
433
- ```
434
-
435
- ### toHaveTextContaining
436
-
437
- Checks if element contains a specific text.
438
-
439
- ##### Usage
440
-
441
- ```js
442
- browser.url('https://webdriver.io/')
443
- const elem = $('.tagline')
444
- expect(elem).toHaveTextContaining('browser and mobile automation test framework')
445
- ```
446
-
447
- ### toBeDisplayedInViewport
448
-
449
- Checks if an element is within the viewport by calling [`isDisplayedInViewport`](/docs/api/element/isDisplayedInViewport.html) on the element.
450
-
451
- ##### Usage
452
-
453
- ```js
454
- const elem = $('#elem')
455
- expect(elem).toBeDisplayedInViewport()
456
- ```
457
-
458
- ### toBeVisibleInViewport
459
-
460
- Same as `toBeDisplayedInViewport`.
461
-
462
- ##### Usage
463
-
464
- ```js
465
- const elem = $('#elem')
466
- expect(elem).toBeVisibleInViewport()
467
- ```
468
-
469
- ### toHaveChildren
470
-
471
- Checks amount of the element's children by calling `element.$('./*')` command.
472
-
473
- ##### Usage
474
-
475
- ```js
476
- const list = $('ul')
477
- expect(list).toHaveChildren() // the list has at least one item
478
- // same as
479
- expect(list).toHaveChildren({ gte: 1 })
480
-
481
- expect(list).toHaveChildren(3) // the list has 3 items
482
- // same as
483
- expect(list).toHaveChildren({ eq: 3 })
484
- ```
485
-
486
- ### toBeElementsArrayOfSize
487
-
488
- Checks amount of fetched elements using [`$$`](/docs/api/browser/$$.html) command.
489
-
490
- ##### Usage
491
-
492
- ```js
493
- const listItems = $$('ul>li')
494
- expect(listItems).toBeElementsArrayOfSize(5) // 5 items in the list
495
-
496
- expect(listItems).toBeElementsArrayOfSize({ lte: 10 })
497
- // same as
498
- assert.ok(listItems.length <= 10)
499
- ```
42
+ Please see [API doc](docs/API.md)
500
43
 
501
44
  ## Error messages
502
45
 
package/jasmine.d.ts CHANGED
@@ -2,5 +2,5 @@
2
2
 
3
3
  declare module jasmine {
4
4
  interface Matchers<T> extends ExpectWebdriverIO.Matchers<any, T> {}
5
- interface AsyncMatchers<T, U> extends ExpectWebdriverIO.Matchers<any, T> {}
5
+ interface AsyncMatchers<T, U> extends ExpectWebdriverIO.Matchers<Promise<void>, T> {}
6
6
  }
@@ -2,10 +2,11 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.addMatchers = exports.loadExpect = exports.isJasmine = void 0;
4
4
  let expectLib;
5
- exports.isJasmine = () => {
5
+ const isJasmine = () => {
6
6
  return Boolean(global.jasmine && global.expect && global.expectAsync && global.jasmine.getEnv && global.jasmine.addMatchers && global.jasmine.addAsyncMatchers);
7
7
  };
8
- exports.loadExpect = () => {
8
+ exports.isJasmine = isJasmine;
9
+ const loadExpect = () => {
9
10
  try {
10
11
  return expectLib = require('expect');
11
12
  }
@@ -13,7 +14,8 @@ exports.loadExpect = () => {
13
14
  return console.error('Failed to load expect package. Make sure it has been installed: npm i expect');
14
15
  }
15
16
  };
16
- exports.addMatchers = () => {
17
+ exports.loadExpect = loadExpect;
18
+ const addMatchers = () => {
17
19
  if (global.expect === undefined) {
18
20
  if (!exports.loadExpect()) {
19
21
  return;
@@ -43,3 +45,4 @@ exports.addMatchers = () => {
43
45
  expectLib.addAsyncMatchers({ ...matchers });
44
46
  });
45
47
  };
48
+ exports.addMatchers = addMatchers;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.toHaveProperty = exports.toHaveElementProperty = exports.toHaveElementPropertyFn = void 0;
3
+ exports.toHaveElementProperty = exports.toHaveElementPropertyFn = void 0;
4
4
  const utils_1 = require("../../utils");
5
5
  const expectAdapter_1 = require("../../util/expectAdapter");
6
6
  async function condition(el, property, value, options = {}) {
@@ -41,7 +41,7 @@ function toHaveElementPropertyFn(received, property, value, options = {}) {
41
41
  }
42
42
  return {
43
43
  pass,
44
- message: () => message
44
+ message: () => message,
45
45
  };
46
46
  });
47
47
  }
@@ -50,8 +50,3 @@ function toHaveElementProperty(...args) {
50
50
  return expectAdapter_1.runExpect.call(this, toHaveElementPropertyFn, args);
51
51
  }
52
52
  exports.toHaveElementProperty = toHaveElementProperty;
53
- function toHaveProperty(...args) {
54
- console.warn('expect(...).toHaveProperty is deprecated. Use toHaveElementProperty instead.');
55
- return expectAdapter_1.runExpect.call(this, toHaveElementPropertyFn, args);
56
- }
57
- exports.toHaveProperty = toHaveProperty;
@@ -5,6 +5,9 @@ const utils_1 = require("../../utils");
5
5
  const expectAdapter_1 = require("../../util/expectAdapter");
6
6
  async function condition(el, text, options) {
7
7
  const actualText = await el.getText();
8
+ if (Array.isArray(text)) {
9
+ return utils_1.compareTextWithArray(actualText, text, options);
10
+ }
8
11
  return utils_1.compareText(actualText, text, options);
9
12
  }
10
13
  function toHaveTextFn(received, text, options = {}) {
@@ -1,23 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.toBeRequested = void 0;
4
- const utils_1 = require("../../utils");
4
+ const toBeRequestedTimes_1 = require("./toBeRequestedTimes");
5
5
  const expectAdapter_1 = require("../../util/expectAdapter");
6
6
  function toBeRequestedFn(received, options = {}) {
7
- const isNot = this.isNot || false;
8
- const { expectation = `called`, verb = 'be' } = this;
9
- return browser.call(async () => {
10
- let actual;
11
- const pass = await utils_1.waitUntil(async () => {
12
- actual = received.calls.length;
13
- return actual > 0;
14
- }, isNot, options);
15
- const message = utils_1.enhanceError('mock', 0, actual, this, verb, expectation, '', options);
16
- return {
17
- pass,
18
- message: () => message
19
- };
20
- });
7
+ return toBeRequestedTimes_1.toBeRequestedTimes.call({ ...(this || {}), expectation: 'called' }, received, { ...options, gte: 1 });
21
8
  }
22
9
  function toBeRequested(...args) {
23
10
  return expectAdapter_1.runExpect.call(this, toBeRequestedFn, args);
@@ -1,21 +1,27 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.toBeRequestedTimes = void 0;
3
+ exports.toBeRequestedTimes = exports.toBeRequestedTimesFn = void 0;
4
4
  const utils_1 = require("../../utils");
5
5
  const expectAdapter_1 = require("../../util/expectAdapter");
6
6
  const formatMessage_1 = require("../../util/formatMessage");
7
7
  function toBeRequestedTimesFn(received, times = {}, options = {}) {
8
8
  const isNot = this.isNot || false;
9
9
  const { expectation = `called${typeof times === 'number' ? ' ' + times : ''} time${times !== 1 ? 's' : ''}`, verb = 'be' } = this;
10
- const eq = typeof times === 'number' ? times : times.eq;
11
- const gte = typeof times === 'number' ? 1 : (times.gte || 1);
12
- const lte = typeof times === 'number' ? 0 : (times.lte || 0);
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;
13
19
  return browser.call(async () => {
14
20
  let actual;
15
21
  const pass = await utils_1.waitUntil(async () => {
16
22
  actual = received.calls.length;
17
23
  return utils_1.compareNumbers(actual, gte, lte, eq);
18
- }, isNot, options);
24
+ }, isNot, { ...options, wait: isNot ? 0 : options.wait });
19
25
  const error = formatMessage_1.numberError(gte, lte, eq);
20
26
  const message = utils_1.enhanceError('mock', error, actual, this, verb, expectation, '', options);
21
27
  return {
@@ -24,6 +30,7 @@ function toBeRequestedTimesFn(received, times = {}, options = {}) {
24
30
  };
25
31
  });
26
32
  }
33
+ exports.toBeRequestedTimesFn = toBeRequestedTimesFn;
27
34
  function toBeRequestedTimes(...args) {
28
35
  return expectAdapter_1.runExpect.call(this, toBeRequestedTimesFn, args);
29
36
  }