first commit
This commit is contained in:
BIN
code/.DS_Store
vendored
Normal file
BIN
code/.DS_Store
vendored
Normal file
Binary file not shown.
13
code/1-status/index.html
Normal file
13
code/1-status/index.html
Normal file
@@ -0,0 +1,13 @@
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title> WebDiver testpage</title>
|
||||
<meta name="description" content="">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
</head>
|
||||
<body>
|
||||
<h1>This is a WebDiver testpage</h1>
|
||||
<script src="webdriver.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
19
code/1-status/webdriver.js
Normal file
19
code/1-status/webdriver.js
Normal file
@@ -0,0 +1,19 @@
|
||||
// Query the status method
|
||||
request = new XMLHttpRequest();
|
||||
request.open('GET', 'http://localhost:7020/status', true);
|
||||
|
||||
request.onload = function() {
|
||||
// Put the received data into the dom
|
||||
var el = document.createElement('div');
|
||||
var contents = document.createTextNode(request.responseText);
|
||||
el.appendChild(contents);
|
||||
document.getElementsByTagName('body')[0].appendChild(el);
|
||||
};
|
||||
|
||||
request.onerror = function() {
|
||||
// There was a connection error of some sort
|
||||
console.error('err:', arguments);
|
||||
};
|
||||
|
||||
// Fire the request
|
||||
request.send();
|
||||
13
code/2-session/index.html
Normal file
13
code/2-session/index.html
Normal file
@@ -0,0 +1,13 @@
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title> WebDiver testpage</title>
|
||||
<meta name="description" content="">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
</head>
|
||||
<body>
|
||||
<h1>This is a WebDiver testpage</h1>
|
||||
<script src="webdriver.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
30
code/2-session/webdriver.js
Normal file
30
code/2-session/webdriver.js
Normal file
@@ -0,0 +1,30 @@
|
||||
var getSession = function (cb) {
|
||||
var request = new XMLHttpRequest();
|
||||
request.open('POST', 'http://localhost:7020/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(JSON.stringify({
|
||||
desiredCapabilities: {
|
||||
browserName: 'phantomjs',
|
||||
version: '',
|
||||
platform: 'ANY'
|
||||
}
|
||||
}));
|
||||
};
|
||||
|
||||
|
||||
getSession(function (err, data) {
|
||||
var el = document.createElement('div');
|
||||
var contents = document.createTextNode(JSON.stringify(data));
|
||||
el.appendChild(contents);
|
||||
document.getElementsByTagName('body')[0].appendChild(el);
|
||||
});
|
||||
|
||||
13
code/3-url/index.html
Normal file
13
code/3-url/index.html
Normal file
@@ -0,0 +1,13 @@
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title> WebDiver testpage</title>
|
||||
<meta name="description" content="">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
</head>
|
||||
<body>
|
||||
<h1>This is a WebDiver testpage</h1>
|
||||
<script src="webdriver.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
47
code/3-url/webdriver.js
Normal file
47
code/3-url/webdriver.js
Normal file
@@ -0,0 +1,47 @@
|
||||
var openUrl = function (url, session, cb) {
|
||||
var request = new XMLHttpRequest();
|
||||
request.open('POST', 'http://localhost:7020/session/' + session + '/url', true);
|
||||
request.setRequestHeader('Content-Type', 'application/json');
|
||||
|
||||
request.onload = function() {
|
||||
cb(null, JSON.parse(request.responseText));
|
||||
};
|
||||
|
||||
request.onerror = function() {
|
||||
cb(arguments, null);
|
||||
};
|
||||
|
||||
request.send(JSON.stringify({url: url}));
|
||||
};
|
||||
|
||||
var getSession = function (cb) {
|
||||
var request = new XMLHttpRequest();
|
||||
request.open('POST', 'http://localhost:7020/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(JSON.stringify({
|
||||
desiredCapabilities: {
|
||||
browserName: 'phantomjs',
|
||||
version: '',
|
||||
platform: 'ANY'
|
||||
}
|
||||
}));
|
||||
};
|
||||
|
||||
getSession(function (err, data) {
|
||||
openUrl('http://localhost:5000', data.sessionId, function (err, url) {
|
||||
var success = err ? 'Nope.' : 'Yep.';
|
||||
var el = document.createElement('div');
|
||||
var contents = document.createTextNode('Success: ' + success);
|
||||
el.appendChild(contents);
|
||||
document.getElementsByTagName('body')[0].appendChild(el);
|
||||
});
|
||||
});
|
||||
13
code/4-title/index.html
Normal file
13
code/4-title/index.html
Normal file
@@ -0,0 +1,13 @@
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title> WebDiver testpage</title>
|
||||
<meta name="description" content="">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
</head>
|
||||
<body>
|
||||
<h1>This is a WebDiver testpage</h1>
|
||||
<script src="webdriver.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
64
code/4-title/webdriver.js
Normal file
64
code/4-title/webdriver.js
Normal file
@@ -0,0 +1,64 @@
|
||||
var getPageTitle = function (session, cb) {
|
||||
var request = new XMLHttpRequest();
|
||||
request.open('GET', 'http://localhost:7020/session/' + session + '/title', true);
|
||||
request.setRequestHeader('Content-Type', 'application/json');
|
||||
|
||||
request.onload = function() {
|
||||
cb(null, JSON.parse(request.responseText));
|
||||
};
|
||||
|
||||
request.onerror = function() {
|
||||
cb(arguments, null);
|
||||
};
|
||||
|
||||
request.send();
|
||||
};
|
||||
|
||||
var openUrl = function (url, session, cb) {
|
||||
var request = new XMLHttpRequest();
|
||||
request.open('POST', 'http://localhost:7020/session/' + session + '/url', true);
|
||||
request.setRequestHeader('Content-Type', 'application/json');
|
||||
|
||||
request.onload = function() {
|
||||
cb(null);
|
||||
};
|
||||
|
||||
request.onerror = function() {
|
||||
cb(arguments);
|
||||
};
|
||||
|
||||
request.send(JSON.stringify({url: url}));
|
||||
};
|
||||
|
||||
var getSession = function (cb) {
|
||||
var request = new XMLHttpRequest();
|
||||
request.open('POST', 'http://localhost:7020/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(JSON.stringify({
|
||||
desiredCapabilities: {
|
||||
browserName: 'phantomjs',
|
||||
version: '',
|
||||
platform: 'ANY'
|
||||
}
|
||||
}));
|
||||
};
|
||||
|
||||
getSession(function (err, data) {
|
||||
openUrl('http://localhost:5000', data.sessionId, function (err) {
|
||||
getPageTitle(data.sessionId, function (err, title) {
|
||||
var el = document.createElement('div');
|
||||
var contents = document.createTextNode(JSON.stringify(title));
|
||||
el.appendChild(contents);
|
||||
document.getElementsByTagName('body')[0].appendChild(el);
|
||||
});
|
||||
});
|
||||
});
|
||||
13
code/5-closeSession/index.html
Normal file
13
code/5-closeSession/index.html
Normal file
@@ -0,0 +1,13 @@
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title> WebDiver testpage</title>
|
||||
<meta name="description" content="">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
</head>
|
||||
<body>
|
||||
<h1>This is a WebDiver testpage</h1>
|
||||
<script src="webdriver.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
72
code/5-closeSession/webdriver.js
Normal file
72
code/5-closeSession/webdriver.js
Normal file
@@ -0,0 +1,72 @@
|
||||
var closeSession = function (session) {
|
||||
var request = new XMLHttpRequest();
|
||||
request.open('DELETE', 'http://localhost:7020/session/' + session, true);
|
||||
request.setRequestHeader('Content-Type', 'application/json');
|
||||
request.send();
|
||||
};
|
||||
|
||||
var getPageTitle = function (session, cb) {
|
||||
var request = new XMLHttpRequest();
|
||||
request.open('GET', 'http://localhost:7020/session/' + session + '/title', true);
|
||||
request.setRequestHeader('Content-Type', 'application/json');
|
||||
|
||||
request.onload = function() {
|
||||
cb(null, JSON.parse(request.responseText));
|
||||
};
|
||||
|
||||
request.onerror = function() {
|
||||
cb(arguments, null);
|
||||
};
|
||||
|
||||
request.send();
|
||||
};
|
||||
|
||||
var openUrl = function (url, session, cb) {
|
||||
var request = new XMLHttpRequest();
|
||||
request.open('POST', 'http://localhost:7020/session/' + session + '/url', true);
|
||||
request.setRequestHeader('Content-Type', 'application/json');
|
||||
|
||||
request.onload = function() {
|
||||
cb(null);
|
||||
};
|
||||
|
||||
request.onerror = function() {
|
||||
cb(arguments);
|
||||
};
|
||||
|
||||
request.send(JSON.stringify({url: url}));
|
||||
};
|
||||
|
||||
var getSession = function (cb) {
|
||||
var request = new XMLHttpRequest();
|
||||
request.open('POST', 'http://localhost:7020/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(JSON.stringify({
|
||||
desiredCapabilities: {
|
||||
browserName: 'phantomjs',
|
||||
version: '',
|
||||
platform: 'ANY'
|
||||
}
|
||||
}));
|
||||
};
|
||||
|
||||
getSession(function (err, data) {
|
||||
openUrl('http://localhost:5000', data.sessionId, function (err) {
|
||||
getPageTitle(data.sessionId, function (err, title) {
|
||||
var el = document.createElement('div');
|
||||
var contents = document.createTextNode(JSON.stringify(title));
|
||||
el.appendChild(contents);
|
||||
document.getElementsByTagName('body')[0].appendChild(el);
|
||||
closeSession(data.sessionId);
|
||||
});
|
||||
});
|
||||
});
|
||||
16
code/6-screenshot/index.html
Normal file
16
code/6-screenshot/index.html
Normal file
@@ -0,0 +1,16 @@
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title> WebDiver testpage</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>This is a WebDiver testpage</h1>
|
||||
<script src="webdriver.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
71
code/6-screenshot/webdriver.js
Normal file
71
code/6-screenshot/webdriver.js
Normal file
@@ -0,0 +1,71 @@
|
||||
var getScreenshot = function (session, cb) {
|
||||
var request = new XMLHttpRequest();
|
||||
request.open('GET', 'http://localhost:7020/session/' + session + '/screenshot', true);
|
||||
request.setRequestHeader('Content-Type', 'application/json');
|
||||
|
||||
request.onload = function() {
|
||||
cb(null, JSON.parse(request.responseText));
|
||||
};
|
||||
|
||||
request.onerror = function() {
|
||||
cb(arguments, null);
|
||||
};
|
||||
|
||||
request.send();
|
||||
};
|
||||
|
||||
var closeSession = function (session) {
|
||||
var request = new XMLHttpRequest();
|
||||
request.open('DELETE', 'http://localhost:7020/session/' + session, true);
|
||||
request.setRequestHeader('Content-Type', 'application/json');
|
||||
request.send();
|
||||
};
|
||||
|
||||
var openUrl = function (url, session, cb) {
|
||||
var request = new XMLHttpRequest();
|
||||
request.open('POST', 'http://localhost:7020/session/' + session + '/url', true);
|
||||
request.setRequestHeader('Content-Type', 'application/json');
|
||||
|
||||
request.onload = function() {
|
||||
cb(null);
|
||||
};
|
||||
|
||||
request.onerror = function() {
|
||||
cb(arguments);
|
||||
};
|
||||
|
||||
request.send(JSON.stringify({url: url}));
|
||||
};
|
||||
|
||||
var getSession = function (cb) {
|
||||
var request = new XMLHttpRequest();
|
||||
request.open('POST', 'http://localhost:7020/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(JSON.stringify({
|
||||
desiredCapabilities: {
|
||||
browserName: 'phantomjs',
|
||||
version: '',
|
||||
platform: 'ANY'
|
||||
}
|
||||
}));
|
||||
};
|
||||
|
||||
getSession(function (err, data) {
|
||||
openUrl('http://reuter.de', data.sessionId, function (err) {
|
||||
getScreenshot(data.sessionId, function (err, screenshot) {
|
||||
var img = document.createElement('img');
|
||||
img.src = 'data:image/png;base64,' + screenshot.value;
|
||||
document.getElementsByTagName('body')[0].appendChild(img);
|
||||
closeSession(data.sessionId);
|
||||
});
|
||||
});
|
||||
});
|
||||
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();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
15
code/sample_page/index.html
Normal file
15
code/sample_page/index.html
Normal file
@@ -0,0 +1,15 @@
|
||||
<!doctype>
|
||||
<html>
|
||||
<head>
|
||||
<meta charste="utf-8">
|
||||
<title>I will be replaced</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>I will be replaced</h1>
|
||||
<script>
|
||||
var ua = window.navigator.userAgent;
|
||||
document.getElementsByTagName('title')[0].innerHTML = ua;
|
||||
document.getElementsByTagName('h1')[0].innerHTML = ua;
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user