Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export default class AccessibilityModule extends BaseModule {
accessibilityMap: Map<number, boolean>
LOG_DISABLED_SHOWN: Map<number, boolean>
testMetadata: Record<string, { [key: string]: unknown; }> = {}
currentTestName: string | null = null

constructor(accessibilityConfig: Accessibility, isNonBstackA11y: boolean) {
super()
Expand Down Expand Up @@ -89,15 +90,15 @@ export default class AccessibilityModule extends BaseModule {
//patching getA11yResultsSummary
(browser as WebdriverIO.Browser).getAccessibilityResultsSummary = async () => {
if (this.isAppAccessibility) {
return await getAppA11yResultsSummary(true, browser, isBrowserstackSession, this.accessibility, sessionId)
return await getAppA11yResultsSummary(true, browser, this.currentTestName, isBrowserstackSession, this.accessibility, sessionId)
}
return await this.getA11yResultsSummary(browser)
}

//patching getA11yResults
(browser as WebdriverIO.Browser).getAccessibilityResults = async () => {
if (this.isAppAccessibility) {
return await getAppA11yResults(true, browser, isBrowserstackSession, this.accessibility, sessionId)
return await getAppA11yResults(true, browser, this.currentTestName, isBrowserstackSession, this.accessibility, sessionId)
}
return await this.getA11yResults(browser)
}
Expand Down Expand Up @@ -192,6 +193,7 @@ export default class AccessibilityModule extends BaseModule {
const suiteTitle = (typeof args.suiteTitle === 'string' ? args.suiteTitle : '') || ''
const test = (args.test && typeof args.test === 'object' ? args.test as { title?: string } : {}) || {}

this.currentTestName = test.title || null
const autoInstance: AutomationFrameworkInstance = AutomationFramework.getTrackedInstance()
const testInstance: TestFrameworkInstance = TestFramework.getTrackedInstance()

Expand Down Expand Up @@ -276,6 +278,7 @@ export default class AccessibilityModule extends BaseModule {

if (!autoInstance || !testInstance) {
this.logger.error('No tracked instances found for accessibility after test')
this.currentTestName = null
return
}

Expand Down Expand Up @@ -323,6 +326,9 @@ export default class AccessibilityModule extends BaseModule {

} catch (error) {
this.logger.error(`Accessibility results could not be processed for the test case. Error: ${error}`)
} finally {
this.currentTestName = null
this.logger.debug('[AccessibilityModule] Current test name cleared after test completion')
}
}

Expand Down Expand Up @@ -373,8 +379,9 @@ export default class AccessibilityModule extends BaseModule {
return
}
if (this.isAppAccessibility) {
const testName=this.currentTestName || undefined
const results: unknown = await (browser as WebdriverIO.Browser).execute(
formatString(this.scriptInstance.performScan, JSON.stringify(_getParamsForAppAccessibility(commandName))) as string,
formatString(this.scriptInstance.performScan, JSON.stringify(_getParamsForAppAccessibility(commandName, testName))) as string,
{}
)
BStackLogger.debug(util.format(results as string))
Expand Down
21 changes: 11 additions & 10 deletions packages/wdio-browserstack-service/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -566,19 +566,20 @@ export const formatString = (template: (string | null), ...values: (string | nul
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const _getParamsForAppAccessibility = ( commandName?: string ): { thTestRunUuid: any, thBuildUuid: any, thJwtToken: any, authHeader: any, scanTimestamp: number, method: string | undefined } => {
export const _getParamsForAppAccessibility = ( commandName?: string, testName?: string ): { thTestRunUuid: any, thBuildUuid: any, thJwtToken: any, authHeader: any, scanTimestamp: number, method: string | undefined, testName: string | undefined } => {
return {
'thTestRunUuid': process.env.TEST_ANALYTICS_ID,
'thBuildUuid': process.env.BROWSERSTACK_TESTHUB_UUID,
'thJwtToken': process.env.BROWSERSTACK_TESTHUB_JWT,
'authHeader': process.env.BSTACK_A11Y_JWT,
'scanTimestamp': Date.now(),
'method': commandName
'method': commandName,
'testName': testName
}
}

/* eslint-disable @typescript-eslint/no-explicit-any */
export const performA11yScan = async (isAppAutomate: boolean, browser: WebdriverIO.Browser | WebdriverIO.MultiRemoteBrowser, isBrowserStackSession?: boolean, isAccessibility?: boolean | string, commandName?: string) : Promise<{ [key: string]: any; } | undefined> => {
export const performA11yScan = async (isAppAutomate: boolean, browser: WebdriverIO.Browser | WebdriverIO.MultiRemoteBrowser, isBrowserStackSession?: boolean, isAccessibility?: boolean | string, commandName?: string, testName?: string,) : Promise<{ [key: string]: any; } | undefined> => {

if (!isAccessibilityAutomationSession(isAccessibility)) {
BStackLogger.warn('Not an Accessibility Automation session, cannot perform Accessibility scan.')
Expand All @@ -587,7 +588,7 @@ export const performA11yScan = async (isAppAutomate: boolean, browser: Webdriver

try {
if (isAppAccessibilityAutomationSession(isAccessibility, isAppAutomate)) {
const results: unknown = await (browser as WebdriverIO.Browser).execute(formatString(AccessibilityScripts.performScan, JSON.stringify(_getParamsForAppAccessibility(commandName))) as string, {})
const results: unknown = await (browser as WebdriverIO.Browser).execute(formatString(AccessibilityScripts.performScan, JSON.stringify(_getParamsForAppAccessibility(commandName, testName))) as string, {})
BStackLogger.debug(util.format(results as string))
return ( results as { [key: string]: any; } | undefined )
}
Expand Down Expand Up @@ -626,7 +627,7 @@ export const getA11yResults = PerformanceTester.measureWrapper(PERFORMANCE_SDK_E
}
})

export const getAppA11yResults = PerformanceTester.measureWrapper(PERFORMANCE_SDK_EVENTS.A11Y_EVENTS.GET_RESULTS, async (isAppAutomate: boolean, browser: WebdriverIO.Browser, isBrowserStackSession?: boolean, isAccessibility?: boolean | string, sessionId?: string | null) : Promise<Array<{ [key: string]: any; }>> => {
export const getAppA11yResults = PerformanceTester.measureWrapper(PERFORMANCE_SDK_EVENTS.A11Y_EVENTS.GET_RESULTS, async (isAppAutomate: boolean, browser: WebdriverIO.Browser, testName: string, isBrowserStackSession?: boolean, isAccessibility?: boolean | string, sessionId?: string | null) : Promise<Array<{ [key: string]: any; }>> => {
if (!isBrowserStackSession) {
return [] // since we are running only on Automate as of now
}
Expand All @@ -638,7 +639,7 @@ export const getAppA11yResults = PerformanceTester.measureWrapper(PERFORMANCE_SD

try {
const apiUrl = `${APIUtils.APP_ALLY_ENDPOINT}/${APP_ALLY_ISSUES_ENDPOINT}`
const apiRespone = await getAppA11yResultResponse(apiUrl, isAppAutomate, browser, isBrowserStackSession, isAccessibility, sessionId)
const apiRespone = await getAppA11yResultResponse(apiUrl, isAppAutomate, browser, testName, isBrowserStackSession, isAccessibility, sessionId)
const result = apiRespone?.data?.data?.issues
BStackLogger.debug(`Polling Result: ${JSON.stringify(result)}`)
return result
Expand All @@ -649,7 +650,7 @@ export const getAppA11yResults = PerformanceTester.measureWrapper(PERFORMANCE_SD
}
})

export const getAppA11yResultsSummary = PerformanceTester.measureWrapper(PERFORMANCE_SDK_EVENTS.A11Y_EVENTS.GET_RESULTS_SUMMARY, async (isAppAutomate: boolean, browser: WebdriverIO.Browser, isBrowserStackSession?: boolean, isAccessibility?: boolean | string, sessionId?: string | null) : Promise<{ [key: string]: any; }> => {
export const getAppA11yResultsSummary = PerformanceTester.measureWrapper(PERFORMANCE_SDK_EVENTS.A11Y_EVENTS.GET_RESULTS_SUMMARY, async (isAppAutomate: boolean, browser: WebdriverIO.Browser, testName: string, isBrowserStackSession?: boolean, isAccessibility?: boolean | string, sessionId?: string | null) : Promise<{ [key: string]: any; }> => {
if (!isBrowserStackSession) {
return {} // since we are running only on Automate as of now
}
Expand All @@ -661,7 +662,7 @@ export const getAppA11yResultsSummary = PerformanceTester.measureWrapper(PERFORM

try {
const apiUrl = `${APIUtils.APP_ALLY_ENDPOINT}/${APP_ALLY_ISSUES_SUMMARY_ENDPOINT}`
const apiRespone = await getAppA11yResultResponse(apiUrl, isAppAutomate, browser, isBrowserStackSession, isAccessibility, sessionId)
const apiRespone = await getAppA11yResultResponse(apiUrl, isAppAutomate, browser, testName, isBrowserStackSession, isAccessibility, sessionId)
const result = apiRespone?.data?.data?.summary
BStackLogger.debug(`Polling Result: ${JSON.stringify(result)}`)
return result
Expand All @@ -671,9 +672,9 @@ export const getAppA11yResultsSummary = PerformanceTester.measureWrapper(PERFORM
}
})

const getAppA11yResultResponse = async (apiUrl: string, isAppAutomate: boolean, browser: WebdriverIO.Browser, isBrowserStackSession?: boolean, isAccessibility?: boolean | string, sessionId?: string | null) : Promise<PollingResult> => {
const getAppA11yResultResponse = async (apiUrl: string, isAppAutomate: boolean, browser: WebdriverIO.Browser, testName: string, isBrowserStackSession?: boolean, isAccessibility?: boolean | string, sessionId?: string | null) : Promise<PollingResult> => {
BStackLogger.debug('Performing scan before getting results summary')
await performA11yScan(isAppAutomate, browser, isBrowserStackSession, isAccessibility)
await performA11yScan(isAppAutomate, browser, isBrowserStackSession, isAccessibility, undefined, testName)
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passing undefined as a placeholder for commandName since other callers use commandName but not testName, while this call needs testName but not commandName.

const upperTimeLimit = process.env.BSTACK_A11Y_POLLING_TIMEOUT ? Date.now() + parseInt(process.env.BSTACK_A11Y_POLLING_TIMEOUT) * 1000 : Date.now() + 30000
const params = { test_run_uuid: process.env.TEST_ANALYTICS_ID, session_id: sessionId, timestamp: Date.now() } // Query params to pass
const header = { Authorization: `Bearer ${process.env.BSTACK_A11Y_JWT}` }
Expand Down
8 changes: 4 additions & 4 deletions packages/wdio-browserstack-service/tests/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1987,7 +1987,7 @@ describe('getAppA11yResults', () => {
capabilities: {}
} as unknown as WebdriverIO.Browser

const result = await getAppA11yResults(true, browser, false, true)
const result = await getAppA11yResults(true, browser, 'testName', false, true)
expect(result).toEqual([])
})

Expand All @@ -1998,7 +1998,7 @@ describe('getAppA11yResults', () => {
capabilities: {}
} as unknown as WebdriverIO.Browser

const result = await getAppA11yResults(true, browser, true, false)
const result = await getAppA11yResults(true, browser, 'testName', true, false)
expect(result).toEqual([])
expect(logInfoMock.mock.calls[0][0])
.toContain('Not an Accessibility Automation session, cannot retrieve Accessibility results summary.')
Expand All @@ -2020,7 +2020,7 @@ describe('getAppA11yResults', () => {
vi.spyOn(utils, 'isAppAccessibilityAutomationSession').mockReturnValue(true)
vi.spyOn(utils, 'performA11yScan').mockResolvedValue(undefined)

const result = await getAppA11yResults(true, browser, true, true, 'session123')
const result = await getAppA11yResults(true, browser, 'testName', true, true, 'session123')

expect(result).toEqual(mockResults)

Expand All @@ -2036,7 +2036,7 @@ describe('getAppA11yResults', () => {
capabilities: {}
} as unknown as WebdriverIO.Browser

const result = await getAppA11yResults(true, browser, true, true, 'session123')
const result = await getAppA11yResults(true, browser, 'testName', true, true, 'session123')
expect(result).toEqual([])
})
})
Expand Down