@wdio/visual-service 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,16 @@
1
+ # @wdio/visual-service
2
+
3
+ ## 1.0.0
4
+
5
+ ### Major Changes
6
+
7
+ - 36d3868: Support for WebdriverIO v8
8
+
9
+ ### Minor Changes
10
+
11
+ - 36d3868: (feat): add visual matcher
12
+
13
+ ### Patch Changes
14
+
15
+ - Updated dependencies [36d3868]
16
+ - webdriver-image-comparison@3.0.0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 OpenJS Foundation
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,14 @@
1
+ WebdriverIO Visual Service
2
+ ==========================
3
+
4
+ > A WebdriverIO service for image comparison / visual regression testing.
5
+
6
+ ## Installation
7
+
8
+ The easiest way is to keep `@wdio/visual-service` as a dev-dependency in your `package.json`, via:
9
+
10
+ ```sh
11
+ npm install @wdio/visual-service --save-dev
12
+ ```
13
+
14
+ Instructions on how to get started can be found in the [visual testing](https://webdriver.io/docs/visual-testing) docs on the WebdriverIO project page.
@@ -0,0 +1,86 @@
1
+ /// <reference types="./expect-webdriverio.js" />
2
+ import WdioImageComparisonService from './service.js';
3
+ import type { Output, Result, WdioCheckFullPageMethodOptions, WdioSaveFullPageMethodOptions, WdioSaveElementMethodOptions, WdioSaveScreenMethodOptions, WdioCheckElementMethodOptions, WdioCheckScreenMethodOptions } from './types.js';
4
+ declare global {
5
+ namespace WebdriverIO {
6
+ interface Browser {
7
+ /**
8
+ * Saves an image of an element
9
+ */
10
+ saveElement(element: Element, tag: string, saveElementOptions?: WdioSaveElementMethodOptions): Promise<Output>;
11
+ /**
12
+ * Saves an image of a viewport
13
+ */
14
+ saveScreen(tag: string, saveScreenOptions?: WdioSaveScreenMethodOptions): Promise<Output>;
15
+ /**
16
+ * Saves an image of the complete screen
17
+ */
18
+ saveFullPageScreen(tag: string, saveFullPageScreenOptions?: WdioSaveFullPageMethodOptions): Promise<Output>;
19
+ /**
20
+ * Saves an image of the complete screen with the tabbable lines and dots
21
+ */
22
+ saveTabbablePage(tag: string, saveTabbableOptions?: WdioSaveFullPageMethodOptions): Promise<Output>;
23
+ /**
24
+ * Compare an image of an element
25
+ */
26
+ checkElement(element: Element, tag: string, checkElementOptions?: WdioCheckElementMethodOptions): Promise<Result>;
27
+ /**
28
+ * Compares an image of a viewport
29
+ */
30
+ checkScreen(tag: string, checkScreenOptions?: WdioCheckScreenMethodOptions): Promise<Result>;
31
+ /**
32
+ * Compares an image of the complete screen
33
+ */
34
+ checkFullPageScreen(tag: string, checkFullPageOptions?: WdioCheckFullPageMethodOptions): Promise<Result>;
35
+ /**
36
+ * Compares an image of the complete screen with the tabbable lines and dots
37
+ */
38
+ checkTabbablePage(tag: string, checkTabbableOptions?: WdioCheckFullPageMethodOptions): Promise<Result>;
39
+ }
40
+ interface Element {
41
+ }
42
+ interface Capabilities {
43
+ 'wdio-ics:options'?: {
44
+ logName?: string;
45
+ };
46
+ }
47
+ }
48
+ namespace ExpectWebdriverIO {
49
+ interface Matchers<R, T> {
50
+ /**
51
+ * cks that if current screen matches with snapshot of baseline.
52
+ * @param tag snapshot name
53
+ * @param expectedResult either a number representing a mismatch percentage (defaults to 0) or an asymmetric matcher
54
+ * @param options options to pass into the `checkScreen` method
55
+ */
56
+ toMatchScreenSnapshot(tag: string, expectedResult?: number | ExpectWebdriverIO.PartialMatcher, options?: WdioCheckScreenMethodOptions): R;
57
+ toMatchScreenSnapshot(tag: string, options?: WdioCheckScreenMethodOptions): R;
58
+ /**
59
+ * Checks that if the full page screenshot matches with snapshot of baseline.
60
+ * @param tag snapshot name
61
+ * @param expectedResult either a number representing a mismatch percentage (defaults to 0) or an asymmetric matcher
62
+ * @param options options to pass into the `checkFullPageScreen` method
63
+ */
64
+ toMatchFullPageSnapshot(tag: string, expectedResult?: number | ExpectWebdriverIO.PartialMatcher, options?: WdioCheckFullPageMethodOptions): R;
65
+ toMatchFullPageSnapshot(tag: string, options?: WdioCheckFullPageMethodOptions): R;
66
+ /**
67
+ * Checks that if given element matches with snapshot of baseline.
68
+ * @param tag snapshot name
69
+ * @param expectedResult either a number representing a mismatch percentage (defaults to 0) or an asymmetric matcher
70
+ * @param options options to pass into the `checkElement` method
71
+ */
72
+ toMatchElementSnapshot(tag: string, expectedResult?: number | ExpectWebdriverIO.PartialMatcher, options?: WdioCheckElementMethodOptions): R;
73
+ toMatchElementSnapshot(tag: string, options?: WdioCheckElementMethodOptions): R;
74
+ /**
75
+ * Checks that if the full page screenshot including tab marks matches with snapshot of baseline.
76
+ * @param tag snapshot name
77
+ * @param expectedResult either a number representing a mismatch percentage (defaults to 0) or an asymmetric matcher
78
+ * @param options options to pass into the `checkTabbablePage` method
79
+ */
80
+ toMatchTabbablePageSnapshot(tag: string, expectedResult?: number | ExpectWebdriverIO.PartialMatcher, options?: WdioCheckFullPageMethodOptions): R;
81
+ toMatchTabbablePageSnapshot(tag: string, options?: WdioCheckFullPageMethodOptions): R;
82
+ }
83
+ }
84
+ }
85
+ export default WdioImageComparisonService;
86
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,OAAO,0BAA0B,MAAM,cAAc,CAAA;AACrD,OAAO,KAAK,EACR,MAAM,EACN,MAAM,EACN,8BAA8B,EAC9B,6BAA6B,EAC7B,4BAA4B,EAC5B,2BAA2B,EAC3B,6BAA6B,EAC7B,4BAA4B,EAC/B,MAAM,YAAY,CAAA;AAEnB,OAAO,CAAC,MAAM,CAAC;IACX,UAAU,WAAW,CAAC;QAClB,UAAU,OAAO;YACb;;eAEG;YACH,WAAW,CACP,OAAO,EAAE,OAAO,EAChB,GAAG,EAAE,MAAM,EACX,kBAAkB,CAAC,EAAE,4BAA4B,GAClD,OAAO,CAAC,MAAM,CAAC,CAAC;YAEnB;;eAEG;YACH,UAAU,CACN,GAAG,EAAE,MAAM,EACX,iBAAiB,CAAC,EAAE,2BAA2B,GAChD,OAAO,CAAC,MAAM,CAAC,CAAC;YAEnB;;eAEG;YACH,kBAAkB,CACd,GAAG,EAAE,MAAM,EACX,yBAAyB,CAAC,EAAE,6BAA6B,GAC1D,OAAO,CAAC,MAAM,CAAC,CAAC;YAEnB;;eAEG;YACH,gBAAgB,CACZ,GAAG,EAAE,MAAM,EACX,mBAAmB,CAAC,EAAE,6BAA6B,GACpD,OAAO,CAAC,MAAM,CAAC,CAAC;YAEnB;;eAEG;YACH,YAAY,CACR,OAAO,EAAE,OAAO,EAChB,GAAG,EAAE,MAAM,EACX,mBAAmB,CAAC,EAAE,6BAA6B,GACpD,OAAO,CAAC,MAAM,CAAC,CAAC;YAEnB;;eAEG;YACH,WAAW,CACP,GAAG,EAAE,MAAM,EACX,kBAAk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,GACxC,CAAC,CAAA;YACJ;;;;;eAKG;YACH,2BAA2B,CACvB,GAAG,EAAE,MAAM,EACX,cAAc,CAAC,EAAE,MAAM,GAAG,iBAAiB,CAAC,cAAc,EAC1D,OAAO,CAAC,EAAE,8BAA8B,GACzC,CAAC,CAAA;YACJ,2BAA2B,CACvB,GAAG,EAAE,MAAM,EACX,OAAO,CAAC,EAAE,8BAA8B,GACzC,CAAC,CAAA;SACP;KACJ;CACJ;AAED,eAAe,0BAA0B,CAAA"}
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ import WdioImageComparisonService from './service.js';
2
+ export default WdioImageComparisonService;
@@ -0,0 +1,19 @@
1
+ /// <reference types="./expect-webdriverio.js" />
2
+ import type { WdioCheckFullPageMethodOptions, WdioCheckElementMethodOptions, WdioCheckScreenMethodOptions } from './types.js';
3
+ export declare function toMatchScreenSnapshot(browser: WebdriverIO.Browser, tag: string, expectedResultOrOptions?: number | ExpectWebdriverIO.PartialMatcher, optionsOrUndefined?: WdioCheckScreenMethodOptions): Promise<{
4
+ pass: boolean;
5
+ message: () => string;
6
+ }>;
7
+ export declare function toMatchFullPageSnapshot(browser: WebdriverIO.Browser, tag: string, expectedResultOrOptions?: number | ExpectWebdriverIO.PartialMatcher, optionsOrUndefined?: WdioCheckFullPageMethodOptions): Promise<{
8
+ pass: boolean;
9
+ message: () => string;
10
+ }>;
11
+ export declare function toMatchElementSnapshot(element: WebdriverIO.Element, tag: string, expectedResultOrOptions?: number | ExpectWebdriverIO.PartialMatcher, optionsOrUndefined?: WdioCheckElementMethodOptions): Promise<{
12
+ pass: boolean;
13
+ message: () => string;
14
+ }>;
15
+ export declare function toMatchTabbablePageSnapshot(browser: WebdriverIO.Browser, tag: string, expectedResultOrOptions?: number | ExpectWebdriverIO.PartialMatcher, optionsOrUndefined?: WdioCheckFullPageMethodOptions): Promise<{
16
+ pass: boolean;
17
+ message: () => string;
18
+ }>;
19
+ //# sourceMappingURL=matcher.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"matcher.d.ts","sourceRoot":"","sources":["../src/matcher.ts"],"names":[],"mappings":";AAGA,OAAO,KAAK,EACR,8BAA8B,EAC9B,6BAA6B,EAC7B,4BAA4B,EAC/B,MAAM,YAAY,CAAA;AA2FnB,wBAAsB,qBAAqB,CACvC,OAAO,EAAE,WAAW,CAAC,OAAO,EAC5B,GAAG,EAAE,MAAM,EACX,uBAAuB,CAAC,EAAE,MAAM,GAAG,iBAAiB,CAAC,cAAc,EACnE,kBAAkB,CAAC,EAAE,4BAA4B;;;GAKpD;AAED,wBAAsB,uBAAuB,CACzC,OAAO,EAAE,WAAW,CAAC,OAAO,EAC5B,GAAG,EAAE,MAAM,EACX,uBAAuB,CAAC,EAAE,MAAM,GAAG,iBAAiB,CAAC,cAAc,EACnE,kBAAkB,CAAC,EAAE,8BAA8B;;;GAKtD;AAED,wBAAsB,sBAAsB,CACxC,OAAO,EAAE,WAAW,CAAC,OAAO,EAC5B,GAAG,EAAE,MAAM,EACX,uBAAuB,CAAC,EAAE,MAAM,GAAG,iBAAiB,CAAC,cAAc,EACnE,kBAAkB,CAAC,EAAE,6BAA6B;;;GAMrD;AAED,wBAAsB,2BAA2B,CAC7C,OAAO,EAAE,WAAW,CAAC,OAAO,EAC5B,GAAG,EAAE,MAAM,EACX,uBAAuB,CAAC,EAAE,MAAM,GAAG,iBAAiB,CAAC,cAAc,EACnE,kBAAkB,CAAC,EAAE,8BAA8B;;;GAKtD"}
@@ -0,0 +1,93 @@
1
+ import { getBrowserObject } from './utils.js';
2
+ const DEFAULT_EXPECTED_RESULT = 0;
3
+ const asymmetricMatcher = typeof Symbol === 'function' && Symbol.for
4
+ ? Symbol.for('jest.asymmetricMatcher')
5
+ : 1267621;
6
+ function isAsymmetricMatcher(expected) {
7
+ return Boolean(expected && typeof expected === 'object' && '$$typeof' in expected && expected.$$typeof === asymmetricMatcher && 'asymmetricMatch' in expected);
8
+ }
9
+ function compareResult(result, expected) {
10
+ /**
11
+ * expected value is an asymmetric matcher, e.g.
12
+ *
13
+ * ```ts
14
+ * expect(browser).toMatchScreenSnapshot('foo', expect.closeTo(0, 2))
15
+ * ```
16
+ */
17
+ if (isAsymmetricMatcher(expected)) {
18
+ const pass = expected.asymmetricMatch(result.misMatchPercentage);
19
+ return {
20
+ pass,
21
+ message: () => 'Expected image to match with given asymmetric matcher but did not pass!\n'
22
+ };
23
+ }
24
+ /**
25
+ * expected value is a number
26
+ *
27
+ * ```ts
28
+ * expect(browser).toMatchScreenSnapshot('foo', 0)
29
+ * ```
30
+ */
31
+ if (typeof expected === 'number') {
32
+ return {
33
+ pass: result.misMatchPercentage === expected,
34
+ message: () => (`Expected image to have a mismatch percentage of ${expected}%, but was ${result.misMatchPercentage}%\n` +
35
+ 'Please compare the images manually and update the baseline if the new screenshot is correct.\n' +
36
+ `\nBaseline: ${result.folders.baseline}\n` +
37
+ `Actual Screenshot: ${result.folders.actual}\n` +
38
+ `Difference: ${result.folders.diff}\n` +
39
+ '\nSee https://webdriver.io/docs/api/visual-regression.html for more information.')
40
+ };
41
+ }
42
+ throw new Error(`Invalid matcher, expect either a number or an asymmetric matcher, but found ${expected}`);
43
+ }
44
+ function parseMatcherParams(tag, expectedResult, options) {
45
+ /**
46
+ * throw if `tag` is not a string
47
+ */
48
+ if (typeof tag !== 'string') {
49
+ throw new Error(`Expected a snapshot tag as a string but received "${typeof tag}"`);
50
+ }
51
+ /**
52
+ * if `expectedResult` is an object, it is an options object
53
+ * ```ts
54
+ * expect(browser).toMatchScreenSnapshot('foo', { hideAfterFirstScroll: [element] })
55
+ * ```
56
+ */
57
+ if (typeof expectedResult === 'object' && !isAsymmetricMatcher(expectedResult)) {
58
+ options = expectedResult;
59
+ expectedResult = DEFAULT_EXPECTED_RESULT;
60
+ }
61
+ /**
62
+ * make sure `options` is an object
63
+ */
64
+ if (typeof options !== 'object') {
65
+ options = {};
66
+ }
67
+ /**
68
+ * overwrite `returnAllCompareData` to allow us to provide a better assertion message
69
+ */
70
+ options.returnAllCompareData = true;
71
+ return { expectedResult, options };
72
+ }
73
+ export async function toMatchScreenSnapshot(browser, tag, expectedResultOrOptions, optionsOrUndefined) {
74
+ const { expectedResult, options } = parseMatcherParams(tag, expectedResultOrOptions, optionsOrUndefined);
75
+ const result = await browser.checkScreen(tag, options);
76
+ return compareResult(result, expectedResult || DEFAULT_EXPECTED_RESULT);
77
+ }
78
+ export async function toMatchFullPageSnapshot(browser, tag, expectedResultOrOptions, optionsOrUndefined) {
79
+ const { expectedResult, options } = parseMatcherParams(tag, expectedResultOrOptions, optionsOrUndefined);
80
+ const result = await browser.checkFullPageScreen(tag, options);
81
+ return compareResult(result, expectedResult || DEFAULT_EXPECTED_RESULT);
82
+ }
83
+ export async function toMatchElementSnapshot(element, tag, expectedResultOrOptions, optionsOrUndefined) {
84
+ const { expectedResult, options } = parseMatcherParams(tag, expectedResultOrOptions, optionsOrUndefined);
85
+ const browser = getBrowserObject(await element);
86
+ const result = await browser.checkElement(await element, tag, options);
87
+ return compareResult(result, expectedResult || DEFAULT_EXPECTED_RESULT);
88
+ }
89
+ export async function toMatchTabbablePageSnapshot(browser, tag, expectedResultOrOptions, optionsOrUndefined) {
90
+ const { expectedResult, options } = parseMatcherParams(tag, expectedResultOrOptions, optionsOrUndefined);
91
+ const result = await browser.checkTabbablePage(tag, options);
92
+ return compareResult(result, expectedResult || DEFAULT_EXPECTED_RESULT);
93
+ }
@@ -0,0 +1,9 @@
1
+ import type { ClassOptions } from 'webdriver-image-comparison';
2
+ import { BaseClass } from 'webdriver-image-comparison';
3
+ export default class WdioImageComparisonService extends BaseClass {
4
+ #private;
5
+ private _browser?;
6
+ constructor(options: ClassOptions);
7
+ before(capabilities: WebdriverIO.Capabilities, _specs: string[], browser: WebdriverIO.Browser | WebdriverIO.MultiRemoteBrowser): void;
8
+ }
9
+ //# sourceMappingURL=service.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"service.d.ts","sourceRoot":"","sources":["../src/service.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAA;AAC9D,OAAO,EACH,SAAS,EASZ,MAAM,4BAA4B,CAAA;AAsBnC,MAAM,CAAC,OAAO,OAAO,0BAA2B,SAAQ,SAAS;;IAC7D,OAAO,CAAC,QAAQ,CAAC,CAAsD;gBAE3D,OAAO,EAAE,YAAY;IAGjC,MAAM,CACF,YAAY,EAAE,WAAW,CAAC,YAAY,EACtC,MAAM,EAAE,MAAM,EAAE,EAChB,OAAO,EAAE,WAAW,CAAC,OAAO,GAAG,WAAW,CAAC,kBAAkB;CA2HpE"}
@@ -0,0 +1,100 @@
1
+ import logger from '@wdio/logger';
2
+ import { expect } from '@wdio/globals';
3
+ import { BaseClass, checkElement, checkFullPageScreen, checkScreen, saveElement, saveFullPageScreen, saveScreen, saveTabbablePage, checkTabbablePage, } from 'webdriver-image-comparison';
4
+ import { getFolders, getInstanceData } from './utils.js';
5
+ import { toMatchScreenSnapshot, toMatchFullPageSnapshot, toMatchElementSnapshot, toMatchTabbablePageSnapshot } from './matcher.js';
6
+ const log = logger('@wdio/visual-service');
7
+ const elementCommands = { saveElement, checkElement };
8
+ const pageCommands = {
9
+ saveScreen,
10
+ saveFullPageScreen,
11
+ saveTabbablePage,
12
+ checkScreen,
13
+ checkFullPageScreen,
14
+ checkTabbablePage,
15
+ };
16
+ export default class WdioImageComparisonService extends BaseClass {
17
+ _browser;
18
+ constructor(options) {
19
+ super(options);
20
+ }
21
+ before(capabilities, _specs, browser) {
22
+ this._browser = browser;
23
+ if (!this._browser.isMultiremote) {
24
+ log.info('Adding commands to global browser');
25
+ this.#addCommandsToBrowser(capabilities, this._browser);
26
+ }
27
+ else {
28
+ this.#extendMultiremoteBrowser(capabilities);
29
+ }
30
+ /**
31
+ * add custom matcher for visual comparison
32
+ */
33
+ expect.extend({
34
+ toMatchScreenSnapshot,
35
+ toMatchFullPageSnapshot,
36
+ toMatchElementSnapshot,
37
+ toMatchTabbablePageSnapshot,
38
+ });
39
+ }
40
+ #extendMultiremoteBrowser(capabilities) {
41
+ const browser = this._browser;
42
+ const browserNames = Object.keys(capabilities);
43
+ log.info(`Adding commands to Multi Browser: ${browserNames.join(', ')}`);
44
+ for (const browserName of browserNames) {
45
+ const multiremoteBrowser = browser;
46
+ const browserInstance = multiremoteBrowser.getInstance(browserName);
47
+ this.#addCommandsToBrowser(capabilities[browserName].capabilities, browserInstance);
48
+ }
49
+ /**
50
+ * Add all the commands to the global browser object that will execute
51
+ * on each browser in the Multi Remote
52
+ */
53
+ for (const command of [
54
+ ...Object.keys(elementCommands),
55
+ ...Object.keys(pageCommands),
56
+ ]) {
57
+ browser.addCommand(command, function (...args) {
58
+ const returnData = {};
59
+ for (const browserName of browserNames) {
60
+ const multiremoteBrowser = browser;
61
+ const browserInstance = multiremoteBrowser.getInstance(browserName);
62
+ /**
63
+ * casting command to `checkScreen` to simplify type handling here
64
+ */
65
+ returnData[browserName] = browserInstance[command].call(browserInstance, ...args);
66
+ }
67
+ return returnData;
68
+ });
69
+ }
70
+ }
71
+ #addCommandsToBrowser(capabilities, currentBrowser) {
72
+ const instanceData = getInstanceData(capabilities, currentBrowser);
73
+ const folders = this.folders;
74
+ const defaultOptions = this.defaultOptions;
75
+ for (const [commandName, command] of Object.entries(elementCommands)) {
76
+ log.info(`Adding element command "${commandName}" to browser object`);
77
+ currentBrowser.addCommand(commandName, function (element, tag, elementOptions = {}) {
78
+ return command({
79
+ executor: this.execute.bind(currentBrowser),
80
+ screenShot: this.takeScreenshot.bind(currentBrowser),
81
+ }, instanceData, getFolders(elementOptions, folders), element, tag, {
82
+ wic: defaultOptions,
83
+ method: elementOptions,
84
+ });
85
+ });
86
+ }
87
+ for (const [commandName, command] of Object.entries(pageCommands)) {
88
+ log.info(`Adding element command "${commandName}" to browser object`);
89
+ currentBrowser.addCommand(commandName, function (tag, pageOptions = {}) {
90
+ return command({
91
+ executor: this.execute.bind(currentBrowser),
92
+ screenShot: this.takeScreenshot.bind(currentBrowser),
93
+ }, instanceData, getFolders(pageOptions, folders), tag, {
94
+ wic: defaultOptions,
95
+ method: pageOptions,
96
+ });
97
+ });
98
+ }
99
+ }
100
+ }
@@ -0,0 +1,30 @@
1
+ import type { ScreenshotOutput, ImageCompareResult, CheckScreenMethodOptions, SaveScreenMethodOptions, CheckElementMethodOptions, SaveElementMethodOptions, CheckFullPageMethodOptions, SaveFullPageMethodOptions } from 'webdriver-image-comparison';
2
+ type MultiOutput = {
3
+ [browserName: string]: ScreenshotOutput;
4
+ };
5
+ export type Output = MultiOutput | ScreenshotOutput;
6
+ type MultiResult = {
7
+ [browserName: string]: ImageCompareResult | number;
8
+ };
9
+ export type Result = MultiResult | (ImageCompareResult | number);
10
+ export interface WdioIcsCommonOptions {
11
+ hideElements?: WebdriverIO.Element[];
12
+ removeElements?: WebdriverIO.Element[];
13
+ }
14
+ export interface WdioIcsScrollOptions extends WdioIcsCommonOptions {
15
+ hideAfterFirstScroll?: WebdriverIO.Element[];
16
+ }
17
+ export interface WdioCheckFullPageMethodOptions extends Omit<CheckFullPageMethodOptions, keyof WdioIcsScrollOptions>, WdioIcsScrollOptions {
18
+ }
19
+ export interface WdioSaveFullPageMethodOptions extends Omit<SaveFullPageMethodOptions, keyof WdioIcsScrollOptions>, WdioIcsScrollOptions {
20
+ }
21
+ export interface WdioSaveElementMethodOptions extends Omit<SaveElementMethodOptions, keyof WdioIcsCommonOptions>, WdioIcsCommonOptions {
22
+ }
23
+ export interface WdioSaveScreenMethodOptions extends Omit<SaveScreenMethodOptions, keyof WdioIcsCommonOptions>, WdioIcsCommonOptions {
24
+ }
25
+ export interface WdioCheckElementMethodOptions extends Omit<CheckElementMethodOptions, keyof WdioIcsCommonOptions>, WdioIcsCommonOptions {
26
+ }
27
+ export interface WdioCheckScreenMethodOptions extends Omit<CheckScreenMethodOptions, keyof WdioIcsCommonOptions>, WdioIcsCommonOptions {
28
+ }
29
+ export {};
30
+ //# sourceMappingURL=types.d.ts.map
@@ -0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AAQ,IAAI,CAAC,yBAAyB,EAAE,MAAM,oBAAoB,CAAC,EAC/D,oBAAoB;CAAG;AAC/B,MAAM,WAAW,4BACb,SAAQ,IAAI,CAAC,wBAAwB,EAAE,MAAM,oBAAoB,CAAC,EAC9D,oBAAoB;CAAG"}
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,19 @@
1
+ import type { Folders, InstanceData, CheckScreenMethodOptions, SaveScreenMethodOptions, CheckFullPageMethodOptions, SaveFullPageMethodOptions, CheckElementMethodOptions, SaveElementMethodOptions } from 'webdriver-image-comparison';
2
+ /**
3
+ * Get the folders data
4
+ *
5
+ * If folder options are passed in use those values
6
+ * Otherwise, use the values set during instantiation
7
+ */
8
+ type getFolderMethodOptions = CheckElementMethodOptions | CheckFullPageMethodOptions | CheckScreenMethodOptions | SaveElementMethodOptions | SaveFullPageMethodOptions | SaveScreenMethodOptions;
9
+ export declare function getFolders(methodOptions: getFolderMethodOptions, folders: Folders): Folders;
10
+ /**
11
+ * Get the instance data
12
+ */
13
+ export declare function getInstanceData(capabilities: WebdriverIO.Capabilities, currentBrowser: WebdriverIO.Browser): InstanceData;
14
+ /**
15
+ * traverse up the scope chain until browser element was reached
16
+ */
17
+ export declare function getBrowserObject(elem: WebdriverIO.Element | WebdriverIO.Browser): WebdriverIO.Browser;
18
+ export {};
19
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACR,OAAO,EACP,YAAY,EACZ,wBAAwB,EACxB,uBAAuB,EACvB,0BAA0B,EAC1B,yBAAyB,EACzB,yBAAyB,EACzB,wBAAwB,EAC3B,MAAM,4BAA4B,CAAA;AAOnC;;;;;GAKG;AACH,KAAK,sBAAsB,GACrB,yBAAyB,GACzB,0BAA0B,GAC1B,wBAAwB,GACxB,wBAAwB,GACxB,yBAAyB,GACzB,uBAAuB,CAAC;AAC9B,wBAAgB,UAAU,CACtB,aAAa,EAAE,sBAAsB,EACrC,OAAO,EAAE,OAAO,GACjB,OAAO,CAMT;AAED;;GAEG;AACH,wBAAgB,eAAe,CAC3B,YAAY,EAAE,WAAW,CAAC,YAAY,EACtC,cAAc,EAAE,WAAW,CAAC,OAAO,GACpC,YAAY,CAyDd;AAED;;GAEG;AACH,wBAAgB,gBAAgB,CAAE,IAAI,EAAE,WAAW,CAAC,OAAO,GAAG,WAAW,CAAC,OAAO,GAAG,WAAW,CAAC,OAAO,CAGtG"}
package/dist/utils.js ADDED
@@ -0,0 +1,56 @@
1
+ /// <reference types="webdriverio" />
2
+ export function getFolders(methodOptions, folders) {
3
+ return {
4
+ actualFolder: methodOptions.actualFolder ?? folders.actualFolder,
5
+ baselineFolder: methodOptions.baselineFolder ?? folders.baselineFolder,
6
+ diffFolder: methodOptions.diffFolder ?? folders.diffFolder,
7
+ };
8
+ }
9
+ /**
10
+ * Get the instance data
11
+ */
12
+ export function getInstanceData(capabilities, currentBrowser) {
13
+ const currentCapabilities = currentBrowser.requestedCapabilities.alwaysMatch
14
+ ? currentBrowser.requestedCapabilities.alwaysMatch
15
+ : currentBrowser.requestedCapabilities;
16
+ const browserName = (capabilities.browserName ||
17
+ currentCapabilities.browserName ||
18
+ 'browserName-not-known').toLowerCase();
19
+ const browserVersion = (capabilities.browserVersion ||
20
+ currentCapabilities.browserVersion ||
21
+ 'not-known').toLowerCase();
22
+ const logName = 'wdio-ics:options' in capabilities
23
+ ? capabilities['wdio-ics:options']?.logName ??
24
+ ''
25
+ : '';
26
+ const name = 'wdio-ics:options' in capabilities
27
+ ? capabilities['wdio-ics:options']?.name ?? ''
28
+ : '';
29
+ // For mobile
30
+ const platformName = (capabilities.platformName ||
31
+ currentCapabilities.platformName ||
32
+ 'not-known').toLowerCase();
33
+ const platformVersion = (capabilities['appium:platformVersion'] ||
34
+ currentCapabilities['appium:platformVersion'] ||
35
+ 'not-known').toLowerCase();
36
+ const deviceName = (capabilities['appium:deviceName'] || '').toLowerCase();
37
+ const nativeWebScreenshot = !!(capabilities['appium:nativeWebScreenshot'] ||
38
+ currentCapabilities['appium:nativeWebScreenshot']);
39
+ return {
40
+ browserName,
41
+ browserVersion,
42
+ deviceName,
43
+ logName,
44
+ name,
45
+ nativeWebScreenshot,
46
+ platformName,
47
+ platformVersion,
48
+ };
49
+ }
50
+ /**
51
+ * traverse up the scope chain until browser element was reached
52
+ */
53
+ export function getBrowserObject(elem) {
54
+ const elemObject = elem;
55
+ return elemObject.parent ? getBrowserObject(elemObject.parent) : elem;
56
+ }
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@wdio/visual-service",
3
+ "author": "Wim Selles - wswebcreation",
4
+ "description": "Image comparison / visual regression testing for WebdriverIO",
5
+ "version": "1.0.0",
6
+ "license": "MIT",
7
+ "homepage": "https://webdriver.io/docs/visual-testing",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "https://github.com/webdriverio/visual-testing.git"
11
+ },
12
+ "keywords": [
13
+ "webdriverio",
14
+ "visual",
15
+ "regression",
16
+ "image",
17
+ "comparison"
18
+ ],
19
+ "exports": "./dist/index.js",
20
+ "type": "module",
21
+ "types": "./dist/index.d.ts",
22
+ "dependencies": {
23
+ "@wdio/logger": "^8.24.12",
24
+ "@wdio/types": "^8.26.2",
25
+ "webdriver-image-comparison": "^3.0.0"
26
+ },
27
+ "scripts": {
28
+ "build": "run-s clean build:*",
29
+ "build:tsc": "tsc --project ./tsconfig.json",
30
+ "clean": "rimraf coverage build .tmp",
31
+ "watch": "pnpm run build:tsc -w"
32
+ }
33
+ }