Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refactor cy funcs #19080

Merged
merged 24 commits into from
Dec 8, 2021
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
isStopped, contentWindowListeners.
sainthkh committed Nov 29, 2021
commit ad14e42099de37bef358c22d1e6010e92ceaf93a
135 changes: 69 additions & 66 deletions packages/driver/src/cypress/cy.ts
Original file line number Diff line number Diff line change
@@ -126,6 +126,7 @@ class $Cy implements ITimeouts, IStability, IAssertions, IRetries, IJQuery, ILoc
state: any
config: any
Cypress: any
Cookies: any
devices: {
keyboard: Keyboard
mouse: Mouse
@@ -207,13 +208,15 @@ class $Cy implements ITimeouts, IStability, IAssertions, IRetries, IJQuery, ILoc
this.state = state
this.config = config
this.Cypress = Cypress
this.Cookies = Cookies
initVideoRecorder(Cypress)

// bind methods
this.$$ = this.$$.bind(this)
this.isCy = this.isCy.bind(this)
this.cleanup = this.cleanup.bind(this)
this.fail = this.fail.bind(this)
this.isStopped = this.isStopped.bind(this)

// init traits

@@ -335,6 +338,10 @@ class $Cy implements ITimeouts, IStability, IAssertions, IRetries, IJQuery, ILoc
return (val === this) || $utils.isInstanceOf(val, $Chainer)
}

isStopped () {
return this.queue.stopped
}

fail (err, options = {}) {
// this means the error has already been through this handler and caught
// again. but we don't need to run it through again, so we can re-throw
@@ -535,77 +542,75 @@ class $Cy implements ITimeouts, IStability, IAssertions, IRetries, IJQuery, ILoc
// which need to run
return this.state('index', this.queue.length)
}
}

export default {
create (specWindow, Cypress, Cookies, state, config, log) {
let cy = new $Cy(specWindow, Cypress, Cookies, state, config)
const commandFns = {}
contentWindowListeners (contentWindow) {
const cy = this

const testConfigOverride = new TestConfigOverride()
$Listeners.bindTo(contentWindow, {
// eslint-disable-next-line @cypress/dev/arrow-body-multiline-braces
onError: (handlerType) => (event) => {
const { originalErr, err, promise } = $errUtils.errorFromUncaughtEvent(handlerType, event)
const handled = cy.onUncaughtException({
err,
promise,
handlerType,
frameType: 'app',
})

const isStopped = () => {
return cy.queue.stopped
}
debugErrors('uncaught AUT error: %o', originalErr)

const contentWindowListeners = function (contentWindow) {
$Listeners.bindTo(contentWindow, {
// eslint-disable-next-line @cypress/dev/arrow-body-multiline-braces
onError: (handlerType) => (event) => {
const { originalErr, err, promise } = $errUtils.errorFromUncaughtEvent(handlerType, event)
const handled = cy.onUncaughtException({
err,
promise,
handlerType,
frameType: 'app',
})
$errUtils.logError(cy.Cypress, handlerType, originalErr, handled)

debugErrors('uncaught AUT error: %o', originalErr)
// return undefined so the browser does its default
// uncaught exception behavior (logging to console)
return undefined
},
onSubmit (e) {
return cy.Cypress.action('app:form:submitted', e)
},
onBeforeUnload (e) {
cy.isStable(false, 'beforeunload')

$errUtils.logError(Cypress, handlerType, originalErr, handled)
cy.Cookies.setInitial()

// return undefined so the browser does its default
// uncaught exception behavior (logging to console)
return undefined
},
onSubmit (e) {
return Cypress.action('app:form:submitted', e)
},
onBeforeUnload (e) {
cy.isStable(false, 'beforeunload')
cy.resetTimer()

Cookies.setInitial()
cy.Cypress.action('app:window:before:unload', e)

cy.resetTimer()
// return undefined so our beforeunload handler
// doesn't trigger a confirmation dialog
return undefined
},
onUnload (e) {
return cy.Cypress.action('app:window:unload', e)
},
onNavigation (...args) {
return cy.Cypress.action('app:navigation:changed', ...args)
},
onAlert (str) {
return cy.Cypress.action('app:window:alert', str)
},
onConfirm (str) {
const results = cy.Cypress.action('app:window:confirm', str)

Cypress.action('app:window:before:unload', e)

// return undefined so our beforeunload handler
// doesn't trigger a confirmation dialog
return undefined
},
onUnload (e) {
return Cypress.action('app:window:unload', e)
},
onNavigation (...args) {
return Cypress.action('app:navigation:changed', ...args)
},
onAlert (str) {
return Cypress.action('app:window:alert', str)
},
onConfirm (str) {
const results = Cypress.action('app:window:confirm', str)

// return false if ANY results are false
// else true
const ret = !_.some(results, returnedFalse)

Cypress.action('app:window:confirmed', str, ret)

return ret
},
})
}
// return false if ANY results are false
// else true
const ret = !_.some(results, returnedFalse)

cy.Cypress.action('app:window:confirmed', str, ret)

return ret
},
})
}
}

export default {
create (specWindow, Cypress, Cookies, state, config, log) {
let cy = new $Cy(specWindow, Cypress, Cookies, state, config)
const commandFns = {}

const testConfigOverride = new TestConfigOverride()

const enqueue = function (obj) {
// if we have a nestedIndex it means we're processing
@@ -718,8 +723,6 @@ export default {
}

_.extend(cy, {
isStopped,

initialize ($autIframe) {
setRemoteIframeProps($autIframe, state)

@@ -731,7 +734,7 @@ export default {
// initially set the content window listeners too
// so we can tap into all the normal flow of events
// like before:unload, navigation events, etc
contentWindowListeners(getContentWindow($autIframe))
cy.contentWindowListeners(getContentWindow($autIframe))

// the load event comes from the autIframe anytime any window
// inside of it loads.
@@ -756,7 +759,7 @@ export default {
// because they would have been automatically applied during
// onBeforeAppWindowLoad, but in the case where we visited
// about:blank in a visit, we do need these
contentWindowListeners(getContentWindow($autIframe))
cy.contentWindowListeners(getContentWindow($autIframe))

Cypress.action('app:window:load', state('window'))

@@ -1022,7 +1025,7 @@ export default {

cy.urlNavigationEvent('before:load')

contentWindowListeners(contentWindow)
cy.contentWindowListeners(contentWindow)

cy.wrapNativeMethods(contentWindow)