5
5
i18nReady : true
6
6
---
7
7
8
- { /* TODO: REVISE COPY TO V2 */ }
9
-
10
8
import CommandTabs from ' @components/CommandTabs.astro' ;
11
9
12
10
:::note
@@ -16,14 +14,14 @@ Make sure to go through the [prerequisites instructions] to be able to follow th
16
14
:::
17
15
18
16
This WebDriver testing example will use [ WebdriverIO] , and its testing suite. It is expected to have Node.js already
19
- installed, along with ` npm ` or ` yarn ` although the [ finished example project] uses ` yarn ` .
17
+ installed, along with ` npm ` or ` yarn ` although the [ finished example project] uses ` pnpm ` .
20
18
21
19
## Create a Directory for the Tests
22
20
23
21
Let's create a space to write these tests in our project. We will be using a nested directory for
24
22
this example project as we will later also go over other frameworks, but typically you only need to use one. Create
25
- the directory we will use with ` mkdir -p webdriver/webdriverio ` . The rest of this guide assumes you are inside the
26
- ` webdriver/webdriverio ` directory.
23
+ the directory we will use with ` mkdir e2e-tests ` . The rest of this guide assumes you are inside the
24
+ ` e2e-tests ` directory.
27
25
28
26
## Initializing a WebdriverIO Project
29
27
@@ -38,16 +36,17 @@ guide on setting it up from scratch.
38
36
"name" : " webdriverio" ,
39
37
"version" : " 1.0.0" ,
40
38
"private" : true ,
39
+ "type" : " module" ,
41
40
"scripts" : {
42
41
"test" : " wdio run wdio.conf.js"
43
42
},
44
43
"dependencies" : {
45
- "@wdio/cli" : " ^7.9.1 "
44
+ "@wdio/cli" : " ^9.19.0 "
46
45
},
47
46
"devDependencies" : {
48
- "@wdio/local-runner" : " ^7.9.1 " ,
49
- "@wdio/mocha-framework" : " ^7.9.1 " ,
50
- "@wdio/spec-reporter" : " ^7.9 .0"
47
+ "@wdio/local-runner" : "^9.19.0 ,
48
+ "@wdio/mocha-framework" : " ^9.19.0 " ,
49
+ "@wdio/spec-reporter" : " ^9.19 .0"
51
50
}
52
51
}
53
52
```
@@ -80,21 +79,27 @@ config file which controls most aspects of our testing suite.
80
79
` wdio.conf.js ` :
81
80
82
81
``` javascript
83
- const os = require (' os' );
84
- const path = require (' path' );
85
- const { spawn , spawnSync } = require (' child_process' );
82
+ import os from ' os' ;
83
+ import path from ' path' ;
84
+ import { spawn , spawnSync } from ' child_process' ;
85
+ import { fileURLToPath } from ' url' ;
86
+
87
+ const __dirname = fileURLToPath (new URL (' .' , import .meta.url));
86
88
87
89
// keep track of the `tauri-driver` child process
88
90
let tauriDriver;
91
+ let exit = false ;
89
92
90
- exports .config = {
93
+ export const config = {
94
+ host: ' 127.0.0.1' ,
95
+ port: 4444 ,
91
96
specs: [' ./develop/tests/specs/**/*.js' ],
92
97
maxInstances: 1 ,
93
98
capabilities: [
94
99
{
95
100
maxInstances: 1 ,
96
101
' tauri:options' : {
97
- application: ' ../.. /target/release/hello- tauri-webdriver ' ,
102
+ application: ' ../src-tauri /target/debug/ tauri-app ' ,
98
103
},
99
104
},
100
105
],
@@ -106,24 +111,71 @@ exports.config = {
106
111
},
107
112
108
113
// ensure the rust project is built since we expect this binary to exist for the webdriver sessions
109
- onPrepare : () => spawnSync (' cargo' , [' build' , ' --release' ]),
114
+ onPrepare : () => {
115
+ spawnSync (' yarn' , [' tauri' , ' build' , ' --debug' , ' --no-bundle' ], {
116
+ cwd: path .resolve (__dirname , ' ..' ),
117
+ stdio: ' inherit' ,
118
+ shell: true ,
119
+ });
120
+ },
110
121
111
122
// ensure we are running `tauri-driver` before the session starts so that we can proxy the webdriver requests
112
- beforeSession : () =>
113
- ( tauriDriver = spawn (
123
+ beforeSession : () => {
124
+ tauriDriver = spawn (
114
125
path .resolve (os .homedir (), ' .cargo' , ' bin' , ' tauri-driver' ),
115
126
[],
116
127
{ stdio: [null , process .stdout , process .stderr ] }
117
- )),
128
+ );
129
+
130
+ tauriDriver .on (' error' , (error ) => {
131
+ console .error (' tauri-driver error:' , error);
132
+ process .exit (1 );
133
+ });
134
+ tauriDriver .on (' exit' , (code ) => {
135
+ if (! exit) {
136
+ console .error (' tauri-driver exited with code:' , code);
137
+ process .exit (1 );
138
+ }
139
+ });
140
+ },
118
141
119
142
// clean up the `tauri-driver` process we spawned at the start of the session
120
- afterSession : () => tauriDriver .kill (),
143
+ // note that afterSession might not run if the session fails to start, so we also run the cleanup on shutdown
144
+ afterSession : () => {
145
+ closeTauriDriver ();
146
+ },
121
147
};
148
+
149
+ function closeTauriDriver () {
150
+ exit = true ;
151
+ tauriDriver? .kill ();
152
+ }
153
+
154
+ function onShutdown (fn ) {
155
+ const cleanup = () => {
156
+ try {
157
+ fn ();
158
+ } finally {
159
+ process .exit ();
160
+ }
161
+ };
162
+
163
+ process .on (' exit' , cleanup);
164
+ process .on (' SIGINT' , cleanup);
165
+ process .on (' SIGTERM' , cleanup);
166
+ process .on (' SIGHUP' , cleanup);
167
+ process .on (' SIGBREAK' , cleanup);
168
+ }
169
+
170
+ // ensure tauri-driver is closed when our test process exits
171
+ onShutdown (() => {
172
+ closeTauriDriver ();
173
+ });
122
174
` ` `
123
175
124
- If you are interested in the properties on the ` exports. config` object, I [ suggest reading the documentation] [ webdriver documentation ] .
176
+ If you are interested in the properties on the ` config` object, we [suggest reading the documentation][webdriver documentation].
125
177
For non-WDIO specific items, there are comments explaining why we are running commands in ` onPrepare` , ` beforeSession` ,
126
- and ` afterSession ` . We also have our specs set to ` "./develop/tests /specs/**/*.js" ` , so let's create a spec now.
178
+ and ` afterSession` . We also have our specs set to ` " ./test /specs/**/*.js" ` , so let's create a spec now.
127
179
128
180
## Spec
129
181
@@ -217,7 +269,7 @@ of configuration and a single command to run it! Even better, we didn't have to
217
269
218
270
[prerequisites instructions]: /develop/tests/webdriver/
219
271
[webdriverio]: https://webdriver.io/
220
- [ finished example project ] : https://github.com/chippers/hello_tauri
272
+ [finished example project]: https://github.com/tauri-apps/webdriver-example
221
273
[mocha]: https://mochajs.org/
222
274
[webdriver documentation]: https://webdriver.io/docs/configurationfile
223
275
[webdriverio api docs]: https://webdriver.io/docs/api
0 commit comments