在selenium 2测试中可视化/显示鼠标光标位置(例如PHPUnit Webdriver)

时间:2016-03-08 12:45:10

标签: selenium selenium-webdriver mouse

我正在运行\PHPUnit_Extensions_Selenium2TestCase来运行移动鼠标并进行点击($this->moveto)的测试。为了改进调试和查看体验,我想看看鼠标光标当前在哪里。使用上述方法不会移动系统鼠标光标。

4 个答案:

答案 0 :(得分:3)

每次加载页面后执行javascript,启用鼠标光标显示。

    /**
     * Enable mouse cursor display
     */
    protected function enableCursor()
    {
        $this->execute(array('script' => <<<EOF
        var seleniumFollowerImg=document.createElement("img");
        seleniumFollowerImg.setAttribute('src', 'data:image/png;base64,'
            + 'iVBORw0KGgoAAAANSUhEUgAAABQAAAAeCAQAAACGG/bgAAAAAmJLR0QA/4ePzL8AAAAJcEhZcwAA'
            + 'HsYAAB7GAZEt8iwAAAAHdElNRQfgAwgMIwdxU/i7AAABZklEQVQ4y43TsU4UURSH8W+XmYwkS2I0'
            + '9CRKpKGhsvIJjG9giQmliHFZlkUIGnEF7KTiCagpsYHWhoTQaiUUxLixYZb5KAAZZhbunu7O/PKf'
            + 'e+fcA+/pqwb4DuximEqXhT4iI8dMpBWEsWsuGYdpZFttiLSSgTvhZ1W/SvfO1CvYdV1kPghV68a3'
            + '0zzUWZH5pBqEui7dnqlFmLoq0gxC1XfGZdoLal2kea8ahLoqKXNAJQBT2yJzwUTVt0bS6ANqy1ga'
            + 'VCEq/oVTtjji4hQVhhnlYBH4WIJV9vlkXLm+10R8oJb79Jl1j9UdazJRGpkrmNkSF9SOz2T71s7M'
            + 'SIfD2lmmfjGSRz3hK8l4w1P+bah/HJLN0sys2JSMZQB+jKo6KSc8vLlLn5ikzF4268Wg2+pPOWW6'
            + 'ONcpr3PrXy9VfS473M/D7H+TLmrqsXtOGctvxvMv2oVNP+Av0uHbzbxyJaywyUjx8TlnPY2YxqkD'
            + 'dAAAAABJRU5ErkJggg==');
        seleniumFollowerImg.setAttribute('id', 'selenium_mouse_follower');
        seleniumFollowerImg.setAttribute('style', 'position: absolute; z-index: 99999999999; pointer-events: none;');
        document.body.appendChild(seleniumFollowerImg);
jQuery(document).mousemove(function(e){
    jQuery("#selenium_mouse_follower").stop().animate({left:e.pageX, top:e.pageY});
});
EOF
        , 'args' => array()));
    }

这取决于在目标页面中加载的JQuery beeing - 另一种解决方案,没有花哨的动画,也是可能的。

答案 1 :(得分:2)

这是@JordanW的答案,但没有JQuery:

dicta = {i.split('=')[0]: i.split('=')[1] for i in s.split()}

免责声明:仅在Chrome上进行了测试

答案 2 :(得分:1)

以亚历克斯的为例,我发现将Javascipt直接放入我的页面更容易。这是我最终得到的代码。谢谢你的帮助。

&#13;
&#13;
function enableCursor()
{
	var seleniumFollowerImg=document.createElement("img");
	seleniumFollowerImg.setAttribute('src', 'data:image/png;base64,'
		+ 'iVBORw0KGgoAAAANSUhEUgAAABQAAAAeCAQAAACGG/bgAAAAAmJLR0QA/4ePzL8AAAAJcEhZcwAA'
		+ 'HsYAAB7GAZEt8iwAAAAHdElNRQfgAwgMIwdxU/i7AAABZklEQVQ4y43TsU4UURSH8W+XmYwkS2I0'
		+ '9CRKpKGhsvIJjG9giQmliHFZlkUIGnEF7KTiCagpsYHWhoTQaiUUxLixYZb5KAAZZhbunu7O/PKf'
		+ 'e+fcA+/pqwb4DuximEqXhT4iI8dMpBWEsWsuGYdpZFttiLSSgTvhZ1W/SvfO1CvYdV1kPghV68a3'
		+ '0zzUWZH5pBqEui7dnqlFmLoq0gxC1XfGZdoLal2kea8ahLoqKXNAJQBT2yJzwUTVt0bS6ANqy1ga'
		+ 'VCEq/oVTtjji4hQVhhnlYBH4WIJV9vlkXLm+10R8oJb79Jl1j9UdazJRGpkrmNkSF9SOz2T71s7M'
		+ 'SIfD2lmmfjGSRz3hK8l4w1P+bah/HJLN0sys2JSMZQB+jKo6KSc8vLlLn5ikzF4268Wg2+pPOWW6'
		+ 'ONcpr3PrXy9VfS473M/D7H+TLmrqsXtOGctvxvMv2oVNP+Av0uHbzbxyJaywyUjx8TlnPY2YxqkD'
		+ 'dAAAAABJRU5ErkJggg==');
	seleniumFollowerImg.setAttribute('id', 'selenium_mouse_follower');
	seleniumFollowerImg.setAttribute('style', 'position: absolute; z-index: 99999999999; pointer-events: none;');
	document.body.appendChild(seleniumFollowerImg);
	jQuery(document).mousemove(function(e){
		jQuery("#selenium_mouse_follower").css('left', e.pageX);
		jQuery("#selenium_mouse_follower").css('top', e.pageY);
	});
};
&#13;
&#13;
&#13;

答案 3 :(得分:0)

感谢与大家分享!这是使其在NodeJS中运行的示例:

ps -ef | grep python文件添加到您的守夜子树的showCursor.js目录中,并使用以下代码段填充该文件

commands

此后,您可以在拥有exports.command = function() { return this.execute(function() { const mouse = document.createElement('img'); mouse.setAttribute( 'src', 'data:image/png;base64,' + 'iVBORw0KGgoAAAANSUhEUgAAABQAAAAeCAQAAACGG/bgAAAAAmJLR0QA/4ePzL8AAAAJcEhZcwAA' + 'HsYAAB7GAZEt8iwAAAAHdElNRQfgAwgMIwdxU/i7AAABZklEQVQ4y43TsU4UURSH8W+XmYwkS2I0' + '9CRKpKGhsvIJjG9giQmliHFZlkUIGnEF7KTiCagpsYHWhoTQaiUUxLixYZb5KAAZZhbunu7O/PKf' + 'e+fcA+/pqwb4DuximEqXhT4iI8dMpBWEsWsuGYdpZFttiLSSgTvhZ1W/SvfO1CvYdV1kPghV68a3' + '0zzUWZH5pBqEui7dnqlFmLoq0gxC1XfGZdoLal2kea8ahLoqKXNAJQBT2yJzwUTVt0bS6ANqy1ga' + 'VCEq/oVTtjji4hQVhhnlYBH4WIJV9vlkXLm+10R8oJb79Jl1j9UdazJRGpkrmNkSF9SOz2T71s7M' + 'SIfD2lmmfjGSRz3hK8l4w1P+bah/HJLN0sys2JSMZQB+jKo6KSc8vLlLn5ikzF4268Wg2+pPOWW6' + 'ONcpr3PrXy9VfS473M/D7H+TLmrqsXtOGctvxvMv2oVNP+Av0uHbzbxyJaywyUjx8TlnPY2YxqkD' + 'dAAAAABJRU5ErkJggg==' ); mouse.setAttribute('id', 'selenium-mouse'); mouse.setAttribute( 'style', 'position: absolute; z-index: 999999; pointer-events: none; left:0; top:0' ); document.body.appendChild(mouse); document.onmousemove = function(e) { document.getElementById('selenium-mouse').style.left = e.pageX + 'px'; document.getElementById('selenium-mouse').style.top = e.pageY + 'px'; }; }, []); }; 变量的任何地方(即在您的测试用例中)访问它:

browser