forked from browserstack/browserstack-runner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrowserstack.js
64 lines (56 loc) · 2.32 KB
/
browserstack.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
(function(){
var BrowserStack = {};
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
// Tiny Ajax Post
var post = function (url, json, cb) {
var req;
if (window.ActiveXObject)
req = new ActiveXObject('Microsoft.XMLHTTP');
else if (window.XMLHttpRequest)
req = new XMLHttpRequest();
else
throw "Strider: No ajax"
req.onreadystatechange = function () {
if (req.readyState==4)
cb(req.responseText);
};
var data;
if(window.CircularJSON) {
data = window.CircularJSON.stringify(json);
} else {
data = JSON.stringify(json);
}
req.open("POST", url, true);
req.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
req.setRequestHeader('X-Browser-String', BrowserStack.browser_string);
req.setRequestHeader('X-Worker-UUID', BrowserStack.worker_uuid);
req.setRequestHeader('Content-type', 'application/json');
req.send(data);
}
// Change some console method to capture the logs.
// This must not replace the console object itself so that other console methods
// from the browser or added by the tested application remain unaffected.
// https://github.com/browserstack/browserstack-runner/pull/199
var browserstack_console = window.console || {};
browserstack_console.log = function () {
var args = BrowserStack.util.toArray(arguments).map(BrowserStack.util.inspect);
post('/_log/', { arguments: args }, function () {});
};
browserstack_console.warn = function () {
var args = BrowserStack.util.toArray(arguments).map(BrowserStack.util.inspect);
post('/_log/', { arguments: args }, function () {});
};
BrowserStack.post = post;
BrowserStack.getParameterByName = getParameterByName;
BrowserStack.browser_string = getParameterByName('_browser_string');
BrowserStack.worker_uuid = getParameterByName('_worker_key');
window.BrowserStack = BrowserStack;
// If the browser didn't have a console object (old IE), then this will create it.
// Otherwise this is a no-op as it will assign the same object it already held.
window.console = browserstack_console;
})();