Created
September 23, 2019 08:22
-
-
Save miller45/c187ea399317cde2320736f4c80a9ba8 to your computer and use it in GitHub Desktop.
mouse plugin for protractor 5.4.x
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//copied but modernized from https://gist.github.com/OrganicPanda/ba22c6dec89ffa7673a27ec28e08aa89 | |
// Hook in to `addEventListener` to track the mouse and display it as a circle | |
var MousePlugin = function () { | |
}; | |
MousePlugin.prototype.onPageLoad = function () { | |
console.log("mousePlugin"); | |
return browser.executeScript(function () { | |
(function () { | |
var EventSniffer = function () { | |
this.history = []; | |
this.callbacks = {}; | |
this.minCacheSize = 100; | |
this.maxCacheSize = 500; | |
}; | |
EventSniffer.prototype.handle = function (name, e) { | |
if (this.history.indexOf(e) > -1) return; | |
this.addToHistory(e); | |
this.trigger(name, e); | |
}; | |
EventSniffer.prototype.trigger = function (name, e) { | |
if (!this.callbacks[name]) return; | |
this.callbacks[name].forEach(function (cb) { | |
cb(e); | |
}); | |
}; | |
EventSniffer.prototype.addToHistory = function (e) { | |
if (this.history.length >= this.maxCacheSize) { | |
this.history = this.history | |
.slice(this.history.length - this.minCacheSize); | |
} | |
this.history.push(e); | |
}; | |
EventSniffer.prototype.on = function (name, cb) { | |
if (!this.callbacks[name]) { | |
this.callbacks[name] = []; | |
// Add a dummy event listener incase the page hasn't | |
document.addEventListener(name, function () { | |
}); | |
} | |
this.callbacks[name].push(cb); | |
}; | |
EventSniffer.prototype.install = function () { | |
var proto = EventTarget.prototype; | |
var oldAEL = proto.addEventListener; | |
var self = this; | |
proto.addEventListener = function (name, listener, options) { | |
// Add our own event listener first | |
oldAEL.call(this, name, function (e) { | |
self.handle(name, e); | |
}, options); | |
// The add the users listener as normal | |
return oldAEL.apply(this, [name, listener, options]); | |
}; | |
} | |
var MouseTracker = function () { | |
this.indicator = document.createElement('div'); | |
this.indicator.setAttribute('class', 'mouse-tracker'); | |
this.style = document.createElement('style'); | |
this.style.innerHTML = [ | |
'.mouse-tracker {', | |
'width: 0.5em;', | |
'height: 0.5em;', | |
'background: orange;', | |
'box-shadow: 0 0 0 1px white;', | |
'border-radius: 50%;', | |
'position: absolute;', | |
'top: 0;', | |
'left: 0;', | |
'z-index: 100000;', | |
'pointer-events: none;', | |
'transform: translate(-50%, -50%);', | |
'transition: background-color 0.2s linear;', | |
'}', | |
'.mouse-tracker.mousedown {', | |
'background: rgba(0, 128, 0, 0.5);', | |
'}', | |
'@keyframes mouse-tracker-click {', | |
'to {', | |
'width: 5em;', | |
'height: 5em;', | |
'opacity: 0;', | |
'}', | |
'}', | |
'.mouse-tracker .click {', | |
'width: 0.5em;', | |
'height: 0.5em;', | |
'border: 1px solid rgba(128, 128, 128, 1);', | |
'box-shadow: 0 0 0 1px rgba(256, 256, 256, 1);', | |
'border-radius: 50%;', | |
'position: absolute;', | |
'top: 50%;', | |
'left: 50%;', | |
'pointer-events: none;', | |
'transform: translate(-50%, -50%);', | |
'animation: 1s mouse-tracker-click;', | |
'}' | |
].join('\n'); | |
}; | |
MouseTracker.prototype.move = function (x, y) { | |
this.indicator.style.left = x + 'px'; | |
this.indicator.style.top = y + 'px'; | |
}; | |
MouseTracker.prototype.click = function () { | |
var click = document.createElement('div'); | |
click.setAttribute('class', 'click'); | |
click.addEventListener('animationend', function () { | |
click.parentElement.removeChild(click); | |
}, false); | |
this.indicator.appendChild(click); | |
}; | |
MouseTracker.prototype.mousedown = function () { | |
this.indicator.classList.add('mousedown'); | |
}; | |
MouseTracker.prototype.mouseup = function () { | |
this.indicator.classList.remove('mousedown'); | |
}; | |
MouseTracker.prototype.install = function () { | |
document.body.appendChild(this.indicator); | |
document.head.appendChild(this.style); | |
} | |
var tracker = new MouseTracker(); | |
var sniffer = new EventSniffer(); | |
sniffer.install(); | |
tracker.install(); | |
sniffer.on('click', function (e) { | |
tracker.click(); | |
}); | |
sniffer.on('mousemove', function (e) { | |
tracker.move(e.x, e.y); | |
}); | |
sniffer.on('mousedown', function (e) { | |
tracker.mousedown(); | |
}); | |
sniffer.on('mouseup', function (e) { | |
tracker.mouseup(); | |
}); | |
})(); | |
}); | |
}; | |
// Slow things down a bit | |
function waitForCondition() { | |
return browser.driver.sleep(500).then(function () { | |
return true; | |
}); | |
} | |
module.exports = new MousePlugin(); | |
module.exports.MousePlugin = MousePlugin; | |
module.exports.waitForCondition = waitForCondition; |
@miller45 Can you please help me in this plugin, I am new to Protractor.
I have created mouse-plugin.js file and mention its path in protractor.conf.js file in exports.config = { ... } section. But it is not working, also not showing any error.
Am I missing anything. Please help.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Include into config like this