forked from DevExpress/testcafe
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement uniform URL for connecting remote browsers (closes DevExpre…
- Loading branch information
1 parent
03eee60
commit 87ad77a
Showing
3 changed files
with
99 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import Promise from 'pinkie'; | ||
import { EventEmitter } from 'events'; | ||
import promisifyEvent from 'promisify-event'; | ||
import timeLimit from 'time-limit-promise'; | ||
|
||
|
||
const REMOTE_REDIRECT_TIMEOUT = 10000; | ||
const ADDING_CONNECTION_WAITING_TIMEOUT = 10000; | ||
|
||
export default class RemotesQueue { | ||
constructor () { | ||
this.events = new EventEmitter(); | ||
this.shiftingTimeout = Promise.resolve(); | ||
this.pendingConnections = {}; | ||
} | ||
|
||
add (remoteConnection) { | ||
var connectionReadyPromise = promisifyEvent(remoteConnection, 'ready') | ||
.then(() => this.remove(remoteConnection)); | ||
|
||
this.pendingConnections[remoteConnection.id] = { | ||
connection: remoteConnection, | ||
readyPromise: connectionReadyPromise | ||
}; | ||
|
||
this.events.emit('connection-added', remoteConnection.id); | ||
} | ||
|
||
remove (remoteConnection) { | ||
delete this.pendingConnections[remoteConnection.id]; | ||
} | ||
|
||
shift () { | ||
var shiftingPromise = this.shiftingTimeout | ||
.then(async () => { | ||
var headId = Object.keys(this.pendingConnections)[0]; | ||
|
||
if (!headId) | ||
headId = await timeLimit(promisifyEvent(this.events, 'connection-added'), ADDING_CONNECTION_WAITING_TIMEOUT); | ||
|
||
return headId ? this.pendingConnections[headId].connection : null; | ||
}); | ||
|
||
this.shiftingTimeout = shiftingPromise | ||
.then(connection => { | ||
if (!connection) | ||
return Promise.resolve(); | ||
|
||
return timeLimit(this.pendingConnections[connection.id].readyPromise, REMOTE_REDIRECT_TIMEOUT); | ||
}); | ||
|
||
return shiftingPromise; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,49 @@ | ||
import Promise from 'pinkie'; | ||
import qrcode from 'qrcode-terminal'; | ||
import chalk from 'chalk'; | ||
import log from './log'; | ||
import promisifyEvent from 'promisify-event'; | ||
import dedent from 'dedent'; | ||
|
||
|
||
export default async function (testCafe, remoteCount, showQRCode) { | ||
var connections = []; | ||
var connectionPromises = []; | ||
|
||
if (remoteCount) { | ||
log.hideSpinner(); | ||
|
||
var description = dedent(` | ||
Connecting ${remoteCount} remote browser(s)... | ||
Navigate to the appropriate URL from each of the remote browsers. | ||
Navigate to the following URL from each remote browser. | ||
`); | ||
|
||
log.write(description); | ||
|
||
if (showQRCode) | ||
log.write('You can either enter the URL or scan the QR-code.'); | ||
|
||
for (var i = 0; i < remoteCount; i++) { | ||
var browserConnection = await testCafe.createBrowserConnection(); | ||
|
||
log.write(`Browser #${i + 1}: ${chalk.underline.blue(browserConnection.url)}`); | ||
var connectionUrl = testCafe.browserConnectionGateway.connectUrl; | ||
|
||
if (showQRCode) | ||
qrcode.generate(browserConnection.url); | ||
log.write(`Connect URL: ${chalk.underline.blue(connectionUrl)}`); | ||
|
||
await promisifyEvent(browserConnection, 'ready'); | ||
if (showQRCode) | ||
qrcode.generate(connectionUrl); | ||
|
||
connections.push(browserConnection); | ||
log.write(`${chalk.green('CONNECTED')} ${browserConnection.userAgent}\n`); | ||
for (var i = 0; i < remoteCount; i++) { | ||
connectionPromises.push(testCafe | ||
.createBrowserConnection() | ||
.then(bc => promisifyEvent(bc, 'ready').then(() => bc)) | ||
.then(bc => { | ||
log.hideSpinner(); | ||
log.write(`${chalk.green('CONNECTED')} ${bc.userAgent}`); | ||
log.showSpinner(); | ||
return bc; | ||
}) | ||
); | ||
} | ||
|
||
log.showSpinner(); | ||
} | ||
|
||
return connections; | ||
return await Promise.all(connectionPromises); | ||
} |