Skip to content
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

chore: add after:browser:launch node event #28180

Merged
merged 21 commits into from
Nov 6, 2023
Merged
Changes from 3 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
f292279
chore: extract Target.attachedToTarget and Target.targetDestroyed han…
chrisbreiding Oct 30, 2023
277885e
chore: add after:browser:launch node event
chrisbreiding Oct 30, 2023
7be4103
add to firefox and webkit [run ci]
chrisbreiding Oct 30, 2023
dde0d7a
Merge branch 'develop' into crb/cy-puppeteer-refactors
chrisbreiding Oct 30, 2023
054961d
fix electron spec
chrisbreiding Oct 30, 2023
af92198
Merge branch 'crb/cy-puppeteer-refactors' into crb/cy-puppeteer-after…
chrisbreiding Oct 30, 2023
4ba7fb2
un-nest browser-cri tests
chrisbreiding Oct 30, 2023
bbcce26
Merge branch 'crb/cy-puppeteer-refactors' of github.com:cypress-io/cy…
chrisbreiding Oct 30, 2023
671e509
Merge branch 'crb/cy-puppeteer-refactors' into crb/cy-puppeteer-after…
chrisbreiding Oct 30, 2023
96baf2c
Update cli/types/cypress.d.ts
chrisbreiding Oct 31, 2023
3f304c4
Merge branch 'develop' into crb/cy-puppeteer-after-browser-launch
chrisbreiding Oct 31, 2023
2d862af
add comment
chrisbreiding Nov 3, 2023
03ae6a6
add webSocketDebuggerUrl to webkit's after:browser:launch
chrisbreiding Nov 3, 2023
c688aec
Merge branch 'crb/cy-puppeteer-after-browser-launch' of github.com:cy…
chrisbreiding Nov 3, 2023
5c5c4f1
Merge branch 'develop' into crb/cy-puppeteer-after-browser-launch
chrisbreiding Nov 3, 2023
bd7b832
fix tests
chrisbreiding Nov 6, 2023
ac2ab40
Merge remote-tracking branch 'origin/develop' into crb/cy-puppeteer-a…
chrisbreiding Nov 6, 2023
a8d66a4
Merge remote-tracking branch 'origin/develop' into crb/cy-puppeteer-a…
chrisbreiding Nov 6, 2023
5e93741
re-query target id on reconnect
chrisbreiding Nov 6, 2023
3e16190
Revert "re-query target id on reconnect"
chrisbreiding Nov 6, 2023
d58db53
try not reconnecting if child target
chrisbreiding Nov 6, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
266 changes: 133 additions & 133 deletions packages/server/test/unit/browsers/browser-cri-client_spec.ts
Original file line number Diff line number Diff line change
@@ -127,200 +127,200 @@ describe('lib/browsers/cri-client', function () {

expect(criImport.Version).to.be.calledTwice
})
})

context('#ensureMinimumProtocolVersion', function () {
function withProtocolVersion (actual, test) {
return getClient()
.then((client: any) => {
client.versionInfo = { 'Protocol-Version': actual }

return client.ensureMinimumProtocolVersion(test)
})
}
context('#ensureMinimumProtocolVersion', function () {
function withProtocolVersion (actual, test) {
return getClient()
.then((client: any) => {
client.versionInfo = { 'Protocol-Version': actual }

it('resolves if protocolVersion = current', function () {
return expect(withProtocolVersion('1.3', '1.3')).to.be.fulfilled
return client.ensureMinimumProtocolVersion(test)
})
}

it('resolves if protocolVersion > current', function () {
return expect(withProtocolVersion('1.4', '1.3')).to.be.fulfilled
})
it('resolves if protocolVersion = current', function () {
return expect(withProtocolVersion('1.3', '1.3')).to.be.fulfilled
})

it('rejects if protocolVersion < current', function () {
return expect(withProtocolVersion('1.2', '1.3')).to.be
.rejected.then((err) => {
expect(stripAnsi(err.message)).to.eq(`A minimum CDP version of 1.3 is required, but the current browser has 1.2.`)
})
it('resolves if protocolVersion > current', function () {
return expect(withProtocolVersion('1.4', '1.3')).to.be.fulfilled
})

it('rejects if protocolVersion < current', function () {
return expect(withProtocolVersion('1.2', '1.3')).to.be
.rejected.then((err) => {
expect(stripAnsi(err.message)).to.eq(`A minimum CDP version of 1.3 is required, but the current browser has 1.2.`)
})
})
})

context('#attachToTargetUrl', function () {
it('creates a page client when the passed in url is found', async function () {
const mockPageClient = {}
context('#attachToTargetUrl', function () {
it('creates a page client when the passed in url is found', async function () {
const mockPageClient = {}

send.withArgs('Target.getTargets').resolves({ targetInfos: [{ targetId: '1', url: 'http://foo.com' }, { targetId: '2', url: 'http://bar.com' }] })
criClientCreateStub.withArgs({ target: '1', onAsynchronousError: onError, host: HOST, port: PORT, protocolManager: undefined, fullyManageTabs: undefined, browserClient: { on, send, close } }).resolves(mockPageClient)
send.withArgs('Target.getTargets').resolves({ targetInfos: [{ targetId: '1', url: 'http://foo.com' }, { targetId: '2', url: 'http://bar.com' }] })
criClientCreateStub.withArgs({ target: '1', onAsynchronousError: onError, host: HOST, port: PORT, protocolManager: undefined, fullyManageTabs: undefined, browserClient: { on, send, close } }).resolves(mockPageClient)

const browserClient = await getClient()
const browserClient = await getClient()

const client = await browserClient.attachToTargetUrl('http://foo.com')
const client = await browserClient.attachToTargetUrl('http://foo.com')

expect(client).to.be.equal(mockPageClient)
})
expect(client).to.be.equal(mockPageClient)
})

it('creates a page client when the passed in url is found and notifies the protocol manager and fully managed tabs', async function () {
const mockPageClient = {}
const protocolManager: any = {
connectToBrowser: sinon.stub().resolves(),
}
it('creates a page client when the passed in url is found and notifies the protocol manager and fully managed tabs', async function () {
const mockPageClient = {}
const protocolManager: any = {
connectToBrowser: sinon.stub().resolves(),
}

send.withArgs('Target.getTargets').resolves({ targetInfos: [{ targetId: '1', url: 'http://foo.com' }, { targetId: '2', url: 'http://bar.com' }] })
send.withArgs('Target.setDiscoverTargets', { discover: true })
on.withArgs('Target.targetDestroyed', sinon.match.func)
criClientCreateStub.withArgs({ target: '1', onAsynchronousError: onError, host: HOST, port: PORT, protocolManager, fullyManageTabs: true, browserClient: { on, send, close } }).resolves(mockPageClient)
send.withArgs('Target.getTargets').resolves({ targetInfos: [{ targetId: '1', url: 'http://foo.com' }, { targetId: '2', url: 'http://bar.com' }] })
send.withArgs('Target.setDiscoverTargets', { discover: true })
on.withArgs('Target.targetDestroyed', sinon.match.func)
criClientCreateStub.withArgs({ target: '1', onAsynchronousError: onError, host: HOST, port: PORT, protocolManager, fullyManageTabs: true, browserClient: { on, send, close } }).resolves(mockPageClient)

const browserClient = await getClient({ protocolManager, fullyManageTabs: true })
const browserClient = await getClient({ protocolManager, fullyManageTabs: true })

const client = await browserClient.attachToTargetUrl('http://foo.com')
const client = await browserClient.attachToTargetUrl('http://foo.com')

expect(client).to.be.equal(mockPageClient)
expect(protocolManager.connectToBrowser).to.be.calledWith(client)
})
expect(client).to.be.equal(mockPageClient)
expect(protocolManager.connectToBrowser).to.be.calledWith(client)
})

it('creates a page client when the passed in url is found and notifies the protocol manager and fully managed tabs and attaching to target throws', async function () {
const mockPageClient = {}
const protocolManager: any = {
connectToBrowser: sinon.stub().resolves(),
}
it('creates a page client when the passed in url is found and notifies the protocol manager and fully managed tabs and attaching to target throws', async function () {
const mockPageClient = {}
const protocolManager: any = {
connectToBrowser: sinon.stub().resolves(),
}

send.withArgs('Target.getTargets').resolves({ targetInfos: [{ targetId: '1', url: 'http://foo.com' }, { targetId: '2', url: 'http://bar.com' }] })
send.withArgs('Target.setDiscoverTargets', { discover: true })
on.withArgs('Target.targetDestroyed', sinon.match.func)
send.withArgs('Target.getTargets').resolves({ targetInfos: [{ targetId: '1', url: 'http://foo.com' }, { targetId: '2', url: 'http://bar.com' }] })
send.withArgs('Target.setDiscoverTargets', { discover: true })
on.withArgs('Target.targetDestroyed', sinon.match.func)

send.withArgs('Network.enable').throws(new Error('ProtocolError: Inspected target navigated or closed'))
send.withArgs('Network.enable').throws(new Error('ProtocolError: Inspected target navigated or closed'))

criClientCreateStub.withArgs({ target: '1', onAsynchronousError: onError, host: HOST, port: PORT, protocolManager, fullyManageTabs: true, browserClient: { on, send, close } }).resolves(mockPageClient)
criClientCreateStub.withArgs({ target: '1', onAsynchronousError: onError, host: HOST, port: PORT, protocolManager, fullyManageTabs: true, browserClient: { on, send, close } }).resolves(mockPageClient)

const browserClient = await getClient({ protocolManager, fullyManageTabs: true })
const browserClient = await getClient({ protocolManager, fullyManageTabs: true })

const client = await browserClient.attachToTargetUrl('http://foo.com')
const client = await browserClient.attachToTargetUrl('http://foo.com')

expect(client).to.be.equal(mockPageClient)
expect(protocolManager.connectToBrowser).to.be.calledWith(client)
expect(client).to.be.equal(mockPageClient)
expect(protocolManager.connectToBrowser).to.be.calledWith(client)

// This would throw if the error was not caught
await on.withArgs('Target.attachedToTarget').args[0][1]({ targetInfo: { type: 'worker' } })
})
// This would throw if the error was not caught
await on.withArgs('Target.attachedToTarget').args[0][1]({ targetInfo: { type: 'worker' } })
})

it('retries when the passed in url is not found', async function () {
sinon.stub(protocol, '_getDelayMsForRetry')
.onFirstCall().returns(100)
.onSecondCall().returns(100)
.onThirdCall().returns(100)
it('retries when the passed in url is not found', async function () {
sinon.stub(protocol, '_getDelayMsForRetry')
.onFirstCall().returns(100)
.onSecondCall().returns(100)
.onThirdCall().returns(100)

const mockPageClient = {}
const mockPageClient = {}

send.withArgs('Target.getTargets').resolves({ targetInfos: [{ targetId: '1', url: 'http://foo.com' }, { targetId: '2', url: 'http://bar.com' }] })
send.withArgs('Target.getTargets').resolves({ targetInfos: [{ targetId: '1', url: 'http://foo.com' }, { targetId: '2', url: 'http://bar.com' }] })
send.withArgs('Target.getTargets').resolves({ targetInfos: [{ targetId: '1', url: 'http://foo.com' }, { targetId: '2', url: 'http://bar.com' }, { targetId: '3', url: 'http://baz.com' }] })
criClientCreateStub.withArgs({ target: '1', onAsynchronousError: onError, host: HOST, port: PORT, protocolManager: undefined, fullyManageTabs: undefined, browserClient: { on, send, close } }).resolves(mockPageClient)
send.withArgs('Target.getTargets').resolves({ targetInfos: [{ targetId: '1', url: 'http://foo.com' }, { targetId: '2', url: 'http://bar.com' }] })
send.withArgs('Target.getTargets').resolves({ targetInfos: [{ targetId: '1', url: 'http://foo.com' }, { targetId: '2', url: 'http://bar.com' }] })
send.withArgs('Target.getTargets').resolves({ targetInfos: [{ targetId: '1', url: 'http://foo.com' }, { targetId: '2', url: 'http://bar.com' }, { targetId: '3', url: 'http://baz.com' }] })
criClientCreateStub.withArgs({ target: '1', onAsynchronousError: onError, host: HOST, port: PORT, protocolManager: undefined, fullyManageTabs: undefined, browserClient: { on, send, close } }).resolves(mockPageClient)

const browserClient = await getClient()
const browserClient = await getClient()

const client = await browserClient.attachToTargetUrl('http://foo.com')
const client = await browserClient.attachToTargetUrl('http://foo.com')

expect(client).to.be.equal(mockPageClient)
})
expect(client).to.be.equal(mockPageClient)
})

it('throws when the passed in url is not found after retrying', async function () {
sinon.stub(protocol, '_getDelayMsForRetry')
.onFirstCall().returns(100)
.onSecondCall().returns(undefined)
it('throws when the passed in url is not found after retrying', async function () {
sinon.stub(protocol, '_getDelayMsForRetry')
.onFirstCall().returns(100)
.onSecondCall().returns(undefined)

const mockPageClient = {}
const mockPageClient = {}

send.withArgs('Target.getTargets').resolves({ targetInfos: [{ targetId: '1', url: 'http://foo.com' }, { targetId: '2', url: 'http://bar.com' }] })
send.withArgs('Target.getTargets').resolves({ targetInfos: [{ targetId: '1', url: 'http://foo.com' }, { targetId: '2', url: 'http://bar.com' }] })
criClientCreateStub.withArgs({ target: '1', onAsynchronousError: onError, host: HOST, port: PORT, protocolManager: undefined, fullyManageTabs: undefined, browserClient: { on, send, close } }).resolves(mockPageClient)
send.withArgs('Target.getTargets').resolves({ targetInfos: [{ targetId: '1', url: 'http://foo.com' }, { targetId: '2', url: 'http://bar.com' }] })
send.withArgs('Target.getTargets').resolves({ targetInfos: [{ targetId: '1', url: 'http://foo.com' }, { targetId: '2', url: 'http://bar.com' }] })
criClientCreateStub.withArgs({ target: '1', onAsynchronousError: onError, host: HOST, port: PORT, protocolManager: undefined, fullyManageTabs: undefined, browserClient: { on, send, close } }).resolves(mockPageClient)

const browserClient = await getClient()
const browserClient = await getClient()

await expect(browserClient.attachToTargetUrl('http://baz.com')).to.be.rejected
})
await expect(browserClient.attachToTargetUrl('http://baz.com')).to.be.rejected
})
})

context('#resetBrowserTargets', function () {
it('closes the currently attached target while keeping a tab open', async function () {
const mockCurrentlyAttachedTarget = {
targetId: '100',
close: sinon.stub().resolves(sinon.stub().resolves()),
}
context('#resetBrowserTargets', function () {
it('closes the currently attached target while keeping a tab open', async function () {
const mockCurrentlyAttachedTarget = {
targetId: '100',
close: sinon.stub().resolves(sinon.stub().resolves()),
}

const mockUpdatedCurrentlyAttachedTarget = {
targetId: '101',
}
const mockUpdatedCurrentlyAttachedTarget = {
targetId: '101',
}

send.withArgs('Target.createTarget', { url: 'about:blank' }).resolves(mockUpdatedCurrentlyAttachedTarget)
send.withArgs('Target.closeTarget', { targetId: '100' }).resolves()
criClientCreateStub.withArgs({ target: '101', onAsynchronousError: onError, host: HOST, port: PORT, protocolManager: undefined, fullyManageTabs: undefined }).resolves(mockUpdatedCurrentlyAttachedTarget)
send.withArgs('Target.createTarget', { url: 'about:blank' }).resolves(mockUpdatedCurrentlyAttachedTarget)
send.withArgs('Target.closeTarget', { targetId: '100' }).resolves()
criClientCreateStub.withArgs({ target: '101', onAsynchronousError: onError, host: HOST, port: PORT, protocolManager: undefined, fullyManageTabs: undefined }).resolves(mockUpdatedCurrentlyAttachedTarget)

const browserClient = await getClient() as any
const browserClient = await getClient() as any

browserClient.currentlyAttachedTarget = mockCurrentlyAttachedTarget
browserClient.currentlyAttachedTarget = mockCurrentlyAttachedTarget

await browserClient.resetBrowserTargets(true)
await browserClient.resetBrowserTargets(true)

expect(mockCurrentlyAttachedTarget.close).to.be.called
expect(browserClient.currentlyAttachedTarget).to.eql(mockUpdatedCurrentlyAttachedTarget)
})
expect(mockCurrentlyAttachedTarget.close).to.be.called
expect(browserClient.currentlyAttachedTarget).to.eql(mockUpdatedCurrentlyAttachedTarget)
})

it('closes the currently attached target without keeping a tab open', async function () {
const mockCurrentlyAttachedTarget = {
targetId: '100',
close: sinon.stub().resolves(sinon.stub().resolves()),
}
it('closes the currently attached target without keeping a tab open', async function () {
const mockCurrentlyAttachedTarget = {
targetId: '100',
close: sinon.stub().resolves(sinon.stub().resolves()),
}

send.withArgs('Target.closeTarget', { targetId: '100' }).resolves()
send.withArgs('Target.closeTarget', { targetId: '100' }).resolves()

const browserClient = await getClient() as any
const browserClient = await getClient() as any

browserClient.currentlyAttachedTarget = mockCurrentlyAttachedTarget
browserClient.currentlyAttachedTarget = mockCurrentlyAttachedTarget

await browserClient.resetBrowserTargets(false)
await browserClient.resetBrowserTargets(false)

expect(mockCurrentlyAttachedTarget.close).to.be.called
})
expect(mockCurrentlyAttachedTarget.close).to.be.called
})

it('throws when there is no currently attached target', async function () {
const browserClient = await getClient() as any
it('throws when there is no currently attached target', async function () {
const browserClient = await getClient() as any

await expect(browserClient.resetBrowserTargets()).to.be.rejected
})
await expect(browserClient.resetBrowserTargets()).to.be.rejected
})
})

context('#close', function () {
it('closes the currently attached target if it exists and the browser client', async function () {
const mockCurrentlyAttachedTarget = {
close: sinon.stub().resolves(),
}
context('#close', function () {
it('closes the currently attached target if it exists and the browser client', async function () {
const mockCurrentlyAttachedTarget = {
close: sinon.stub().resolves(),
}

const browserClient = await getClient() as any
const browserClient = await getClient() as any

browserClient.currentlyAttachedTarget = mockCurrentlyAttachedTarget
browserClient.currentlyAttachedTarget = mockCurrentlyAttachedTarget

await browserClient.close()
await browserClient.close()

expect(mockCurrentlyAttachedTarget.close).to.be.called
expect(close).to.be.called
})
expect(mockCurrentlyAttachedTarget.close).to.be.called
expect(close).to.be.called
})

it('just the browser client with no currently attached target', async function () {
const browserClient = await getClient() as any
it('just the browser client with no currently attached target', async function () {
const browserClient = await getClient() as any

await browserClient.close()
await browserClient.close()

expect(close).to.be.called
})
expect(close).to.be.called
})
})
})