Skip to content

Commit

Permalink
fix pptr 20+
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickhulce committed Mar 7, 2024
1 parent ddcc56f commit db4c6b1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
20 changes: 12 additions & 8 deletions lib/extend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,30 @@ function requireOrUndefined(path: string): any {
}

try {
const libPrefix = requireOrUndefined(`puppeteer/lib/cjs/puppeteer/common/Page.js`)
? 'puppeteer/lib/cjs/puppeteer/common'
: 'puppeteer/lib'
let apiPrefix = 'puppeteer-core/lib/cjs/puppeteer/api' // Puppeteer v18+

Page = requireOrUndefined(`${libPrefix}/Page.js`) // tslint:disable-line
if (!requireOrUndefined(`${apiPrefix}/Page.js`)) {
apiPrefix = 'puppeteer/lib/cjs/common' // Puppeteer v5-v18
} else if (!requireOrUndefined(`${apiPrefix}/Page.js`)) {
apiPrefix = 'puppeteer/lib' // Puppeteer <v5
}

Page = requireOrUndefined(`${apiPrefix}/Page.js`) // tslint:disable-line
if (Page.Page) Page = Page.Page

ElementHandle = requireOrUndefined(`${libPrefix}/ElementHandle.js`) // tslint:disable-line variable-name
ElementHandle = requireOrUndefined(`${apiPrefix}/ElementHandle.js`) // tslint:disable-line variable-name
if (ElementHandle && ElementHandle.ElementHandle) ElementHandle = ElementHandle.ElementHandle

if (!ElementHandle) {
const ExecutionContext = requireOrUndefined(`${libPrefix}/ExecutionContext.js`) // tslint:disable-line variable-name
const ExecutionContext = requireOrUndefined(`${apiPrefix}/ExecutionContext.js`) // tslint:disable-line variable-name
if (ExecutionContext && ExecutionContext.ElementHandle) {
ElementHandle = ExecutionContext.ElementHandle
}
}
if (ElementHandle && ElementHandle.ElementHandle) ElementHandle = ElementHandle.ElementHandle

if (!ElementHandle) {
const JSHandle = require(`${libPrefix}/JSHandle.js`) // tslint:disable-line
const JSHandle = require(`${apiPrefix}/JSHandle.js`) // tslint:disable-line
if (JSHandle && JSHandle.ElementHandle) {
ElementHandle = JSHandle.ElementHandle
}
Expand All @@ -52,8 +56,8 @@ try {
return getQueriesForElement(this)
}
} catch (err) {
// tslint:disable-next-line
console.error('Could not augment puppeteer functions, do you have a conflicting version?')
console.error((err as any).stack)
throw err
}

Expand Down
7 changes: 2 additions & 5 deletions test/__snapshots__/extend.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
exports[`lib/extend.ts should handle the LabelText methods 1`] = `"<input id="label-text-input" type="text">"`;
exports[`lib/extend.ts should handle the get* method failures 1`] = `
"Evaluation failed: Error: Unable to find an element with the title: missing.
<div
"<div
id="scoped"
>
Expand All @@ -15,8 +13,7 @@ exports[`lib/extend.ts should handle the get* method failures 1`] = `
</h3>
</div>
<stack>:X:X"
</div>"
`;
exports[`lib/extend.ts should handle the get* methods 1`] = `"<input type="text" data-testid="testid-text-input">"`;
6 changes: 3 additions & 3 deletions test/extend.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ describe('lib/extend.ts', () => {
fail()
} catch (err) {
const message = (err as any).message
.replace(/(\s*at .*(\n|$))+/gm, '\n <stack>:X:X')
.replace('TestingLibraryElementError', 'Error')
expect(message).toMatchSnapshot()
expect(message).toContain('Unable to find an element with the title: missing')
const html = message.match(/<div([\s\S]+)div>/)
expect(html[0]).toMatchSnapshot()
}
})

Expand Down

0 comments on commit db4c6b1

Please sign in to comment.