Skip to content

Commit

Permalink
allow chain custom commands
Browse files Browse the repository at this point in the history
  • Loading branch information
jayrepo committed Mar 21, 2022
1 parent 8ee4326 commit 4eb1377
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/wdio-utils/src/shim.ts
Expand Up @@ -251,7 +251,7 @@ let wrapCommand = function wrapCommand<T>(commandName: string, fn: Function): (.
* await $('foo').$('bar')
* ```
*/
if (ELEMENT_QUERY_COMMANDS.includes(prop)) {
if (ELEMENT_QUERY_COMMANDS.includes(prop) || prop.endsWith('$')) {
// this: WebdriverIO.Element
return wrapCommand(prop, function (this: any, ...args: any) {
return this[prop].apply(this, args)
Expand Down Expand Up @@ -336,7 +336,7 @@ let wrapCommand = function wrapCommand<T>(commandName: string, fn: Function): (.
*/
const command = hasWdioSyncSupport && wdioSync && Boolean(global.browser) && !runAsync && !asyncSpec
? wdioSync!.wrapCommand(commandName, fn)
: ELEMENT_QUERY_COMMANDS.includes(commandName)
: ELEMENT_QUERY_COMMANDS.includes(commandName) || commandName.endsWith('$')
? chainElementQuery
: wrapCommandFn

Expand Down
19 changes: 19 additions & 0 deletions packages/wdio-utils/tests/shim-async.test.ts
Expand Up @@ -246,6 +246,25 @@ describe('wrapCommand', () => {
expect(scope.getTagName).toBeCalledTimes(1)
})

it('allows to chain element promises for custom command', async () => {
const rawCommand = jest.fn()
const scope: Partial<BrowserObject> = {
options: {
beforeCommand: jest.fn(),
afterCommand: jest.fn()
},
getTagName: jest.fn().mockResolvedValue('Yayy'),
user$: rawCommand
}
rawCommand.mockReturnValue(Promise.resolve(scope))
const commandB = wrapCommand('user$', rawCommand)
expect(await commandB.call(scope, 'bar').user$('foo').getTagName()).toBe('Yayy')
expect(scope.user$).toBeCalledTimes(2)
expect(scope.user$).toBeCalledWith('bar')
expect(scope.user$).toBeCalledWith('foo')
expect(scope.getTagName).toBeCalledTimes(1)
})

it('allows to access indexed element', async () => {
const rawCommand$ = jest.fn()
const rawCommand$$ = jest.fn()
Expand Down

0 comments on commit 4eb1377

Please sign in to comment.