This repository has been archived by the owner on Jul 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(attachSession): attach protractor to existing webdriver session
Attaching an existing selenium browser session to protractor rather than always creating new one. The session can be passed into the config file as a string via the sessionId.
- Loading branch information
Showing
9 changed files
with
208 additions
and
1 deletion.
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
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,58 @@ | ||
/* | ||
* This is an implementation of the Attach Session Driver Provider. | ||
* It is responsible for setting up the account object, tearing | ||
* it down, and setting up the driver correctly. | ||
*/ | ||
|
||
var util = require('util'), | ||
q = require('q'), | ||
DriverProvider = require('./driverProvider'), | ||
log = require('../logger'), | ||
webdriver = require('selenium-webdriver'), | ||
executors = require('selenium-webdriver/executors'); | ||
|
||
var AttachedSessionDriverProvider = function(config) { | ||
DriverProvider.call(this, config); | ||
}; | ||
util.inherits(AttachedSessionDriverProvider, DriverProvider); | ||
|
||
/** | ||
* Configure and launch (if applicable) the object's environment. | ||
* @public | ||
* @return {q.promise} A promise which will resolve when the environment is | ||
* ready to test. | ||
*/ | ||
AttachedSessionDriverProvider.prototype.setupEnv = function() { | ||
log.puts('Using the selenium server at ' + this.config_.seleniumAddress); | ||
log.puts('Using session id - ' + this.config_.seleniumSessionId); | ||
return q(undefined); | ||
}; | ||
|
||
|
||
/** | ||
* Getting a new driver by attaching an existing session. | ||
* | ||
* @public | ||
* @return {webdriver.WebDriver} webdriver instance | ||
*/ | ||
AttachedSessionDriverProvider.prototype.getNewDriver = function() { | ||
var executor = executors.createExecutor(this.config_.seleniumAddress); | ||
var newDriver; | ||
newDriver = new webdriver.WebDriver | ||
.attachToSession(executor, this.config_.seleniumSessionId); | ||
this.drivers_.push(newDriver); | ||
return newDriver; | ||
}; | ||
|
||
/** | ||
* Maintains the existing session and does not quit the driver. | ||
* | ||
* @public | ||
*/ | ||
AttachedSessionDriverProvider.prototype.quitDriver = function() { | ||
}; | ||
|
||
// new instance w/ each include | ||
module.exports = function(config) { | ||
return new AttachedSessionDriverProvider(config); | ||
}; |
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,105 @@ | ||
#!/usr/bin/env node | ||
|
||
'use strict'; | ||
|
||
var http = require('http'), | ||
spawn = require('child_process').spawnSync; | ||
|
||
var sessionId = ''; | ||
|
||
// 1. Create a new selenium session. | ||
var postData = JSON.stringify( | ||
{'desiredCapabilities': {'browserName': 'firefox'}}); | ||
var createOptions = { | ||
hostname: 'localhost', | ||
port: 4444, | ||
path: '/wd/hub/session', | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
'Content-Length': Buffer.byteLength(postData) | ||
} | ||
}; | ||
var req = http.request(createOptions, function(res) { | ||
res.on('data', setBody); | ||
res.on('end', checkSession); | ||
}); | ||
req.write(postData); | ||
req.end(); | ||
|
||
// 2. After making the request to create a selenium session, read the selenium | ||
// session id. | ||
var setBody = function(chunk) { | ||
var body = chunk.toString(); | ||
sessionId = JSON.parse(body).sessionId; | ||
}; | ||
|
||
// 3. After getting the session id, verify that the selenium session exists. | ||
// If the session exists, run the protractor test. | ||
var checkSession = function() { | ||
var checkOptions = { | ||
hostname: 'localhost', | ||
port: 4444, | ||
path: '/wd/hub/session/' + sessionId, | ||
method: 'GET' | ||
}; | ||
var state = ''; | ||
var req = http.request(checkOptions, function(res) { | ||
res.on('data', function(chunk) { | ||
state = JSON.parse(chunk.toString()).state; | ||
}); | ||
res.on('end', function() { | ||
if (state === 'success') { | ||
var runProtractor = spawn('bin/protractor', | ||
['spec/attachSession.js', '--seleniumSessionId=' + sessionId]); | ||
console.log(runProtractor.stdout.toString()); | ||
if (runProtractor.status !== 0) { | ||
throw new Error('Protractor did not run properly.'); | ||
} | ||
} | ||
else { | ||
throw new Error('The selenium session was not created.'); | ||
} | ||
checkStoppedSession(); | ||
}); | ||
}); | ||
req.end(); | ||
}; | ||
|
||
// 4. After the protractor test completes, check to see that the session still | ||
// exists. If we can find the session, delete it. | ||
var checkStoppedSession = function() { | ||
var checkOptions = { | ||
hostname: 'localhost', | ||
port: 4444, | ||
path: '/wd/hub/session/' + sessionId, | ||
method: 'GET' | ||
}; | ||
var state = ''; | ||
var req = http.request(checkOptions, function(res) { | ||
res.on('data', function(chunk) { | ||
state = JSON.parse(chunk.toString()).state; | ||
}); | ||
res.on('end', function() { | ||
if (state === 'success') { | ||
deleteSession(); | ||
} | ||
else { | ||
throw new Error('The selenium session should still exist.'); | ||
} | ||
}); | ||
}); | ||
req.end(); | ||
}; | ||
|
||
// 5. Delete the selenium session. | ||
var deleteSession = function() { | ||
var deleteOptions = { | ||
hostname: 'localhost', | ||
port: 4444, | ||
path: '/wd/hub/session/' + sessionId, | ||
method: 'DELETE' | ||
}; | ||
var req = http.request(deleteOptions); | ||
req.end(); | ||
}; |
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,21 @@ | ||
var env = require('./environment.js'); | ||
|
||
exports.config = { | ||
seleniumAddress: env.seleniumAddress, | ||
|
||
framework: 'jasmine', | ||
|
||
specs: [ | ||
'attachSessionProvider/attachSession_spec.js' | ||
], | ||
|
||
capabilities: env.capabilities, | ||
|
||
baseUrl: 'http://localhost:8081', | ||
|
||
// Special option for Angular2, to test against all Angular2 applications | ||
// on the page. This means that Protractor will wait for every app to be | ||
// stable before each action, and search within all apps when finding | ||
// elements. | ||
useAllAngular2AppRoots: true | ||
}; |
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,11 @@ | ||
describe('selenium session id', function() { | ||
var URL = '/ng2/#/async'; | ||
|
||
beforeEach(function() { | ||
browser.get(URL); | ||
}); | ||
it('should be able to use an existing session', function() { | ||
var increment = $('#increment'); | ||
expect(increment).toBeDefined(); | ||
}); | ||
}); |
3f3805f
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.
Hi @juliemr any examples for this to play in real time projects? pls add points here
http://stackoverflow.com/questions/38451848/how-to-connect-and-use-an-already-opened-browser-window-in-protractor