Skip to content

Commit

Permalink
refresh -> restart
Browse files Browse the repository at this point in the history
  • Loading branch information
starpit committed May 28, 2021
1 parent 220af2e commit ef64b20
Showing 1 changed file with 42 additions and 35 deletions.
77 changes: 42 additions & 35 deletions packages/test/src/api/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,46 @@ const prepareElectron = (popup: string[]) => {
return new Application(opts)
}

/** Add app.client commands */
function addCommands(ctx: ISuite) {
// add an isActive command; isFocused is not what we want
if (ctx.app && ctx.app.client) {
// ref: https://github.com/webdriverio/webdriverio/issues/1362#issuecomment-224042781
ctx.app.client.addCommand('isActive', selector => {
return ctx.app.client.execute(selector => {
const focused = document.activeElement

if (!focused || focused === document.body) {
return false
} else if (document.querySelector) {
return document.querySelector(selector) === focused
}

return false
}, selector)
})
}
}

/** restart the app */
export const restart = async (ctx: ISuite) => {
try {
await ctx.app.restart()
addCommands(ctx)
} catch (err) {
const errorIsNavigatedError: boolean =
err.message.includes('Inspected target navigated or closed') ||
err.message.includes('cannot determine loading status') ||
err.message.includes('Inspected target navigated or closed')

if (!errorIsNavigatedError) {
throw err
}
}

return CLI.waitForSession(ctx)
}

/** reload the app */
export const refresh = async (ctx: ISuite, wait = true, clean = false) => {
try {
Expand Down Expand Up @@ -319,23 +359,8 @@ export const before = (ctx: ISuite, options?: BeforeOptions): HookFunction => {
ctx.timeout(process.env.TIMEOUT || 60000)
await start()

// add an isActive command; isFocused is not what we want
if (ctx.app && ctx.app.client) {
// ref: https://github.com/webdriverio/webdriverio/issues/1362#issuecomment-224042781
ctx.app.client.addCommand('isActive', selector => {
return ctx.app.client.execute(selector => {
const focused = document.activeElement

if (!focused || focused === document.body) {
return false
} else if (document.querySelector) {
return document.querySelector(selector) === focused
}

return false
}, selector)
})
}
// add app.client commands
addCommands(ctx)

// see https://github.com/electron-userland/spectron/issues/763
// and https://github.com/webdriverio/webdriverio/issues/6092
Expand Down Expand Up @@ -505,24 +530,6 @@ export const oops = (ctx: ISuite, wait = false) => async (err: Error) => {
// throw err
}

/** restart the app */
export const restart = async (ctx: ISuite) => {
try {
await ctx.app.restart()
} catch (err) {
const errorIsNavigatedError: boolean =
err.message.includes('Inspected target navigated or closed') ||
err.message.includes('cannot determine loading status') ||
err.message.includes('Inspected target navigated or closed')

if (!errorIsNavigatedError) {
throw err
}
}

return CLI.waitForSession(ctx)
}

/** only execute the test in local */
export const localIt = (msg: string, func: Func) => {
if (process.env.MOCHA_RUN_TARGET !== 'webpack') return it(msg, func)
Expand Down

0 comments on commit ef64b20

Please sign in to comment.