Skip to content

Commit 9c82ab7

Browse files
committed
spike: attempt to use url() and hash() in the automation client and unbind it from the window
1 parent 0e74d2c commit 9c82ab7

File tree

5 files changed

+55
-19
lines changed

5 files changed

+55
-19
lines changed

packages/driver/src/cy/commands/location.ts

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,16 @@ import _ from 'lodash'
33
import $errUtils from '../../cypress/error_utils'
44

55
export default (Commands, Cypress, cy) => {
6-
Commands.addQuery('url', function url (options: Partial<Cypress.UrlOptions> = {}) {
7-
// Make sure the url command can communicate with the AUT.
8-
// otherwise, it yields an empty string
9-
Cypress.ensure.commandCanCommunicateWithAUT(cy)
10-
this.set('timeout', options.timeout)
11-
6+
Commands.add('url', function url (options: Partial<Cypress.UrlOptions> = {}) {
127
Cypress.log({ message: '', hidden: options.log === false, timeout: options.timeout })
138

14-
return () => {
15-
const href = cy.getRemoteLocation('href')
16-
17-
return options.decode ? decodeURI(href) : href
18-
}
9+
return Cypress.automation('get:aut:url', {})
1910
})
2011

21-
Commands.addQuery('hash', function url (options: Partial<Cypress.Loggable & Cypress.Timeoutable> = {}) {
22-
// Make sure the hash command can communicate with the AUT.
23-
Cypress.ensure.commandCanCommunicateWithAUT(cy)
24-
this.set('timeout', options.timeout)
25-
12+
Commands.add('hash', function url (options: Partial<Cypress.Loggable & Cypress.Timeoutable> = {}) {
2613
Cypress.log({ message: '', hidden: options.log === false, timeout: options.timeout })
2714

28-
return () => cy.getRemoteLocation('hash')
15+
return Cypress.automation('get:aut:url:hash', {})
2916
})
3017

3118
Commands.addQuery('location', function location (key, options: Partial<Cypress.Loggable & Cypress.Timeoutable> = {}) {
@@ -34,7 +21,7 @@ export default (Commands, Cypress, cy) => {
3421

3522
// Make sure the location command can communicate with the AUT.
3623
// otherwise the command just yields 'null' and the reason may be unclear to the user.
37-
Cypress.ensure.commandCanCommunicateWithAUT(cy)
24+
// Cypress.ensure.commandCanCommunicateWithAUT(cy)
3825
if (_.isObject(key)) {
3926
options = key
4027
}

packages/driver/src/cy/commands/window.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export default (Commands, Cypress, cy, state) => {
100100

101101
Commands.addQuery('window', function windowFn (options: Partial<Cypress.Loggable & Cypress.Timeoutable> = {}) {
102102
// Make sure the window command can communicate with the AUT.
103-
Cypress.ensure.commandCanCommunicateWithAUT(cy)
103+
// Cypress.ensure.commandCanCommunicateWithAUT(cy)
104104
this.set('timeout', options.timeout)
105105
Cypress.log({
106106
hidden: options.log === false,

packages/server/lib/browsers/bidi_automation.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,22 @@ export class BidiAutomation {
659659
}
660660

661661
return
662+
case 'get:aut:url':
663+
{
664+
const { contexts: autContext } = await this.webDriverClient.browsingContextGetTree({
665+
root: this.autContextId,
666+
})
667+
668+
return autContext ? autContext[0].url : ''
669+
}
670+
case 'get:aut:url:hash':
671+
{
672+
const { contexts: autContext } = await this.webDriverClient.browsingContextGetTree({
673+
root: this.autContextId,
674+
})
675+
676+
return autContext ? new URL(autContext[0].url).hash : ''
677+
}
662678
default:
663679
debug('BiDi automation not implemented for message: %s', message)
664680
throw new AutomationNotImplemented(message, 'BiDiAutomation')

packages/server/lib/browsers/cdp_automation.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,33 @@ export class CdpAutomation implements CDPClient, AutomationMiddleware {
464464
return false
465465
}
466466

467+
private _getAutFrame = async () => {
468+
try {
469+
const frameTree = (await this.sendDebuggerCommandFn('Page.getFrameTree')).frameTree
470+
const frame = _.find(frameTree?.childFrames || [], ({ frame }) => {
471+
return frame?.name?.startsWith('Your project:')
472+
}) as HasFrame | undefined
473+
474+
return frame?.frame
475+
} catch (err) {
476+
debugVerbose('failed to get aut frame:', err.stack)
477+
478+
return undefined
479+
}
480+
}
481+
482+
private _getAutUrl = async () => {
483+
const frame = await this._getAutFrame()
484+
485+
return frame?.url || ''
486+
}
487+
488+
private _getAutUrlHash = async () => {
489+
const frame = await this._getAutFrame()
490+
491+
return frame?.urlFragment || ''
492+
}
493+
467494
_handlePausedRequests = async (client: CriClient) => {
468495
// NOTE: only supported in chromium based browsers
469496
await client.send('Fetch.enable', {
@@ -611,6 +638,10 @@ export class CdpAutomation implements CDPClient, AutomationMiddleware {
611638
}
612639

613640
return cdpKeyPress(data, this.sendDebuggerCommandFn, this.executionContexts, (await this.send('Page.getFrameTree')).frameTree)
641+
case 'get:aut:url':
642+
return this._getAutUrl()
643+
case 'get:aut:url:hash':
644+
return this._getAutUrlHash()
614645
default:
615646
throw new Error(`No automation handler registered for: '${message}'`)
616647
}

packages/types/src/server.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ export interface AutomationCommands {
9191
'remote:debugger:protocol': CommandSignature
9292
'response:received': CommandSignature
9393
'key:press': CommandSignature<KeyPressParams, void>
94+
'get:aut:url': CommandSignature<{ url: string }, string>
95+
'get:aut:url:hash': CommandSignature<{ hash: string }, string>
9496
}
9597

9698
export type OnRequestEvent = <T extends keyof AutomationCommands>(message: T, data: AutomationCommands[T]['dataType']) => Promise<AutomationCommands[T]['returnType']>

0 commit comments

Comments
 (0)