-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathinject.js
105 lines (82 loc) · 5.91 KB
/
inject.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
(function () {
window.protractor = {};
window.protractor.logs = [];
window.protractor.ignoreSynchronization = false;
Event.prototype.stopPropagation = function () { };
var url = '';
var time = null;
var mouse = [];
document.addEventListener('click', function (e) {
if (e.target.tagName.toLowerCase() != 'canvas')
log('element(by.css(\'' + selector(e.target).replace(/\\\"/g, '\\\\\\"') + '\'))' + '.click();');
});
document.addEventListener('mousedown', function (e) {
if (e.target.tagName.toLowerCase() == 'canvas')
mouse = [e];
});
document.addEventListener('mousemove', function (e) {
if (e.target.tagName.toLowerCase() == 'canvas' && mouse && mouse.length > 0)
mouse.push(e);
});
document.addEventListener('mouseup', function (e) {
if (e.target.tagName.toLowerCase() == 'canvas' && mouse && mouse.length > 0 && mouse[0].target == e.target) {
if (mouse.reduce(function (a, b) { return a.clientX - b.clientX; }, mouse[0]) <= 1 && mouse.reduce(function (a, b) { return a.clientY - b.clientY; }, mouse[0]) <= 1)
log('browser.driver.actions().mouseMove(element(by.css(\'' + selector(mouse[0].target).replace(/\\\"/g, '\\\\\\"') + '\')), {x: ' + mouse[0].clientX.toString() + ', y:' + mouse[0].clientY.toString() + '}).click().perform();');
else
log('browser.driver.actions().mouseMove(element(by.css(\'' + selector(mouse[0].target).replace(/\\\"/g, '\\\\\\"') + '\')), {x: ' + mouse[0].clientX.toString() + ', y:' + mouse[0].clientY.toString() + '}).mouseDown()' + mouse.reduce(function (a, b, i) { return i > 0 ? a + '.mouseMove({x: ' + (b.clientX - mouse[i - 1].clientX).toString() + ', y:' + (b.clientY - mouse[i - 1].clientY).toString() + '})' : ''; }, '') + '.mouseUp().perform();');
}
});
document.addEventListener('touchstart', function (e) {
if (e.target.tagName.toLowerCase() == 'canvas' && e.targetTouches && e.targetTouches.length > 0)
mouse = [{ target: e.target, clientX: Math.floor(e.targetTouches[0].clientX), clientY: Math.floor(e.targetTouches[0].clientY) }];
});
document.addEventListener('touchmove', function (e) {
if (e.target.tagName.toLowerCase() == 'canvas' && e.targetTouches && e.targetTouches.length > 0 && mouse && mouse.length > 0)
mouse.push({ target: e.target, clientX: Math.floor(e.targetTouches[0].clientX), clientY: Math.floor(e.targetTouches[0].clientY) });
});
document.addEventListener('touchend', function (e) {
if (e.target.tagName.toLowerCase() == 'canvas' && mouse && mouse.length > 0 && mouse[0].target == e.target) {
if (mouse.reduce(function (a, b) { return a.clientX - b.clientX; }, mouse[0]) <= 1 && mouse.reduce(function (a, b) { return a.clientY - b.clientY; }, mouse[0]) <= 1)
log('browser.driver.actions().mouseMove(element(by.css(\'' + selector(mouse[0].target).replace(/\\\"/g, '\\\\\\"') + '\')), {x: ' + mouse[0].clientX.toString() + ', y:' + mouse[0].clientY.toString() + '}).click().perform();');
else
log('browser.driver.actions().mouseMove(element(by.css(\'' + selector(mouse[0].target).replace(/\\\"/g, '\\\\\\"') + '\')), {x: ' + mouse[0].clientX.toString() + ', y:' + mouse[0].clientY.toString() + '}).mouseDown()' + mouse.reduce(function (a, b, i) { return i > 0 ? a + '.mouseMove({x: ' + (b.clientX - mouse[i - 1].clientX).toString() + ', y:' + (b.clientY - mouse[i - 1].clientY).toString() + '})' : ''; }, '') + '.mouseUp().perform();');
}
});
document.addEventListener('change', function (e) {
if (e.target.tagName.toLowerCase() == 'input' && ['text', 'password', 'number'].indexOf(e.target.getAttribute('type').toLowerCase()) != -1)
log('element(by.css(\'' + selector(e.target).replace(/\\\"/g, '\\\\\\"') + '\'))' + '.clear().sendKeys(\'' + e.target.value + '\');');
else if (e.target.tagName.toLowerCase() == 'textarea')
log('element(by.css(\'' + selector(e.target).replace(/\\\"/g, '\\\\\\"') + '\'))' + '.clear().sendKeys(\'' + e.target.value + '\');');
});
var selector = function (target) {
var query = '';
if (target == document)
query = 'body';
else {
var attr = ['ng-model', 'ng-href', 'name', 'aria-label'].reduce(function (a, b) { return a || (target.getAttribute(b) ? b : null); }, null);
if (attr)
query = target.tagName.toLowerCase() + '[' + attr + '="' + target.getAttribute(attr).replace(/\\/g, '\\\\').replace(/\'/g, '\\\'').replace(/\"/g, '\\"').replace(/\0/g, '\\0') + '"]';
else
query = target.tagName.toLowerCase();
var nodes = target.parentNode.querySelectorAll(query);
if (nodes && nodes.length > 1)
query += ':nth-of-type(' + (Array.prototype.slice.call(nodes).indexOf(target) + 1).toString() + ')';
query = query.replace(/\s/g, ' ');
}
if (document.querySelectorAll(query).length > 1 && target.parentNode)
query = selector(target.parentNode) + '>' + query;
return query;
};
var log = function (action) {
if (window.protractor.logs.length == 0)
window.protractor.logs.push('browser.driver.manage().window().setSize(' + window.outerWidth + ', ' + window.outerHeight + ');');
if (window.protractor.ignoreSynchronization && time)
window.protractor.logs.push('browser.sleep(' + (new Date() - time).toString() + ');');
time = new Date();
if (!url || url != window.location.hash)
window.protractor.logs.push('// URL: ' + window.location.hash);
url = window.location.hash;
window.protractor.logs.push(action);
};
return window.protractor.logs;
})();