-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
core(gather-runner): always reset scroll position #9060
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,6 +86,16 @@ class Driver { | |
this._handleReceivedMessageFromTarget(event, []).catch(this._handleEventError); | ||
}); | ||
|
||
// We use isolated execution contexts for `evaluateAsync` that can be destroyed through navigation | ||
// and other page actions. Cleanup our relevant bookkeeping as we see those events. | ||
this.on('Runtime.executionContextDestroyed', event => { | ||
if (event.executionContextId === this._isolatedExecutionContextId) { | ||
this._clearIsolatedContextId(); | ||
} | ||
}); | ||
|
||
this.on('Page.frameNavigated', () => this._clearIsolatedContextId()); | ||
|
||
connection.on('protocolevent', this._handleProtocolEvent.bind(this)); | ||
|
||
/** | ||
|
@@ -464,7 +474,20 @@ class Driver { | |
*/ | ||
async evaluateAsync(expression, options = {}) { | ||
const contextId = options.useIsolation ? await this._getOrCreateIsolatedContextId() : undefined; | ||
return this._evaluateInContext(expression, contextId); | ||
|
||
try { | ||
// `await` is not redunant here because we want to `catch` the async errors | ||
return await this._evaluateInContext(expression, contextId); | ||
} catch (err) { | ||
// If we were using isolation and the context disappeared on us, retry one more time. | ||
if (contextId && err.message.includes('Cannot find context')) { | ||
this._clearIsolatedContextId(); | ||
const freshContextId = await this._getOrCreateIsolatedContextId(); | ||
return this._evaluateInContext(expression, freshContextId); | ||
} | ||
|
||
throw err; | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -1324,6 +1347,22 @@ class Driver { | |
return flattenedDocument.nodes ? flattenedDocument.nodes : []; | ||
} | ||
|
||
/** | ||
* @param {{x: number, y: number}} position | ||
* @return {Promise<void>} | ||
*/ | ||
scrollTo(position) { | ||
const scrollExpression = `window.scrollTo(${position.x}, ${position.y})`; | ||
return this.evaluateAsync(scrollExpression, {useIsolation: true}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. feels like the protocol should have somethign for this too, but I don't see anything There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can synthesize a gesture but that's not what we want to do https://chromedevtools.github.io/devtools-protocol/tot/Input/#method-synthesizeScrollGesture There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
yeah, I meant it feels like there should be a method :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. apparently this is exactly what devtools does (e.g. "Scroll into view" in the devtools panel just remote evals |
||
} | ||
|
||
/** | ||
* @return {Promise<{x: number, y: number}>} | ||
*/ | ||
getScrollPosition() { | ||
return this.evaluateAsync(`({x: window.scrollX, y: window.scrollY})`, {useIsolation: true}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. feels like the protocol should have something for this... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. looks like it's |
||
} | ||
|
||
/** | ||
* @param {{additionalTraceCategories?: string|null}=} settings | ||
* @return {Promise<void>} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this still needed given the new event handling?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure what you mean by new event handling, but it's something we should do regardless. Our temporary context can be destroyed at any time and we used to see high volumes of this in sentry once upon a time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh do you just mean
Runtime.executionContextDestroyed
? It can still race if we're trying to eval this between when we receive the context destroyed event and we eval this, plus the aforementioned destroying of our context regardless of navigation :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, I guess it feels like we should be handling it the "correct way"™ or the "sloppily but effectively prevent race conditions way", but not both, but w/e :)