Skip to content

Commit 016ddcd

Browse files
committed
fix: fix code for visual
1 parent ef3d6a4 commit 016ddcd

File tree

2 files changed

+96
-82
lines changed

2 files changed

+96
-82
lines changed

chapter_10.md

+76-63
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
Visual Regression Testing
2-
=========================
1+
# Visual Regression Testing
32

43
To walk through the advanced features, we will start with a clean project. This approach ensures that we have a controlled environment for our tests.
54
With this new project, we'll delve into the capabilities of the WebdriverIO testrunner, focusing on its integration with the [`@wdio/visual-service`](https://github.com/webdriverio/visual-testing). This service, designed specifically for WebdriverIO, accommodates a variety of testing environments, including:
@@ -14,11 +13,11 @@ Our objectives for Visual Testing include:
1413

1514
- [Setup a clean project](#setup-a-clean-project)
1615
- Creating Desktop Web Tests:
17-
- [Element/viewport/full-page snapshot tests](#desktop-web-tests)
18-
- [Use Layout testing](#desktop-web-layout-testing)
16+
- [Element/viewport/full-page snapshot tests](#desktop-web-tests)
17+
- [Use Layout testing](#desktop-web-layout-testing)
1918
- Time permitting:
20-
- [Creating storybook snapshots](#extra-1-storybook-testing)
21-
- [Creating native mobile app element/viewport snapshots](#extra-2-mobile-native-app)
19+
- [Creating storybook snapshots](#extra-1-storybook-testing)
20+
- [Creating native mobile app element/viewport snapshots](#extra-2-mobile-native-app)
2221

2322
We will guide you through setting up a clean project in the #setup section.
2423

@@ -52,27 +51,27 @@ We will guide you through setting up a clean project in the #setup section.
5251
5. In the `test/specs/`folder, create a file named `desktop.spec.ts` and insert the following code:
5352

5453
```ts
55-
import {$, browser, expect} from '@wdio/globals'
54+
import { $, browser, expect } from "@wdio/globals";
5655

57-
describe('Guinea Pig Application', () => {
56+
describe("Guinea Pig Application", () => {
5857
beforeEach(async () => {
59-
await browser.setWindowSize(1200, 800)
60-
await browser.url('http://guinea-pig.webdriver.io/image-compare.html')
61-
await $('.hero__title-logo').waitForDisplayed()
58+
await browser.setWindowSize(1200, 800);
59+
await browser.url("http://guinea-pig.webdriver.io/image-compare.html");
60+
await $(".hero__title-logo").waitForDisplayed();
6261
});
6362

64-
it('should be able to create an element snapshot', async () => {
65-
await expect($('.hero__title-logo')).toMatchElementSnapshot('logo')
66-
})
63+
it("should be able to create an element snapshot", async () => {
64+
await expect($(".hero__title-logo")).toMatchElementSnapshot("logo");
65+
});
6766

68-
it('should be able to create a viewport snapshot', async () => {
69-
await expect(browser).toMatchScreenSnapshot('viewport')
70-
})
67+
it("should be able to create a viewport snapshot", async () => {
68+
await expect(browser).toMatchScreenSnapshot("viewport");
69+
});
7170

72-
it('should be able to create full page snapshot', async () => {
73-
await expect(browser).toMatchFullPageSnapshot('full-page')
74-
})
75-
})
71+
it("should be able to create full page snapshot", async () => {
72+
await expect(browser).toMatchFullPageSnapshot("full-page");
73+
});
74+
});
7675
```
7776

7877
## Desktop Web Tests
@@ -89,16 +88,22 @@ Execute `npm run wdio -- --spec="test/specs/desktop.spec.ts"` and review the fol
8988

9089
The Guinea Pig app has a sticky header, which may cause incorrect baseline images for element and full-page snapshots. Modify these by:
9190

92-
1. Adding the following to the element snapshot test:
91+
1. Change the following in the element snapshot test:
9392

9493
```ts
94+
// From
95+
.toMatchElementSnapshot('logo')
96+
// To
9597
.toMatchElementSnapshot('logo', {hideElements: [await $('nav.navbar')]})
9698
```
9799

98100
2. And to the full-page snapshot test:
99101

100102
```ts
101-
.toMatchFullPageSnapshot('full page', {hideAfterFirstScroll: [await $('nav.navbar')]})
103+
// From
104+
.toMatchFullPageSnapshot('full-page')
105+
// To
106+
.toMatchFullPageSnapshot('full-page', {hideAfterFirstScroll: [await $('nav.navbar')]})
102107
```
103108

104109
3. Rerun the test: `npm run wdio -- --spec="test/specs/desktop.spec.ts"`.
@@ -111,9 +116,10 @@ The Visual Testing module uses pixel-by-pixel comparison, which can lead to fals
111116
- [`enableLayoutTesting`-service](https://webdriver.io/docs/visual-testing/service-options#enablelayouttesting) option, which makes all text on a page transparent, including font-based icons.
112117
- [`enableLayoutTesting`-method](https://webdriver.io/docs/visual-testing/method-options#enablelayouttesting) option, applying transparency only to text in specific methods.
113118

114-
Experiment with these options and rerun your tests: `npm run wdio -- --spec="test/specs/desktop.spec.ts"`.
119+
Experiment with these options and rerun your tests: `npm run wdio -- --spec="test/specs/desktop.spec.ts"`.
115120

116121
## Extra 1: Storybook Testing
122+
117123
Now, we will combine WebdriverIO and the Visual Testing Module with both locally and externally hosted Storybook instances.
118124

119125
> [!TIP]
@@ -187,14 +193,14 @@ info Using tsconfig paths for react-docgen
187193

188194
</details>
189195

190-
191196
3. After successful installation, Storybook will automatically open in your browser. You will see several components (Button/Header/Page).
192197
4. Start Storybook Visual Testing with `npx wdio ./wdio.conf.ts --storybook`. This creates `.tmp` and `__snapshots__` folders, both at the root of the project, with relevant images.
193198
5. The images have automatically been clipped to the "estimated" size of the component. If you want you can also:
194-
- select the [browsers](https://github.com/webdriverio/visual-testing/tree/main?tab=readme-ov-file#--browsers) by adding `--browsers=chrome,firefox`
195-
- select an [emulated device mode](https://github.com/webdriverio/visual-testing/tree/main?tab=readme-ov-file#--devices) by for example providing for example `--devices="iPhone 14 Pro Max","Pixel 3 XL"` to the command.
199+
- select the [browsers](https://github.com/webdriverio/visual-testing/tree/main?tab=readme-ov-file#--browsers) by adding `--browsers=chrome,firefox`
200+
- select an [emulated device mode](https://github.com/webdriverio/visual-testing/tree/main?tab=readme-ov-file#--devices) by for example providing for example `--devices="iPhone 14 Pro Max","Pixel 3 XL"` to the command.
196201

197202
> [!TIP]
203+
>
198204
> - If you want to see what is happening then provide `--headless=false` to the command line so the test will run in "normal" mode
199205
> - By default snapshots will be clipped. By providing `--clip=false` as an extra argument the "full"-screen snapshot will be taken. This can come in handy when using the `--devices` argument
200206
@@ -221,34 +227,37 @@ Finally, let's apply the Visual Testing service to Mobile Native apps.
221227
1. Follow the instructions to install [`appium-installer`](https://github.com/AppiumTestDistribution/appium-installer)
222228
2. Open a new terminal and execute `appium-installer`, following the necessary steps.
223229

224-
- *Need help setting up Android Environment to run your Appium test?*
225-
- *Install Appium Server*
226-
- *Install Appium Drivers*
227-
- *Run Appium Doctor*
230+
- _Need help setting up Android Environment to run your Appium test?_
231+
- _Install Appium Server_
232+
- _Install Appium Drivers_
233+
- _Run Appium Doctor_
228234

229235
3. Start Appium with `appium server --log-timestamp --relaxed-security`. Keep this terminal open.
230236
4. Create an `apps` folder at your project's root and download the required Android or iOS app from [here](https://github.com/webdriverio/native-demo-app/releases).
231237
5. Create a file named `wdio.android.emulator.conf.ts` in the root of your project with the following contents:
232238

233239
```ts
234-
import type { Options } from '@wdio/types'
235-
import {join} from 'node:path';
236-
import {config as sharedConfig} from './wdio.conf.js';
240+
import type { Options } from "@wdio/types";
241+
import { join } from "node:path";
242+
import { config as sharedConfig } from "./wdio.conf.js";
237243

238244
export const config: Options.Testrunner = {
239245
...sharedConfig,
240246
capabilities: [
241247
{
242-
platformName: 'Android',
243-
'appium:automationName': 'UIAutomator2',
248+
platformName: "Android",
249+
"appium:automationName": "UIAutomator2",
244250
// Change this to the name of your emulator
245-
'appium:deviceName': 'Pixel_7_Pro_Android_14_API_34',
251+
"appium:deviceName": "Pixel_7_Pro_Android_14_API_34",
246252
// Change this to the version of your emulator
247-
'appium:platformVersion': '14.0',
253+
"appium:platformVersion": "14.0",
248254
// Change this to the path/name of your app
249-
'appium:app': join(process.cwd(), './apps/android.wdio.native.app.v1.0.8.apk'),
250-
'appium:newCommandTimeout': 240,
251-
}
255+
"appium:app": join(
256+
process.cwd(),
257+
"./apps/android.wdio.native.app.v1.0.8.apk"
258+
),
259+
"appium:newCommandTimeout": 240,
260+
},
252261
],
253262
port: 4723,
254263
};
@@ -257,37 +266,41 @@ export const config: Options.Testrunner = {
257266
6. Create a test script `mobile.app.spec.ts` in the `test/specs` folder with the following content:
258267

259268
```ts
260-
import {$, browser, expect} from '@wdio/globals'
269+
import { $, browser, expect } from "@wdio/globals";
261270

262-
describe('Mobile Application', () => {
271+
describe("Mobile Application", () => {
263272
beforeEach(async () => {
264-
await relaunchApp()
265-
await $('~Home-screen').waitForDisplayed()
266-
await $('~Login').click()
267-
await $('~button-LOGIN').waitForDisplayed()
268-
})
273+
await relaunchApp();
274+
await $("~Home-screen").waitForDisplayed();
275+
await $("~Login").click();
276+
await $("~button-LOGIN").waitForDisplayed();
277+
});
269278

270-
it('should be able to create an element snapshot', async () => {
271-
await expect($('~button-LOGIN')).toMatchElementSnapshot('login-button')
272-
})
279+
it("should be able to create an element snapshot", async () => {
280+
await expect($("~button-LOGIN")).toMatchElementSnapshot("login-button");
281+
});
273282

274-
it('should be able to create a device snapshot', async () => {
275-
await expect(browser).toMatchScreenSnapshot('app-forms')
276-
})
277-
})
283+
it("should be able to create a device snapshot", async () => {
284+
await expect(browser).toMatchScreenSnapshot("app-forms");
285+
});
286+
});
278287

279288
/**
280289
* Simple function to relaunch the app
281290
*/
282291
async function relaunchApp() {
283-
const PACKAGE_NAME = 'com.wdiodemoapp'
284-
const BUNDLE_ID = 'org.reactjs.native.example.wdiodemoapp'
285-
const appIdentifier = browser.isAndroid ? { 'appId': PACKAGE_NAME } : { 'bundleId': BUNDLE_ID }
286-
const terminateCommand = 'mobile: terminateApp'
287-
const launchCommand = `mobile: ${driver.isAndroid ? 'activateApp' : 'launchApp'}`
288-
289-
await browser.execute(terminateCommand, appIdentifier)
290-
await browser.execute(launchCommand, appIdentifier)
292+
const PACKAGE_NAME = "com.wdiodemoapp";
293+
const BUNDLE_ID = "org.reactjs.native.example.wdiodemoapp";
294+
const appIdentifier = browser.isAndroid
295+
? { appId: PACKAGE_NAME }
296+
: { bundleId: BUNDLE_ID };
297+
const terminateCommand = "mobile: terminateApp";
298+
const launchCommand = `mobile: ${
299+
driver.isAndroid ? "activateApp" : "launchApp"
300+
}`;
301+
302+
await browser.execute(terminateCommand, appIdentifier);
303+
await browser.execute(launchCommand, appIdentifier);
291304
}
292305
```
293306

+20-19
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,31 @@
1-
import { browser, $, $$, expect } from '@wdio/globals'
1+
import { browser, $, $$, expect } from "@wdio/globals";
22

3-
describe('My Vue.js Example Application', () => {
4-
it('should be able to complete ToDos', async () => {
5-
await browser.url('http://todomvc.com/examples/vue/dist/')
3+
describe("My Vue.js Example Application", () => {
4+
it("should be able to complete ToDos", async () => {
5+
await browser.url("http://todomvc.com/examples/vue/dist/");
66

7-
const newTodoInput = await $('.new-todo')
7+
const newTodoInput = await $(".new-todo");
88

9-
await newTodoInput.setValue('ToDo #1')
10-
await browser.keys('Enter')
9+
await newTodoInput.setValue("ToDo #1");
10+
await browser.keys("Enter");
1111

12-
await newTodoInput.setValue('ToDo #2')
13-
await browser.keys('Enter')
12+
await newTodoInput.setValue("ToDo #2");
13+
await browser.keys("Enter");
1414

15-
await newTodoInput.setValue('ToDo #3')
16-
await browser.keys('Enter')
15+
await newTodoInput.setValue("ToDo #3");
16+
await browser.keys("Enter");
1717

1818
// to see that all ToDos were entered
19-
await browser.pause(2000)
19+
await browser.pause(2000);
2020

21-
const allTodos = await $$('.todo-list li')
22-
await allTodos[1].$('.toggle').click()
21+
const allTodos = await $$(".todo-list li");
22+
await expect(allTodos).toBeElementsArrayOfSize(3);
23+
await allTodos[1].$(".toggle").click();
2324

2425
// to see that ToDo was completed
25-
await browser.pause(2000)
26+
await browser.pause(2000);
2627

27-
const todoCount = await $('.todo-count')
28-
await expect(todoCount).toHaveText('2 items left')
29-
})
30-
})
28+
const todoCount = await $(".todo-count");
29+
await expect(todoCount).toHaveText("2 items left");
30+
});
31+
});

0 commit comments

Comments
 (0)