From f4b2c92abfad4f0a813f5a761d5b7a6e0439856f Mon Sep 17 00:00:00 2001 From: rundmd Date: Wed, 26 Jun 2019 09:59:25 -0500 Subject: [PATCH] Update base.ts to accept functions for folder name - allow baselineFolder to be a string or function that is passed an options object - allow screenshotPath to be a string or function that is passed an options object --- docs/OPTIONS.md | 29 +++++++++++++++++++++++++---- lib/base.interface.ts | 2 +- lib/base.spec.ts | 17 +++++++++++++++++ lib/base.ts | 16 ++++++++++++++-- lib/helpers/options.interface.ts | 4 ++-- lib/methods/images.interfaces.ts | 2 +- 6 files changed, 60 insertions(+), 10 deletions(-) diff --git a/docs/OPTIONS.md b/docs/OPTIONS.md index 80bbc1f..4254e90 100644 --- a/docs/OPTIONS.md +++ b/docs/OPTIONS.md @@ -18,11 +18,21 @@ The padding that needs to be added to the address bar on iOS and Android to do a If no baseline image is found during the comparison the image is automatically copied to the baseline folder when this is set to `true` ### `baselineFolder` -- **Type:** `string` +- **Type:** `any` - **Mandatory:** No - **Default:** `./wic/baseline/` -The directory that will hold all the baseline images that are used to during the comparison. +The directory that will hold all the baseline images that are used to during the comparison. If not set, the default value will be used. A function that accepts an option object can also be used to set the baselineFolder value: +``` + getFolder = type = options => { + const testFolder = path.dirname(options.specs[0]); + return path.join(testFolder, 'snapshots', type); + }; + + { + baselineFolder: getFolder(options) + } +``` ### `clearRuntimeFolder` - **Type:** `boolean` @@ -90,11 +100,22 @@ Hide scrollbars in the application. If set to true all scrollbars will be disabl Save the images per instance in a separate folder so for example all Chrome screenshots will be saved in a chrome folder like `desktop_chrome`. ### `screenshotPath` -- **Type:** `string` +- **Type:** `any` - **Default:** `.tmp/` - **Mandatory:** no -The directory that will hold all the actual / difference screenshots +The directory that will hold all the actual / difference screenshots. If not set, the default value will be used. A function that ac +cepts an option object can also be used to set the screenshotPath value: +``` + getFolder = type = options => { + const testFolder = path.dirname(options.specs[0]); + return path.join(testFolder, 'snapshots', type); + }; + + { + screenshotPath: getFolder(options) + } +``` ### `toolBarShadowPadding` - **Type:** `number` diff --git a/lib/base.interface.ts b/lib/base.interface.ts index d6776a5..713b055 100644 --- a/lib/base.interface.ts +++ b/lib/base.interface.ts @@ -1,5 +1,5 @@ export interface Folders { actualFolder: string; // The actual folder where the current screenshots need to be saved - baselineFolder: string; // The baseline folder where the baseline screenshots can be found + baselineFolder: any; // The baseline folder where the baseline screenshots can be found diffFolder: string; // The diff folder where the differences are saved } diff --git a/lib/base.spec.ts b/lib/base.spec.ts index 78da901..eb761d3 100644 --- a/lib/base.spec.ts +++ b/lib/base.spec.ts @@ -13,6 +13,23 @@ describe('BaseClass', () => { expect(instance.folders.diffFolder).toBe('../my_folder/screenshots/diff'); }); + it('should be able to create baselineFolder with a function', () => { + const options = { + baseline: './subfolder//../baseline', + screenshot: './../my_folder//screenshots' + }; + const setPath = (folderPath: any) => { + return folderPath; + }; + const instance = new BaseClass({ + baselineFolder: setPath(options.baseline), + screenshotPath: setPath(options.screenshot) + }); + expect(instance.folders.actualFolder).toBe('../my_folder/screenshots/actual'); + expect(instance.folders.baselineFolder).toBe('baseline'); + expect(instance.folders.diffFolder).toBe('../my_folder/screenshots/diff'); + }); + it('should be able to create BaseClass with default options', () => { const instance = new BaseClass({}); expect(instance.folders.actualFolder).toBe('.tmp/actual'); diff --git a/lib/base.ts b/lib/base.ts index f399c50..161bc53 100644 --- a/lib/base.ts +++ b/lib/base.ts @@ -13,8 +13,20 @@ export default class BaseClass { // determine default options this.defaultOptions = defaultOptions(options); - const baselineFolder = normalize(options.baselineFolder || FOLDERS.DEFAULT.BASE); - const baseFolder = normalize(options.screenshotPath || FOLDERS.DEFAULT.SCREENSHOTS); + let baselineFolder; + let baseFolder; + + if (typeof options.baselineFolder === 'function') { + baselineFolder = options.baselineFolder(options); + } else { + baselineFolder = normalize(options.baselineFolder || FOLDERS.DEFAULT.BASE); + } + + if (typeof options.screenshotPath === 'function') { + baseFolder = options.screenshotPath(options); + } else { + baseFolder = normalize(options.screenshotPath || FOLDERS.DEFAULT.SCREENSHOTS); + } this.folders = { actualFolder: join(baseFolder, FOLDERS.ACTUAL), diff --git a/lib/helpers/options.interface.ts b/lib/helpers/options.interface.ts index 18dfcc6..f71a819 100644 --- a/lib/helpers/options.interface.ts +++ b/lib/helpers/options.interface.ts @@ -7,7 +7,7 @@ export interface ClassOptions{ // If no baseline image is found during the comparison the image is automatically copied to the baseline folder when this is set to `true` autoSaveBaseline?: boolean; // The directory that will hold all the baseline images that are used to during the comparison - baselineFolder?: string; + baselineFolder?: any; // Delete runtime folder (actual & diff) on initialisation clearRuntimeFolder?: boolean; // Enable extra console logging or always saving the diff images during comparison @@ -17,7 +17,7 @@ export interface ClassOptions{ // Save the images per instance in a separate folder. savePerInstance?: boolean; // The directory that will hold all the actual / difference screenshots - screenshotPath?: string; + screenshotPath?: any; // The padding that needs to be added to the toolbar bar on iOS and Android to do a proper cutout of the the viewport. toolBarShadowPadding?: number; diff --git a/lib/methods/images.interfaces.ts b/lib/methods/images.interfaces.ts index c535963..a17101c 100644 --- a/lib/methods/images.interfaces.ts +++ b/lib/methods/images.interfaces.ts @@ -93,7 +93,7 @@ export interface ImageCompareFolderOptions { // The actual folder actualFolder: string; // The baseline folder - baselineFolder: string; + baselineFolder: any; // The name of the browser browserName: string; // The name of the device