Visualize / Show mouse cursor position in selenium 2 tests (for example PHPUnit Webdriver)

前端 未结 4 1811
悲哀的现实
悲哀的现实 2021-01-01 04:50

I am running \\PHPUnit_Extensions_Selenium2TestCase to run tests which move the mouse and make clicks ($this->moveto). For improved debugging an

相关标签:
4条回答
  • 2021-01-01 05:10

    Execute a javascript after each page load which enabled the mouse cursor display.

        /**
         * 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()));
        }
    

    This depends on JQuery beeing loaded in the target page - another solution , without the fancy animation, is also possible.

    0 讨论(0)
  • 2021-01-01 05:14

    Taking Alex's anwser I found it easier to put his Javascipt straight into my page. Here's the code I ended up with. Thanks for your help.

    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);
    	});
    };

    0 讨论(0)
  • 2021-01-01 05:22

    Here is @JordanW's answer but without JQuery:

    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; left:0; top:0');
      document.body.appendChild(seleniumFollowerImg);
      document.onmousemove = function (e) {
        document.getElementById("selenium_mouse_follower").style.left = e.pageX;
        document.getElementById("selenium_mouse_follower").style.top = e.pageY;
      };
    };
    
    enableCursor();

    Disclaimer: Only tested on Chrome

    0 讨论(0)
  • 2021-01-01 05:25

    Thanks for sharing it folks! Here is the example to make it work in NodeJS:

    Add showCursor.js file into commands directory in your nightwatch subtree and fill it with the following code snippet

    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';
        };
      }, []);
    };
    

    And right after that you can access it everywhere you have browser variable, i.e. in your test case:

    // Visit some page ...
    browser.showCursor();
    ...
    
    
    0 讨论(0)
提交回复
热议问题