Skip to content
This repository has been archived by the owner on Dec 16, 2022. It is now read-only.

Commit

Permalink
fix: simplify wait navigation handling
Browse files Browse the repository at this point in the history
closes #30
  • Loading branch information
tnolet committed Oct 14, 2018
1 parent bfde603 commit 565d443
Showing 1 changed file with 10 additions and 26 deletions.
36 changes: 10 additions & 26 deletions src/code-generator/CodeGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ export default class CodeGenerator {
this._frame = 'page'
this._frameId = 0
this._allFrames = {}
this._navigationPromiseSet = false

this._hasNavigation = false
}

generate (events) {
Expand Down Expand Up @@ -66,14 +67,6 @@ export default class CodeGenerator {
}
break
case 'click':
const next = i + 1
if (events[next] && events[next].action === 'navigation*' && this._options.waitForNavigation && !this._navigationPromiseSet) {
const block = new Block(this._frameId)
block.addLine({type: pptrActions.NAVIGATION_PROMISE, value: `const navigationPromise = page.waitForNavigation()`})
this._blocks.push(block)
this._navigationPromiseSet = true
}

this._blocks.push(this._handleClick(selector, events))
break
case 'change':
Expand All @@ -89,10 +82,18 @@ export default class CodeGenerator {
break
case 'navigation*':
this._blocks.push(this._handleWaitForNavigation())
this._hasNavigation = true
break
}
}

if (this._hasNavigation && this._options.waitForNavigation) {
console.debug('Adding navigationPromise declaration')
const block = new Block(this._frameId, { type: pptrActions.NAVIGATION_PROMISE, value: 'const navigationPromise = page.waitForNavigation()' })
this._blocks.unshift(block)
}

console.debug('post processing blocks:', this._blocks)
this._postProcess()

const indent = this._options.wrapAsync ? ' ' : ''
Expand Down Expand Up @@ -120,11 +121,6 @@ export default class CodeGenerator {
}

_postProcess () {
// we want to create only one navigationPromise
if (this._options.waitForNavigation && !this._navigationPromiseSet) {
this._postProcessWaitForNavigation()
}

// when events are recorded from different frames, we want to add a frame setter near the code that uses that frame
if (Object.keys(this._allFrames).length > 0) {
this._postProcessSetFrames()
Expand Down Expand Up @@ -168,18 +164,6 @@ export default class CodeGenerator {
return block
}

_postProcessWaitForNavigation () {
for (let [i, block] of this._blocks.entries()) {
const lines = block.getLines()
for (let line of lines) {
if (line.type === pptrActions.NAVIGATION) {
this._blocks[i].addLineToTop({type: pptrActions.NAVIGATION_PROMISE, value: `const navigationPromise = page.waitForNavigation()`})
return
}
}
}
}

_postProcessSetFrames () {
for (let [i, block] of this._blocks.entries()) {
const lines = block.getLines()
Expand Down

0 comments on commit 565d443

Please sign in to comment.