Skip to content

Commit

Permalink
test fixes and using # prefix for private fields
Browse files Browse the repository at this point in the history
  • Loading branch information
rev-doshi committed Jan 8, 2024
1 parent 855dbd3 commit 0ba8af2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 30 deletions.
56 changes: 28 additions & 28 deletions packages/wdio-browserstack-service/src/Percy/Percy-Handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import { PercyLogger } from './PercyLogger.js'
import { PERCY_DOM_CHANGING_COMMANDS_ENDPOINTS, CAPTURE_MODES } from '../constants.js'

class _PercyHandler {
private _testMetadata: { [key: string]: any } = {}
private sessionName?: string
private _isAppAutomate?: boolean
private isPercyCleanupProcessingUnderway?: boolean = false
private _percyScreenshotCounter: any = 0
private percyDeferredScreenshots: any = []
private percyScreenshotInterval: any = null
#_testMetadata: { [key: string]: any } = {}
#sessionName?: string
#_isAppAutomate?: boolean
#isPercyCleanupProcessingUnderway?: boolean = false
#_percyScreenshotCounter: any = 0
#percyDeferredScreenshots: any = []
#percyScreenshotInterval: any = null

constructor (
private _percyAutoCaptureMode: string | undefined,
Expand All @@ -28,20 +28,20 @@ class _PercyHandler {
isAppAutomate?: boolean,
private _framework?: string
) {
this._isAppAutomate = isAppAutomate
this.#_isAppAutomate = isAppAutomate
if (_percyAutoCaptureMode && !_percyAutoCaptureMode || !CAPTURE_MODES.includes(_percyAutoCaptureMode as string)) {
this._percyAutoCaptureMode = 'auto'
}
}

_setSessionName(name: string) {
this.sessionName = name
this.#sessionName = name
}

async teardown () {
await new Promise<void>((resolve) => {
setInterval(() => {
if (this._percyScreenshotCounter === 0) {
if (this.#_percyScreenshotCounter === 0) {
resolve()
}
}, 1000)
Expand All @@ -53,16 +53,16 @@ class _PercyHandler {
if (eventName) {
if (!sessionName) {
/* Service doesn't wait for handling of browser commands so the below counter is used in teardown method to delay service exit */
this._percyScreenshotCounter += 1
this.#_percyScreenshotCounter += 1
}

(this._browser.percyCaptureMap as PercyCaptureMap).increment(sessionName ? sessionName : (this.sessionName as string), eventName)
await (this._isAppAutomate ? PercySDK.screenshotApp((this._browser.percyCaptureMap as PercyCaptureMap).getName( sessionName ? sessionName : (this.sessionName as string), eventName)) : await PercySDK.screenshot(this._browser, (this._browser.percyCaptureMap as PercyCaptureMap).getName( sessionName ? sessionName : (this.sessionName as string), eventName)))
this._percyScreenshotCounter -= 1
(this._browser.percyCaptureMap as PercyCaptureMap).increment(sessionName ? sessionName : (this.#sessionName as string), eventName)
await (this.#_isAppAutomate ? PercySDK.screenshotApp((this._browser.percyCaptureMap as PercyCaptureMap).getName( sessionName ? sessionName : (this.#sessionName as string), eventName)) : await PercySDK.screenshot(this._browser, (this._browser.percyCaptureMap as PercyCaptureMap).getName( sessionName ? sessionName : (this.#sessionName as string), eventName)))
this.#_percyScreenshotCounter -= 1
}
} catch (err: any) {
this._percyScreenshotCounter -= 1;
(this._browser.percyCaptureMap as PercyCaptureMap).decrement(sessionName ? sessionName : (this.sessionName as string), eventName as string)
this.#_percyScreenshotCounter -= 1;
(this._browser.percyCaptureMap as PercyCaptureMap).decrement(sessionName ? sessionName : (this.#sessionName as string), eventName as string)
PercyLogger.error(`Error while trying to auto capture Percy screenshot ${err}`)
}
}
Expand All @@ -73,8 +73,8 @@ class _PercyHandler {

deferCapture(sessionName: string, eventName: string | null) {
/* Service doesn't wait for handling of browser commands so the below counter is used in teardown method to delay service exit */
this._percyScreenshotCounter += 1
this.percyDeferredScreenshots.push({ sessionName, eventName })
this.#_percyScreenshotCounter += 1
this.#percyDeferredScreenshots.push({ sessionName, eventName })
}

isDOMChangingCommand(args: BeforeCommandArgs): boolean {
Expand Down Expand Up @@ -109,12 +109,12 @@ class _PercyHandler {
}

async cleanupDeferredScreenshots() {
this.isPercyCleanupProcessingUnderway = true
for (const entry of this.percyDeferredScreenshots) {
this.#isPercyCleanupProcessingUnderway = true
for (const entry of this.#percyDeferredScreenshots) {
await this.percyAutoCapture(entry.eventName, entry.sessionName)
}
this.percyDeferredScreenshots = []
this.isPercyCleanupProcessingUnderway = false
this.#percyDeferredScreenshots = []
this.#isPercyCleanupProcessingUnderway = false
}

async browserBeforeCommand (args: BeforeCommandArgs) {
Expand All @@ -124,12 +124,12 @@ class _PercyHandler {
}
do {
await sleep(1000)
} while (this.percyScreenshotInterval)
this.percyScreenshotInterval = setInterval(async () => {
if (!this.isPercyCleanupProcessingUnderway) {
clearInterval(this.percyScreenshotInterval)
} while (this.#percyScreenshotInterval)
this.#percyScreenshotInterval = setInterval(async () => {
if (!this.#isPercyCleanupProcessingUnderway) {
clearInterval(this.#percyScreenshotInterval)
await this.cleanupDeferredScreenshots()
this.percyScreenshotInterval = null
this.#percyScreenshotInterval = null
}
}, 1000)
} catch (err: any) {
Expand All @@ -156,7 +156,7 @@ class _PercyHandler {
eventName = 'keys'
}
if (eventName) {
this.deferCapture(this.sessionName as string, eventName)
this.deferCapture(this.#sessionName as string, eventName)
}
} catch (err: any) {
PercyLogger.error(`Error while trying to calculate auto capture parameters ${err}`)
Expand Down
3 changes: 1 addition & 2 deletions packages/wdio-browserstack-service/src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,7 @@ export default class BrowserstackService implements Services.ServiceInstance {
this._browser,
this._caps,
this._isAppAutomate(),
this._config.framework,
this._caps
this._config.framework
)
this._percyHandler.before()
}
Expand Down

0 comments on commit 0ba8af2

Please sign in to comment.