@@ -86,6 +86,7 @@ const WebSocket = require('ws')
8686const cdp = require ( './devtools/CDPConnection' )
8787const remote = require ( './remote' )
8888const cdpTargets = [ 'page' , 'browser' ]
89+ const fs = require ( 'fs' )
8990
9091/**
9192 * Custom command names supported by Chromium WebDriver.
@@ -775,7 +776,7 @@ class Driver extends webdriver.WebDriver {
775776 * @param connection CDP Connection
776777 */
777778 async register ( username , password , connection ) {
778- await connection . execute ( "Network.setCacheDisabled" , 1 , {
779+ await connection . execute ( "Network.setCacheDisabled" , this . getRandomNumber ( 1 , 10 ) , {
779780 cacheDisabled : true ,
780781 } , null ) ;
781782
@@ -784,7 +785,7 @@ class Driver extends webdriver.WebDriver {
784785
785786 if ( params . method === 'Fetch.authRequired' ) {
786787 const requestParams = params [ 'params' ]
787- connection . execute ( 'Fetch.continueWithAuth' , 1 , {
788+ connection . execute ( 'Fetch.continueWithAuth' , this . getRandomNumber ( 1 , 10 ) , {
788789 requestId : requestParams [ 'requestId' ] ,
789790 authChallengeResponse : {
790791 response : 'ProvideCredentials' ,
@@ -793,7 +794,7 @@ class Driver extends webdriver.WebDriver {
793794 } } )
794795 } else if ( params . method === 'Fetch.requestPaused' ) {
795796 const requestPausedParams = params [ 'params' ]
796- connection . execute ( 'Fetch.continueRequest' , 1 , {
797+ connection . execute ( 'Fetch.continueRequest' , this . getRandomNumber ( 1 , 10 ) , {
797798 requestId : requestPausedParams [ 'requestId' ] ,
798799 } )
799800 }
@@ -811,7 +812,7 @@ class Driver extends webdriver.WebDriver {
811812 * @returns {Promise<void> }
812813 */
813814 async onLogEvent ( connection , callback ) {
814- await connection . execute ( 'Runtime.enable' , 1 , { } , null )
815+ await connection . execute ( 'Runtime.enable' , this . getRandomNumber ( 1 , 10 ) , { } , null )
815816
816817 this . _wsConnection . on ( 'message' , ( message ) => {
817818 const params = JSON . parse ( message )
@@ -854,7 +855,7 @@ class Driver extends webdriver.WebDriver {
854855 * @returns {Promise<void> }
855856 */
856857 async onLogException ( connection , callback ) {
857- await connection . execute ( 'Runtime.enable' , 1 , { } , null )
858+ await connection . execute ( 'Runtime.enable' , this . getRandomNumber ( 1 , 10 ) , { } , null )
858859
859860 this . _wsConnection . on ( 'message' , ( message ) => {
860861 const params = JSON . parse ( message )
@@ -870,6 +871,49 @@ class Driver extends webdriver.WebDriver {
870871 }
871872 } )
872873 }
874+
875+ /**
876+ * @param connection
877+ * @param callback
878+ * @returns {Promise<void> }
879+ */
880+ async logMutationEvents ( connection , callback ) {
881+ await connection . execute ( 'Runtime.enable' , this . getRandomNumber ( 1 , 10 ) , { } , null )
882+ await connection . execute ( 'Page.enable' , this . getRandomNumber ( 1 , 10 ) , { } , null )
883+
884+ await connection . execute ( 'Runtime.addBinding' , this . getRandomNumber ( 1 , 10 ) , {
885+ name : '__webdriver_attribute' ,
886+ } , null )
887+
888+ const mutationListener = fs . readFileSync ( '../../cdp-support/mutation-listener.js' , 'utf-8' ) . toString ( )
889+
890+ this . executeScript ( mutationListener )
891+
892+ await connection . execute ( 'Page.addScriptToEvaluateOnNewDocument' , this . getRandomNumber ( 1 , 10 ) , {
893+ source : mutationListener ,
894+ } , null )
895+
896+ this . _wsConnection . on ( 'message' , async ( message ) => {
897+ const params = JSON . parse ( message )
898+ if ( params . method === 'Runtime.bindingCalled' ) {
899+ let payload = JSON . parse ( params [ 'params' ] [ 'payload' ] )
900+ let elements = await this . findElements ( { css : "*[data-__webdriver_id=" + payload [ 'target' ] } )
901+
902+ if ( elements . length === 0 ) {
903+ return
904+ }
905+
906+ let event = {
907+ element : elements [ 0 ] ,
908+ attribute_name : payload [ 'name' ] ,
909+ current_value : payload [ 'value' ] ,
910+ old_value : payload [ 'oldValue' ]
911+ }
912+ callback ( event )
913+ }
914+ } )
915+ }
916+
873917 /**
874918 * Sends a DevTools command to change the browser's download directory.
875919 *
@@ -967,6 +1011,10 @@ class Driver extends webdriver.WebDriver {
9671011 'Driver.stopCasting(' + deviceName + ')'
9681012 )
9691013 }
1014+
1015+ getRandomNumber ( min , max ) {
1016+ return Math . floor ( Math . random ( ) * ( max - min + 1 ) + min )
1017+ }
9701018}
9711019
9721020// PUBLIC API
0 commit comments