Skip to content

Commit

Permalink
Bump expect-webdriverio to improve async jasmine execution (#7458)
Browse files Browse the repository at this point in the history
* Bump expect-webdriverio to improve async jasmine execution

* remove expectAsync

* Revert "Bump expect-webdriverio to improve async jasmine execution"

This reverts commit d4348bc.

* allow to call expect in async context

* improve TS
  • Loading branch information
christian-bromann committed Sep 22, 2021
1 parent 30670d5 commit 21538c3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ describe('My Login application', () => {
await LoginPage.open();
await LoginPage.login('tomsmith', 'SuperSecretPassword!');
await expectAsync(SecurePage.flashAlert).toBeExisting();
await expectAsync(SecurePage.flashAlert).toHaveTextContaining(
await expect(SecurePage.flashAlert).toBeExisting();
await expect(SecurePage.flashAlert).toHaveTextContaining(
'You logged into a secure area!');
});
});
Expand All @@ -34,8 +34,8 @@ describe('My Login application', () => {
await $('#password').setValue('SuperSecretPassword!');
await $('button[type="submit"]').click();
await expectAsync($('#flash')).toBeExisting();
await expectAsync($('#flash')).toHaveTextContaining(
await expect($('#flash')).toBeExisting();
await expect($('#flash')).toHaveTextContaining(
'You logged into a secure area!');
});
});
Expand Down
21 changes: 20 additions & 1 deletion packages/wdio-utils/src/shim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ declare global {
var browser: any
}

declare global {
namespace NodeJS {
interface Global {
expect: any
expectAsync: any
}
}
}

const ELEMENT_QUERY_COMMANDS = ['$', '$$', 'custom$', 'custom$$', 'shadow$', 'shadow$$', 'react$', 'react$$']
const ELEMENT_PROPS = ['elementId', 'error', 'selector', 'parent', 'index', 'isReactElement', 'length']
const PROMISE_METHODS = ['then', 'catch', 'finally']
Expand Down Expand Up @@ -77,7 +86,7 @@ let executeHooksWithArgs = async function executeHooksWithArgsShim<T> (hookName:

try {
result = hook.apply(null, args)
} catch (e) {
} catch (e: any) {
log.error(e.stack)
return resolve(e)
}
Expand Down Expand Up @@ -340,9 +349,15 @@ async function executeSyncFn (this: any, fn: Function, retries: Retries, args: a
* @return {Promise} that gets resolved once test/hook is done or was retried enough
*/
async function executeAsync(this: any, fn: Function, retries: Retries, args: any[] = []): Promise<unknown> {
const isJasmine = global.jasmine && global.expectAsync
const asyncSpecBefore = asyncSpec
this.wdioRetries = retries.attempts

const expectSync = global.expect
if (isJasmine) {
global.expect = global.expectAsync
}

try {
runAsync = true
asyncSpec = true
Expand All @@ -362,6 +377,10 @@ async function executeAsync(this: any, fn: Function, retries: Retries, args: any
}

throw e
} finally {
if (isJasmine) {
global.expect = expectSync
}
}
}

Expand Down
8 changes: 8 additions & 0 deletions tests/jasmine/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@ const assert = require('assert')

describe('Jasmine smoke test', () => {
it('should return sync value', () => {
browser.isEventuallyDisplayedScenario()
expect(browser).toHaveTitle('Mock Page Title')
expect($('foo')).toBeDisplayed()
})

it('should return async value', async () => {
browser.isEventuallyDisplayedScenario()
await expect(browser).toHaveTitle('Mock Page Title')
await expect($('foo')).toBeDisplayed()
})

let hasRun = false
Expand Down

0 comments on commit 21538c3

Please sign in to comment.