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

Commit

Permalink
fix: fixes wrong placement of navigation promise
Browse files Browse the repository at this point in the history
closes #26
  • Loading branch information
tnolet committed Sep 25, 2018
1 parent a37acc5 commit f39dbca
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "puppeteer-recorder",
"version": "0.4.0",
"version": "0.4.1",
"description": "A Chrome extension for recording browser interaction and generating Puppeteer scripts",
"main": "index.js",
"scripts": {
Expand Down
17 changes: 13 additions & 4 deletions src/code-generator/CodeGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export default class CodeGenerator {
this._frame = 'page'
this._frameId = 0
this._allFrames = {}
this._navigationPromiseSet = false
}

generate (events) {
Expand All @@ -52,8 +53,8 @@ export default class CodeGenerator {
console.debug(`generating code for ${events ? events.length : 0} events`)
let result = ''

for (let event of events) {
const { action, selector, value, href, keyCode, tagName, frameId, frameUrl } = event
for (let i = 0; i < events.length; i++) {
const { action, selector, value, href, keyCode, tagName, frameId, frameUrl } = events[i]

// we need to keep a handle on what frames events originate from
this._setFrames(frameId, frameUrl)
Expand All @@ -65,7 +66,15 @@ export default class CodeGenerator {
}
break
case 'click':
this._blocks.push(this._handleClick(selector))
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':
if (tagName === 'SELECT') {
Expand Down Expand Up @@ -112,7 +121,7 @@ export default class CodeGenerator {

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

Expand Down
9 changes: 6 additions & 3 deletions src/code-generator/__tests__/code-generator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ describe('code-generator', () => {
})

test('it generates the correct waitForNavigation code', () => {
const events = [{ action: 'navigation*' }, { action: 'click', selector: 'a.link' }]
const events = [{ action: 'click', selector: 'a.link' }, { action: 'navigation*' }]
const codeGenerator = new CodeGenerator()
expect(codeGenerator._parseEvents(events)).toContain('const navigationPromise = page.waitForNavigation()\n')
expect(codeGenerator._parseEvents(events)).toContain('await navigationPromise\n')
const code = codeGenerator._parseEvents(events)
const lines = code.split('\n')
expect(lines[1].trim()).toEqual('const navigationPromise = page.waitForNavigation()')
expect(lines[4].trim()).toEqual("await page.click('a.link')")
expect(lines[6].trim()).toEqual('await navigationPromise')
})

test('it does not generate waitForNavigation code when turned off', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Puppeteer Recorder",
"version": "0.4.0",
"version": "0.4.1",
"manifest_version": 2,
"description": "A Chrome extension for recording browser interaction and generating Puppeteer scripts",
"permissions": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ exports[`App.vue it has the correct pristine / empty state 1`] = `
class="text-muted"
>
<small>
0.4.0
0.4.1
</small>
</span>
</a>
Expand Down

0 comments on commit f39dbca

Please sign in to comment.