UNPKG

3.27 kBMarkdownView Raw
1WebdriverIO Sync
2================
3
4> A WebdriverIO plugin. Helper module to run WebdriverIO commands synchronously
5
6A WebdriverIO plugin. Helper module to run WebdriverIO commands synchronously. It overwrites global functions depending on the test framework (e.g. for Mocha describe and it) and uses Fibers to make commands of WebdriverIO using the wdio testrunner synchronous. This package is consumed by all wdio framework adapters.
7
8## Usage
9
10### Using WDIO Testrunner
11
12If you are using the WDIO testrunner all you need to make all your specs run synchronous is to have `@wdio/sync` installed in your project. The testrunner automatically will detect it and transform the commands to make a test like this:
13
14```js
15describe('webdriver.io page', () => {
16 it('should have the right title', async () => {
17 await browser.url('https://webdriver.io')
18 await expect(browser)
19 .toHaveTitle('WebdriverIO · Next-gen browser and mobile automation test framework for Node.js | WebdriverIO')
20 })
21})
22```
23
24easier to read and write like this:
25
26```js
27describe('webdriver.io page', () => {
28 it('should have the right title', () => {
29 browser.url('https://webdriver.io')
30 expect(browser).toHaveTitle('WebdriverIO · Next-gen browser and mobile automation test framework for Node.js | WebdriverIO')
31 })
32})
33```
34
35### Using WebdriverIO as standalone package
36
37Given you have a simple standalone WebdriverIO script like this:
38
39```js
40// standalone.js
41const { remote } = require('webdriverio')
42const sync = require('@wdio/sync').default
43
44;(() => {
45 const browser = await remote({
46 outputDir: __dirname,
47 capabilities: {
48 browserName: 'chrome'
49 }
50 })
51
52 await browser.url('https://webdriver.io')
53 console.log(await browser.getTitle())
54 await browser.deleteSession()
55})().catch(console.error)
56```
57
58With this package you can make WebdriverIO commands synchronous by using the `sync` wrapper:
59
60```js
61// standalone.js
62const { remote } = require('webdriverio')
63const sync = require('@wdio/sync').default
64
65remote({
66 runner: 'local',
67 outputDir: __dirname,
68 capabilities: {
69 browserName: 'chrome'
70 }
71}).then((browser) => sync(() => {
72 /**
73 * sync code from here on
74 */
75 browser.url('https://webdriver.io')
76 console.log(browser.getTitle())
77 browser.deleteSession()
78}))
79```
80
81## Switching Between Sync And Async
82
83While using `@wdio/sync` you can still switch between both by using the `browser.call()` command. It allows you to run async code and return the result into a synchronous environment. For example:
84
85```js
86describe('webdriver.io page', () => {
87 it('should have the right title', () => {
88 /**
89 * synchronous execution here
90 */
91 browser.url('https://webdriver.io')
92
93 const result = browser.call(async () => {
94 /**
95 * asynchronous execution here
96 */
97 await browser.url('https://google.com')
98 return Promise.all([
99 browser.getTitle(),
100 browser.getUrl()
101 ])
102 })
103
104 /**
105 * synchronous execution here
106 */
107 console.log(result) // returns: ['Google', 'https://www.google.com/']
108 })
109})
110```