Skip to content

Commit

Permalink
Sanitize script command logs (#8165)
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-bromann committed Mar 29, 2022
1 parent 1897b94 commit 3841213
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/wdio-utils/src/utils.ts
Expand Up @@ -5,6 +5,7 @@ import type { Services, Clients } from '@wdio/types'

const SCREENSHOT_REPLACEMENT = '"<Screenshot[base64]>"'
const SCRIPT_PLACEHOLDER = '"<Script[base64]>"'
const REGEX_SCRIPT_NAME = /return \(function (\w+)/

/**
* overwrite native element commands with user defined
Expand Down Expand Up @@ -97,6 +98,9 @@ export function transformCommandLogResult (result: { file?: string, script?: str
return SCREENSHOT_REPLACEMENT
} else if (typeof result.script === 'string' && isBase64(result.script)) {
return SCRIPT_PLACEHOLDER
} else if (typeof result.script === 'string' && result.script.match(REGEX_SCRIPT_NAME)) {
const newScript = result.script.match(REGEX_SCRIPT_NAME)![1]
return { ...result, script: `${newScript}(...) [${Buffer.byteLength(result.script, 'utf-8')} bytes]` }
}

return result
Expand Down
4 changes: 4 additions & 0 deletions packages/wdio-utils/tests/utils.test.ts
Expand Up @@ -46,6 +46,10 @@ describe('utils', () => {
expect(transformCommandLogResult({ script: 'foo' })).toEqual({ script: 'foo' })
expect(transformCommandLogResult({ script: (Buffer.from('some script payload')).toString('base64') }))
.toBe('"<Script[base64]>"')

expect(transformCommandLogResult({ script: 'return foobar' })).toEqual({ script: 'return foobar' })
expect(transformCommandLogResult({ script: 'return (function isElementDisplayed(element) {\n...' }))
.toEqual({ script: 'isElementDisplayed(...) [50 bytes]' })
})

describe('overwriteElementCommands', () => {
Expand Down

0 comments on commit 3841213

Please sign in to comment.