Compare commits
6 Commits
selenium-a
...
master
Author | SHA1 | Date |
---|---|---|
John Bintz | 6bfa710b80 | |
John Bintz | bbfac0e23f | |
John Bintz | a0f4fe2099 | |
John Bintz | 9abe069fe8 | |
John Bintz | 576ec2b73b | |
John Bintz | 3e34842eb2 |
|
@ -12,3 +12,10 @@ to recreate my RSpec/Cucumber + Capybara setup purely in JavaScript!
|
||||||
|
|
||||||
`npm install -g persistent_selenium`, then `persistent_selenium`. Point
|
`npm install -g persistent_selenium`, then `persistent_selenium`. Point
|
||||||
your Selenium client to `http://localhost:4443/wd/hub`. Test away!
|
your Selenium client to `http://localhost:4443/wd/hub`. Test away!
|
||||||
|
|
||||||
|
### Command Line Arguments
|
||||||
|
|
||||||
|
* `--only-proxy`: Do not download/start Selenium server locally
|
||||||
|
* `--selenium-host`: The host on which a Selenium server is running (default localhost)
|
||||||
|
* `--selenium-port`: The port on which a selenium server is running (default 4444)
|
||||||
|
* Additional arguments are passed directly to a locally running Selenium server on starting
|
||||||
|
|
102
index.js
102
index.js
|
@ -1,89 +1,29 @@
|
||||||
var selenium = require('selenium-standalone');
|
var selenium = require('selenium-standalone');
|
||||||
var proxy = require('express-http-proxy');
|
var minimist = require('minimist');
|
||||||
var Request = require('request');
|
|
||||||
|
|
||||||
var app = require('express')();
|
var arg = minimist(
|
||||||
var currentSession = null;
|
process.argv.slice(2),
|
||||||
var currentSessionObj = null;
|
{ boolean: ['only-proxy'] }
|
||||||
var seleniumPort = 4444;
|
);
|
||||||
|
|
||||||
var initBrowser = function(cb) {
|
var app = require('./proxy');
|
||||||
Request({
|
app.config = arg;
|
||||||
method: 'post',
|
|
||||||
url: 'http://localhost:' + seleniumPort + '/wd/hub/session/' + currentSession + '/url',
|
function run() {
|
||||||
json: { url: "data:text/html;charset=utf-8;base64,PGh0bWw+PGhlYWQ+PHRpdGxlPm5vZGUtcGVyc2lzdGVudF9zZWxlbml1bTwvdGl0bGU+PGhlYWQ+PGJvZHk+PGgxPm5vZGUtcGVyc2lzdGVudF9zZWxlbml1bSBzdGFydGluZy4uLjwvaDE+PC9ib2R5PjwvaHRtbD4=" }
|
console.log("Persistent selenium listening on port 4443");
|
||||||
}, function(err, response, body) {
|
app.listen(arg['app-port'] || 4443);
|
||||||
cb();
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
app.use(proxy('localhost', {
|
if (arg['only-proxy']) {
|
||||||
filter: function(req, res) {
|
run();
|
||||||
if (currentSession) {
|
} else {
|
||||||
if (req.path === '/wd/hub/session/' + currentSession &&
|
selenium.install({ logger: function(msg) { console.log(msg); }}, function(instErr) {
|
||||||
req.method.toLowerCase() === 'delete') {
|
selenium.start({ spawnOptions: {}, seleniumArgs: arg._ }, function(startErr) {
|
||||||
req._wasDelete = true;
|
if (startErr) {
|
||||||
}
|
console.log(startErr);
|
||||||
|
|
||||||
if (req.path === '/wd/hub/session' &&
|
|
||||||
req.method.toLowerCase() === 'post') {
|
|
||||||
req._wasCreate = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
},
|
|
||||||
decorateRequest: function(req) {
|
|
||||||
req.port = seleniumPort;
|
|
||||||
|
|
||||||
if (currentSession) {
|
|
||||||
if (req.path === '/wd/hub/session/' + currentSession &&
|
|
||||||
req.method.toLowerCase() === 'delete') {
|
|
||||||
req.method = 'GET';
|
|
||||||
req.path = '/wd/hub/sessions';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (req.path === '/wd/hub/session' &&
|
|
||||||
req.method.toLowerCase() === 'post') {
|
|
||||||
req.method = 'GET';
|
|
||||||
req.path = '/wd/hub/sessions';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return req;
|
|
||||||
},
|
|
||||||
intercept: function(origRes, data, req, res, callback) {
|
|
||||||
if (!currentSession && req.url === '/wd/hub/session' &&
|
|
||||||
req.method.toLowerCase() === 'post') {
|
|
||||||
|
|
||||||
currentSessionObj = JSON.parse(data.toString('utf-8'));
|
|
||||||
currentSession = currentSessionObj.sessionId;
|
|
||||||
initBrowser(function() { callback(null, data); });
|
|
||||||
} else {
|
|
||||||
if (req._wasCreate) {
|
|
||||||
data = JSON.stringify(currentSessionObj);
|
|
||||||
initBrowser(function() { callback(null, data); });
|
|
||||||
} else {
|
} else {
|
||||||
if (req._wasDelete) {
|
run();
|
||||||
callback(null, JSON.stringify('{}'));
|
|
||||||
} else {
|
|
||||||
callback(null, data);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
}
|
|
||||||
}));
|
|
||||||
|
|
||||||
var seleniumArgs = process.argv.slice(2);
|
|
||||||
|
|
||||||
selenium.install({ logger: function(msg) { console.log(msg); }}, function(err) {
|
|
||||||
selenium.start({ spawnOptions: {}, seleniumArgs: seleniumArgs }, function(err) {
|
|
||||||
if (err) {
|
|
||||||
console.log(err);
|
|
||||||
} else {
|
|
||||||
console.log("Persistent selenium listening on port 4443");
|
|
||||||
app.listen(4443);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,17 +1,21 @@
|
||||||
{
|
{
|
||||||
"name": "persistent_selenium",
|
"name": "persistent_selenium",
|
||||||
"version": "0.0.4",
|
"version": "0.2.0",
|
||||||
"description": "Keep your Selenium browser windows open while running tests in development.",
|
"description": "Keep your Selenium browser windows open while running tests in development.",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
},
|
},
|
||||||
"keywords": ["selenium", "persistent"],
|
"keywords": [
|
||||||
|
"selenium",
|
||||||
|
"persistent"
|
||||||
|
],
|
||||||
"author": "John Bintz <me@johnbintz.com> (http://johnbintz.com/)",
|
"author": "John Bintz <me@johnbintz.com> (http://johnbintz.com/)",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"express": "^4.12.4",
|
"express": "^4.12.4",
|
||||||
"express-http-proxy": "^0.6.0",
|
"express-http-proxy": "^0.6.0",
|
||||||
|
"minimist": "^1.2.0",
|
||||||
"request": "^2.57.0",
|
"request": "^2.57.0",
|
||||||
"selenium-standalone": "^4.4.2"
|
"selenium-standalone": "^4.4.2"
|
||||||
},
|
},
|
||||||
|
|
|
@ -0,0 +1,79 @@
|
||||||
|
var app = require('express')();
|
||||||
|
var proxy = require('express-http-proxy');
|
||||||
|
var Request = require('request');
|
||||||
|
|
||||||
|
var currentSession = null;
|
||||||
|
var currentSessionObj = null;
|
||||||
|
|
||||||
|
function initBrowser(cb) {
|
||||||
|
var seleniumPort = app.config['selenium-port'] || 4444;
|
||||||
|
var seleniumHost = app.config['selenium-host'] || 'localhost';
|
||||||
|
|
||||||
|
Request({
|
||||||
|
method: 'post',
|
||||||
|
url: 'http://' + seleniumHost + ':' + seleniumPort + '/wd/hub/session/' + currentSession + '/url',
|
||||||
|
json: { url: "data:text/html;charset=utf-8;base64,PGh0bWw+PGhlYWQ+PHRpdGxlPm5vZGUtcGVyc2lzdGVudF9zZWxlbml1bTwvdGl0bGU+PGhlYWQ+PGJvZHk+PGgxPm5vZGUtcGVyc2lzdGVudF9zZWxlbml1bSBzdGFydGluZy4uLjwvaDE+PC9ib2R5PjwvaHRtbD4=" }
|
||||||
|
}, function(err, response, body) {
|
||||||
|
cb();
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
app.use(proxy('localhost', {
|
||||||
|
filter: function(req, res) {
|
||||||
|
if (currentSession) {
|
||||||
|
if (req.path === '/wd/hub/session/' + currentSession &&
|
||||||
|
req.method.toLowerCase() === 'delete') {
|
||||||
|
req._wasDelete = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.path === '/wd/hub/session' &&
|
||||||
|
req.method.toLowerCase() === 'post') {
|
||||||
|
req._wasCreate = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
},
|
||||||
|
decorateRequest: function(req) {
|
||||||
|
req.port = app.config['selenium-port'] || 4444;
|
||||||
|
req.hostname = app.config['selenium-host'] || 'localhost';
|
||||||
|
|
||||||
|
if (currentSession) {
|
||||||
|
if (req.path === '/wd/hub/session/' + currentSession &&
|
||||||
|
req.method.toLowerCase() === 'delete') {
|
||||||
|
req.method = 'GET';
|
||||||
|
req.path = '/wd/hub/sessions';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.path === '/wd/hub/session' &&
|
||||||
|
req.method.toLowerCase() === 'post') {
|
||||||
|
req.method = 'GET';
|
||||||
|
req.path = '/wd/hub/sessions';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return req;
|
||||||
|
},
|
||||||
|
intercept: function(origRes, data, req, res, callback) {
|
||||||
|
if (!currentSession && req.url === '/wd/hub/session' &&
|
||||||
|
req.method.toLowerCase() === 'post') {
|
||||||
|
|
||||||
|
currentSessionObj = JSON.parse(data.toString('utf-8'));
|
||||||
|
currentSession = currentSessionObj.sessionId;
|
||||||
|
initBrowser(function() { callback(null, data); });
|
||||||
|
} else {
|
||||||
|
if (req._wasCreate) {
|
||||||
|
data = JSON.stringify(currentSessionObj);
|
||||||
|
initBrowser(function() { callback(null, data); });
|
||||||
|
} else {
|
||||||
|
if (req._wasDelete) {
|
||||||
|
callback(null, JSON.stringify('{}'));
|
||||||
|
} else {
|
||||||
|
callback(null, data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
|
||||||
|
module.exports = app;
|
Loading…
Reference in New Issue