How to use runSync method in Webdriverio

Best JavaScript code snippet using webdriverio-monorepo

Run Webdriverio automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

test.js

Source: test.js Github

copy
1const curl = require('./curl').curl;
2const browse = require('./browse').browse;
3
4curl('DELETE', '/fruit/apple')
5  .statusShouldMatch(200)
6  .contentShouldMatch({
7    color: null,
8    size: null,
9    name: null
10  })
11  .runSync('Fruit DELETE');
12
13curl('PUT', '/fruit/apple', '{"color":"red","name":"apple","size":"medium"}')
14  .statusShouldMatch(200)
15  .contentShouldMatch({
16    color: null,
17    size: null,
18    name: 'apple'
19  })
20  .runSync('Fruit PUT');
21
22curl('HEAD', '/fruit/apple')
23  .statusShouldMatch(200)
24  .contentShouldMatch('')
25  .runSync('Fruit HEAD');
26
27curl('HEAD', '/fruit/cucumber')
28  .shouldFail(200)
29  .contentShouldMatch('')
30  .runSync('Fruit HEAD fail');
31
32curl('HEAD', '/fruit/apple,pear')
33  .statusShouldMatch(207)
34  .contentShouldMatch({
35    apple: {
36      status: 200,
37      content: ''
38    },
39    pear: {
40      status: 404,
41      content: ''
42    }
43  })
44  .runSync('Fruit HEAD mixed');
45
46curl('GET', '/fruit/banana')
47  .statusShouldMatch(404)
48  .run('Fruit: GET 404');
49
50curl('GET', '/fruit/apple')
51  .statusShouldMatch(200)
52  .contentShouldMatch({
53    color: 'red',
54    size: 'medium',
55    name: 'apple'
56  })
57  .run('Fruit GET');
58
59curl('GET', '/fruit/apple/color;/fruit/grape/color')
60  .statusShouldMatch(200)
61  .contentShouldMatch([
62    'red',
63    'purple'
64  ])
65  .run('Fruit GET multi');
66
67curl('GET', '/fruit/apple/color;/fruit/pear/color')
68  .statusShouldMatch(207)
69  .contentShouldMatch([
70    {
71      status: 200,
72      content: 'red'
73    },
74    {
75      status: 404,
76      content: 'Not found'
77    }
78  ])
79  .run('Fruit GET multi failure');
80
81curl('GET', '/fruit/apple?expand')
82  .statusShouldMatch(200)
83  .contentShouldMatch('apple', ['fruit', 'apple', 'name'])
84  .contentShouldMatch({
85    fruit: {
86      apple: {
87        color: 'red',
88        size: 'medium',
89        name: 'apple'
90      }
91    }
92  })
93  .run('Fruit GET ?expand');
94
95curl('GET', '/fruit/*/color')
96  .statusShouldMatch(200)
97  .contentShouldMatch({
98    grape: 'purple',
99    melon: 'green',
100    apple: 'red',
101    orange: 'orange'
102  })
103  .run('Fruit: get color');
104
105curl('GET', '/drink/wine,orangejuice/name')
106  .statusShouldMatch(200)
107  .contentShouldMatch({orangejuice: 'orangejuice', wine: 'wine'})
108  .run('Drink: get drinks');
109
110curl('PATCH', '/drink/wine/ingredient', 'apple')
111  .statusShouldMatch(200)
112  .runSync('Drink: rename ingredient');
113
114curl('GET', '/drink/wine/ingredient')
115  .statusShouldMatch(200)
116  .contentShouldMatch('apple')
117  .runSync('Drink: check renamed ingredient');
118
119curl('PATCH', '/drink/wine/ingredient', 'grape')
120  .statusShouldMatch(200)
121  .runSync('Drink: undo rename ingredient');
122
123curl('GET', '/drink/wine/ingredient/size')
124  .statusShouldMatch(200)
125  .contentShouldMatch('small')
126  .runSync('Drink: get ingredient size');
127
128curl('GET', '/drink/wine/ingredient.size,name')
129  .statusShouldMatch(200)
130  .contentShouldMatch({
131    ingredient: 'small',
132    name: 'wine'
133  })
134  .runSync('Drink: get name and ingredient size');
135
136curl('GET', '/drink?ingredient==banana')
137  .statusShouldMatch(200)
138  .contentShouldMatch([])
139  .runSync('Drink: empty result from filter search');
140
141curl('GET', '/drink/wiskey')
142  .statusShouldMatch(404)
143  .runSync('Drink: empty result from explicit search');
144
145curl('GET', '/fruit/*?color==green')
146  .statusShouldMatch(200)
147  .contentShouldMatch({
148    melon: {
149      color: 'green',
150      size: 'small',
151      name: 'melon'
152    }
153  })
154  .run('Fruit: filter on color==green');
155
156curl('GET', '/fruit/*/name')
157  .statusShouldMatch(200)
158  .contentShouldMatch({
159    grape: 'grape',
160    melon: 'melon',
161    apple: 'apple',
162    orange: 'orange'
163  })
164  .run('Fruit: No sorting');
165
166curl('GET', '/fruit/*/name?sortBy=color')
167  .statusShouldMatch(200)
168  .contentShouldMatch({
169    grape: 'grape',
170    melon: 'melon',
171    orange: 'orange',
172    apple: 'apple'
173  })
174  .run('Fruit: Sorting');
175
176curl('GET', '/fruit/*/name?sortBy=color&offset=1&limit=2')
177  .contentShouldMatch({
178    orange: 'orange',
179    grape: 'grape'
180  })
181  .run('Fruit: Limit and offset');
182
183curl('GET', '/fruit/*/name?search=range')
184  .contentShouldMatch({
185    orange: 'orange'
186  })
187  .run('Fruit: Search');
188
189curl('GET', '/fruit/*/name?search=non_existing_string')
190  .contentShouldMatch([])
191  .run('Fruit: Search without result');
192
193curl('PATCH', '/fruit/melon/size', 'small')
194  .contentShouldMatch(null)
195  .runSync('Fruit: Patch melon');
196
197curl('PATCH', '/fruit/melon/size?expand', '{"fruit":{"melon":{"size":"small"}}}')
198  .contentShouldMatch({fruit: {melon: {size: null}}})
199  .runSync('Fruit: Patch melon ?expand');
200
201curl('PATCH', '/fruit/melon/size?expand', '{brokenJson')
202  .shouldFail()
203  .contentShouldMatch('Could not parse JSON: Syntax error.')
204  .run('Fruit: Patch melon ?expand broken JSON');
205
206curl('PUT', '/session/member ', '{"login":{"username":"member","password":"member"}}')
207  .statusShouldMatch(200)
208  .contentShouldMatch({login: null})
209  .run('Session: Member login');
210
211curl('PUT', '/session/member ', '{"login":{"username":"member","password":"wrongpassword"}}')
212  .shouldFail()
213  .contentShouldMatch({
214    login: 'Incorrect user-password combination.',
215    groups: 'Forbidden'
216  })
217  .run('Session: Member login faulty password');
218
219/* browse('sample.html')
220    .run('Test browse');
221*/
222
223exports.curl = curl;
224exports.file = require('./file');
225exports.browse = require('./browse');
226
Full Screen

1.js

Source: 1.js Github

copy
1function runSync(id) {
2  const s = new Date().getSeconds();
3  while (true) {
4    if (new Date().getSeconds() - s >= 2) {
5      console.log(`${id} sync 함수 실행`);
6      break;
7    }
8  }
9}
10
11function runAsync(id) {
12  console.log(id + " async 함수 실행");
13}
14
15function executeEventQueue(callStack, eventQueue) {
16  setTimeout(() => {
17    if (callStack.length > 0) executeCallStack(callStack);
18    if (eventQueue.length > 0) callStack.push(eventQueue.shift());
19
20    executeCallStack(callStack, eventQueue);
21  }, 0);
22}
23
24function executeCallStack(callStack) {
25  while (callStack.length > 0) {
26    callStack.pop()();
27  }
28}
29
30//callStack과 eventQueue의 갯수는 1개 이상 n개일 수 있다.
31let callStack = [runSync.bind(null, 1), runSync.bind(null, 2)];
32let eventQueue = [
33  runAsync.bind(null, 1),
34  runAsync.bind(null, 2),
35  runAsync.bind(null, 3),
36];
37
38executeEventQueue(callStack, eventQueue);
39executeCallStack(callStack);
40callStack.push(runSync.bind(null, 3));
41
42setTimeout(() => {
43  callStack.push(runSync.bind(null, 4));
44}, 6000);
45
46setTimeout(() => callStack.push(runSync.bind(null, 5)), 20000);
47
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Webdriverio on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)