Skip to content

Commit 9a50cbc

Browse files
Update setup (#59)
* chore: update wdio deps linting of code * feat: Simplify setup for new examples * fix: fix config * feat: create separate configs - cleanup code a bit * chore: some fixes - remove wdio - fix window size - update readme with EU DC
1 parent ad67820 commit 9a50cbc

File tree

8 files changed

+185
-78
lines changed

8 files changed

+185
-78
lines changed

wdio/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,19 @@ npm run sauce-visual
5757
npm run sauce-visual-check
5858
```
5959

60+
**NOTE:**
61+
By default the tests will be executed on the US DC, if you want to run them on the EU DC then please run the following command
62+
63+
```sh { name=npm-run-eu }
64+
REGION=eu npm run sauce-visual
65+
```
66+
67+
or
68+
69+
```sh { name=npm-run-modified-eu }
70+
REGION=eu npm run sauce-visual-check
71+
```
72+
6073
- Open the test or go to https://app.saucelabs.com/visual/builds to review changes.
6174

6275
## How to add visual testing to your setup

wdio/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
"typescript": "^5.2.2"
1313
},
1414
"scripts": {
15-
"sauce-visual": "wdio run ./wdio.conf.ts --spec src/inventory.spec.ts",
16-
"sauce-visual-check": "VISUAL_CHECK=true wdio run ./wdio.conf.ts --spec src/inventory.spec.ts"
15+
"sauce-visual": "wdio run ./src/configs/wdio.saucelabs.desktop.conf.ts --spec src/inventory.spec.ts",
16+
"sauce-visual-check": "VISUAL_CHECK=true wdio run ./src/configs/wdio.saucelabs.desktop.conf.ts --spec src/inventory.spec.ts"
1717
},
1818
"dependencies": {
1919
"@saucelabs/wdio-sauce-visual-service": "^0.3.84",
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import type { Options } from '@wdio/types';
2+
import { config as sauceSharedConfig } from './wdio.saucelabs.shared.conf.ts';
3+
4+
const buildName = `Sauce Demo Test - ${new Date().getTime()}`;
5+
6+
export const config: Options.Testrunner = {
7+
...sauceSharedConfig,
8+
//
9+
// ============
10+
// Capabilities
11+
// ============
12+
capabilities: [
13+
{
14+
browserName: 'chrome',
15+
browserVersion: 'latest',
16+
platformName: 'Windows 11',
17+
'sauce:options': {
18+
screenResolution: '2560x1600',
19+
build: buildName,
20+
},
21+
},
22+
{
23+
browserName: 'firefox',
24+
browserVersion: 'latest',
25+
platformName: 'Windows 11',
26+
'sauce:options': {
27+
screenResolution: '2560x1600',
28+
build: buildName,
29+
},
30+
},
31+
{
32+
browserName: 'microsoftedge',
33+
browserVersion: 'latest',
34+
platformName: 'Windows 11',
35+
'sauce:options': {
36+
screenResolution: '2560x1600',
37+
build: buildName,
38+
},
39+
},
40+
{
41+
browserName: 'safari',
42+
browserVersion: 'latest',
43+
platformName: 'macOS 13',
44+
'sauce:options': {
45+
screenResolution: '2048x1536',
46+
build: buildName,
47+
},
48+
},
49+
],
50+
};
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import type { Options } from '@wdio/types';
2+
import { browser } from '@wdio/globals';
3+
import { SauceVisualService } from '@saucelabs/wdio-sauce-visual-service';
4+
import { config as sharedConfig } from './wdio.shared.conf.ts';
5+
import { getSauceCredentials } from '../helpers.ts';
6+
7+
//
8+
// Get the Sauce Labs credentials
9+
const { sauceUsername, sauceAccessKey } = await getSauceCredentials();
10+
11+
export const config: Options.Testrunner = {
12+
...sharedConfig,
13+
//
14+
// =================
15+
// Service Providers
16+
// =================
17+
user: sauceUsername,
18+
key: sauceAccessKey,
19+
region: (process.env.REGION || 'us') as Options.SauceRegions,
20+
//
21+
// ============
22+
// Capabilities
23+
// ============
24+
// Are not configured here, they can be found in:
25+
// - wdio.saucelabs.desktop.conf.ts
26+
//
27+
// ========
28+
// Services
29+
// ========
30+
services: (sharedConfig.services || []).concat([
31+
//
32+
// This service is needed for WDIO to make sure it can connect to Sauce Labs to:
33+
// - automatically update the names
34+
// - automatically update the status (passed/failed)
35+
// - automatically send the stacktrace in case of a failure
36+
//
37+
'sauce',
38+
//
39+
// This service is needed for the Sauce Visual service to work
40+
//
41+
[
42+
SauceVisualService,
43+
// The options for the Sauce Visual service
44+
{
45+
buildName: 'Sauce Demo Test',
46+
branch: 'main',
47+
project: 'WDIO examples',
48+
},
49+
],
50+
]),
51+
// =====
52+
// Hooks
53+
// =====
54+
before: async (_capabilities, _specs) => {
55+
// Set all browsers to the "same" viewport
56+
// @ts-ignore
57+
await browser.setWindowSize(1920, 1080);
58+
},
59+
};

wdio/wdio.conf.ts renamed to wdio/src/configs/wdio.shared.conf.ts

Lines changed: 15 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
import type { Options } from '@wdio/types';
2-
import 'dotenv/config';
3-
import updateDotenv from 'update-dotenv';
4-
import readline from 'readline-sync';
5-
import { SauceVisualService } from '@saucelabs/wdio-sauce-visual-service';
6-
7-
const { sauceUsername, sauceAccessKey } = await getSauceCredentials();
82

93
export const config: Options.Testrunner = {
104
//
@@ -28,17 +22,9 @@ export const config: Options.Testrunner = {
2822
// should work too though). These services define specific user and key (or access key)
2923
// values you need to put in here in order to connect to these services.
3024
//
31-
user: sauceUsername,
32-
key: sauceAccessKey,
33-
//
34-
// If you run your tests on Sauce Labs you can specify the region you want to run your tests
35-
// in via the `region` property. Available short handles for regions are `us` (default), `eu` and `apac`.
36-
// These regions are used for the Sauce Labs VM cloud and the Sauce Labs Real Device Cloud.
37-
// If you don't provide the region it will default for the `us`
38-
protocol: 'https',
39-
hostname: 'ondemand.saucelabs.com',
40-
port: 443,
41-
path: '/wd/hub',
25+
// =======================================================
26+
// See wdio.saucelabs.shared.conf.ts for more information.
27+
// =======================================================
4228
//
4329
// ==================
4430
// Specify Test Files
@@ -82,18 +68,12 @@ export const config: Options.Testrunner = {
8268
// Sauce Labs platform configurator - a great tool to configure your capabilities:
8369
// https://saucelabs.com/platform/platform-configurator
8470
//
85-
capabilities: [
86-
{
87-
// capabilities for local browser web tests
88-
browserName: 'chrome', // or 'firefox', 'microsoftedge', 'safari'
89-
browserVersion: 'latest',
90-
platformName: 'Windows 11',
91-
'sauce:options': {
92-
screenResolution: '1920x1080',
93-
build: `Sauce Demo Test - ${new Date().getTime()}`,
94-
},
95-
},
96-
],
71+
// =================================
72+
// For capabilities see:
73+
// - wdio.saucelabs.desktop.conf.ts
74+
// =================================
75+
//
76+
capabilities: [],
9777
//
9878
// ===================
9979
// Test Configurations
@@ -141,17 +121,12 @@ export const config: Options.Testrunner = {
141121
// Services take over a specific job you don't want to take care of. They enhance
142122
// your test setup with almost no effort. Unlike plugins, they don't add new
143123
// commands. Instead, they hook themselves up into the test process.
144-
services: [
145-
'sauce',
146-
[
147-
SauceVisualService,
148-
{
149-
buildName: 'Sauce Demo Test',
150-
branch: 'main',
151-
project: 'WDIO examples',
152-
},
153-
],
154-
],
124+
//
125+
// ==============================================================
126+
// For implementing Sauce Labs, see wdio.saucelabs.shared.conf.ts
127+
// ==============================================================
128+
//
129+
services: [],
155130

156131
// Framework you want to run your specs with.
157132
// The following are supported: Mocha, Jasmine, and Cucumber
@@ -329,36 +304,3 @@ export const config: Options.Testrunner = {
329304
// onReload: function(oldSessionId, newSessionId) {
330305
// }
331306
};
332-
333-
async function getSauceCredentials(): Promise<{
334-
sauceUsername: string;
335-
sauceAccessKey: string;
336-
}> {
337-
var sauceUsername = process.env.SAUCE_USERNAME;
338-
var sauceAccessKey = process.env.SAUCE_ACCESS_KEY;
339-
340-
if (!sauceUsername) {
341-
sauceUsername = await readline.question(
342-
'What is your Sauce Labs username? '
343-
);
344-
await updateDotenv({
345-
SAUCE_USERNAME: sauceUsername,
346-
});
347-
console.log('Sauce Labs username is saved in the .env file.');
348-
}
349-
350-
if (!sauceAccessKey) {
351-
sauceAccessKey = await readline.question(
352-
'What is your Sauce Labs API key?? ',
353-
{
354-
hideEchoBack: true,
355-
}
356-
);
357-
await updateDotenv({
358-
SAUCE_ACCESS_KEY: sauceAccessKey,
359-
});
360-
console.log('Sauce Labs API key is saved in the .env file.');
361-
}
362-
363-
return { sauceUsername, sauceAccessKey };
364-
}

wdio/src/helpers.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import 'dotenv/config';
2+
import updateDotenv from 'update-dotenv';
3+
import readline from 'readline-sync';
4+
5+
/**
6+
* Get the Sauce Labs credentials from the environment variables, if not available,
7+
* ask the user to provide them and store them in a .env file.
8+
*/
9+
export async function getSauceCredentials(): Promise<{
10+
sauceUsername: string;
11+
sauceAccessKey: string;
12+
}> {
13+
let sauceUsername = process.env.SAUCE_USERNAME;
14+
let sauceAccessKey = process.env.SAUCE_ACCESS_KEY;
15+
16+
if (!sauceUsername) {
17+
sauceUsername = await readline.question(
18+
'What is your Sauce Labs username? '
19+
);
20+
await updateDotenv({
21+
SAUCE_USERNAME: sauceUsername,
22+
});
23+
console.log('Sauce Labs username is saved in the .env file.');
24+
}
25+
26+
if (!sauceAccessKey) {
27+
sauceAccessKey = await readline.question(
28+
'What is your Sauce Labs API key?? ',
29+
{
30+
hideEchoBack: true,
31+
}
32+
);
33+
await updateDotenv({
34+
SAUCE_ACCESS_KEY: sauceAccessKey,
35+
});
36+
console.log('Sauce Labs API key is saved in the .env file.');
37+
}
38+
39+
return { sauceUsername, sauceAccessKey };
40+
}

wdio/src/inventory.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { browser } from '@wdio/globals';
12
import InventoryPage from './pages/inventory.page.js';
23
import LoginPage from './pages/login.page.js';
34

wdio/tsconfig.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"compilerOptions": {
3-
"moduleResolution": "node",
4-
"module": "ESNext",
3+
"module": "NodeNext",
4+
"moduleResolution": "NodeNext",
55
"types": [
66
"node",
77
"@wdio/globals/types",
@@ -10,6 +10,8 @@
1010
"@wdio/sauce-service"
1111
],
1212
"allowSyntheticDefaultImports": true,
13-
"target": "es2022"
13+
"target": "es2022",
14+
"noEmit": true,
15+
"allowImportingTsExtensions": true
1416
}
1517
}

0 commit comments

Comments
 (0)