55i18nReady : true
66---
77
8- { /* TODO: REVISE COPY TO V2 */ }
9-
108import CommandTabs from ' @components/CommandTabs.astro' ;
119
1210:::note
@@ -16,14 +14,14 @@ Make sure to go through the [prerequisites instructions] to be able to follow th
1614:::
1715
1816This 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 ` .
2018
2119## Create a Directory for the Tests
2220
2321Let's create a space to write these tests in our project. We will be using a nested directory for
2422this 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.
2725
2826## Initializing a WebdriverIO Project
2927
@@ -38,16 +36,17 @@ guide on setting it up from scratch.
3836 "name" : " webdriverio" ,
3937 "version" : " 1.0.0" ,
4038 "private" : true ,
39+ "type" : " module" ,
4140 "scripts" : {
4241 "test" : " wdio run wdio.conf.js"
4342 },
4443 "dependencies" : {
45- "@wdio/cli" : " ^7.9.1 "
44+ "@wdio/cli" : " ^9.19.0 "
4645 },
4746 "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"
5150 }
5251}
5352```
@@ -80,21 +79,27 @@ config file which controls most aspects of our testing suite.
8079` wdio.conf.js ` :
8180
8281``` 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));
8688
8789// keep track of the `tauri-driver` child process
8890let tauriDriver;
91+ let exit = false ;
8992
90- exports .config = {
93+ export const config = {
94+ host: ' 127.0.0.1' ,
95+ port: 4444 ,
9196 specs: [' ./develop/tests/specs/**/*.js' ],
9297 maxInstances: 1 ,
9398 capabilities: [
9499 {
95100 maxInstances: 1 ,
96101 ' tauri:options' : {
97- application: ' ../.. /target/release/hello- tauri-webdriver ' ,
102+ application: ' ../src-tauri /target/debug/ tauri-app ' ,
98103 },
99104 },
100105 ],
@@ -106,24 +111,71 @@ exports.config = {
106111 },
107112
108113 // 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+ },
110121
111122 // 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 (
114125 path .resolve (os .homedir (), ' .cargo' , ' bin' , ' tauri-driver' ),
115126 [],
116127 { 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+ },
118141
119142 // 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+ },
121147};
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+ });
122174` ` `
123175
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].
125177For 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.
127179
128180## Spec
129181
@@ -217,7 +269,7 @@ of configuration and a single command to run it! Even better, we didn't have to
217269
218270[prerequisites instructions]: /develop/tests/webdriver/
219271[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
221273[mocha]: https://mochajs.org/
222274[webdriver documentation]: https://webdriver.io/docs/configurationfile
223275[webdriverio api docs]: https://webdriver.io/docs/api
0 commit comments