first commit
This commit is contained in:
56
code/7-multibrowser/browser.js
Normal file
56
code/7-multibrowser/browser.js
Normal file
@@ -0,0 +1,56 @@
|
||||
var Browser = function (options) {
|
||||
this.options = options;
|
||||
};
|
||||
|
||||
Browser.prototype.getSession = function (cb) {
|
||||
var args = {
|
||||
desiredCapabilities: {
|
||||
browserName: this.options.name,
|
||||
version: '',
|
||||
platform: 'ANY'
|
||||
}
|
||||
};
|
||||
|
||||
this._send('POST', '/session', cb, args);
|
||||
};
|
||||
|
||||
Browser.prototype.closeSession = function () {
|
||||
this._send('DELETE', '/session/:sessionId', function () {});
|
||||
};
|
||||
|
||||
Browser.prototype.open = function (url, cb) {
|
||||
this._send('POST', '/session/:sessionId/url', cb, {url: url});
|
||||
};
|
||||
|
||||
Browser.prototype.getTitle = function (cb) {
|
||||
this._send('GET', '/session/:sessionId/title', cb);
|
||||
};
|
||||
|
||||
Browser.prototype.getScreenshot = function (cb) {
|
||||
this._send('GET', '/session/:sessionId/screenshot', cb);
|
||||
};
|
||||
|
||||
Browser.prototype.resize = function (size, cb) {
|
||||
this._send('POST', '/session/:sessionId/window/current/size', cb, size);
|
||||
};
|
||||
|
||||
Browser.prototype.setSession = function (session) {
|
||||
this._session = session;
|
||||
};
|
||||
|
||||
Browser.prototype._send = function (method, command, cb, options) {
|
||||
var request = new XMLHttpRequest();
|
||||
var url = 'http://' + this.options.addr + ':' + this.options.port + command;
|
||||
request.open(method, url.replace(':sessionId', this._session), true);
|
||||
request.setRequestHeader('Content-Type', 'application/json');
|
||||
|
||||
request.onload = function() {
|
||||
cb(null, JSON.parse(request.responseText));
|
||||
};
|
||||
|
||||
request.onerror = function() {
|
||||
cb(arguments, null);
|
||||
};
|
||||
|
||||
request.send((options ? JSON.stringify(options) : ''));
|
||||
};
|
||||
39
code/7-multibrowser/index.html
Normal file
39
code/7-multibrowser/index.html
Normal file
@@ -0,0 +1,39 @@
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Multibrowser screenshotting</title>
|
||||
<meta name="description" content="">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<style>
|
||||
img { border: 3px solid #333 }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Multibrowser screenshots</h1>
|
||||
|
||||
<table border="1">
|
||||
<tr>
|
||||
<td colspan="2">PhantomJS</td>
|
||||
<td colspan="2">Chrome</td>
|
||||
<td colspan="2">Firefox</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Title:</td>
|
||||
<td id="phantom-title">-</td>
|
||||
<td>Title:</td>
|
||||
<td id="chrome-title">-</td>
|
||||
<td>Title:</td>
|
||||
<td id="firefox-title">-</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td colspan="2" valign="top"><img src="" id="phantom-shot"/></td>
|
||||
<td colspan="2" valign="top"><img src="" id="chrome-shot"/></td>
|
||||
<td colspan="2" valign="top"><img src="" id="firefox-shot"/></td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<script src="browser.js"></script>
|
||||
<script src="script.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
57
code/7-multibrowser/script.js
Normal file
57
code/7-multibrowser/script.js
Normal file
@@ -0,0 +1,57 @@
|
||||
var phantomjs = new Browser({name: 'phantomjs', port: 7020, addr: 'localhost'});
|
||||
var chrome = new Browser({name: 'chrome', port: 9515, addr: 'localhost'});
|
||||
var firefox = new Browser({name: 'firefox', port: 8000, addr: 'localhost'});
|
||||
//var ie = new Browser({name: 'InternetExplorer', port: 5555, addr: 'localhost'});
|
||||
|
||||
var url = 'http://amazon.de';
|
||||
var windowSize = {width: 1440, height: 2048};
|
||||
|
||||
phantomjs.getSession(function (err, session) {
|
||||
phantomjs.setSession(session.sessionId);
|
||||
phantomjs.open(url, function (err) {
|
||||
phantomjs.getTitle(function (err, title) {
|
||||
document.getElementById('phantom-title').innerHTML = title.value;
|
||||
phantomjs.resize(windowSize, function () {
|
||||
phantomjs.getScreenshot(function (err, screenshot) {
|
||||
var img = document.getElementById('phantom-shot');
|
||||
img.src = 'data:image/png;base64,' + screenshot.value;
|
||||
phantomjs.closeSession();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
chrome.getSession(function (err, session) {
|
||||
chrome.setSession(session.sessionId);
|
||||
chrome.open(url, function (err) {
|
||||
chrome.getTitle(function (err, title) {
|
||||
document.getElementById('chrome-title').innerHTML = title.value;
|
||||
chrome.resize(windowSize, function () {
|
||||
chrome.getScreenshot(function (err, screenshot) {
|
||||
var img = document.getElementById('chrome-shot');
|
||||
img.src = 'data:image/png;base64,' + screenshot.value;
|
||||
//img.style.width = windowSize.width + 'px';
|
||||
//img.style.height = windowSize.height + 'px';
|
||||
chrome.closeSession();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
firefox.getSession(function (err, session) {
|
||||
firefox.setSession(session.sessionId);
|
||||
firefox.open(url, function (err) {
|
||||
firefox.getTitle(function (err, title) {
|
||||
document.getElementById('firefox-title').innerHTML = title.value;
|
||||
firefox.resize(windowSize, function () {
|
||||
firefox.getScreenshot(function (err, screenshot) {
|
||||
var img = document.getElementById('firefox-shot');
|
||||
img.src = 'data:image/png;base64,' + screenshot.value;
|
||||
firefox.closeSession();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user