newspaint

Documenting Problems That Were Difficult To Find The Answer To

JavaScript Function to Call Functions In Sequence

This post is targeted towards PhantomJS where you may want to simulate a user entering data into several fields using the keyboard but pausing between each field entry.

The following function takes a list of functions and executes them with a fixed delay between each event. It takes two parameters:

  • events – array of functions to call sequentially
  • delay – delay, in milliseconds, to pause before calling each event
function space_out_events( events, delay, index ) {
    index = ( typeof( index ) !== 'undefined' ) ? index : 0;

    window.setTimeout(
        function () {
            events[index]();
            if ( index < ( events.length - 1 ) ) {
                space_out_events( events, delay, index + 1 );
            }
        }, delay
    );
}

Example Usage

You have a form on a page to fill out. You want to simulate all the information entered by keyboard with a pause of half a second (500ms), then tab key, then pause another 500ms, before filling out the next field.

    space_out_events(
        [
            function(){ page.sendEvent( 'keypress', config.email ); },
            function(){ page.sendEvent( 'keypress', page.event.key.Tab ); },
            function(){ page.sendEvent( 'keypress', "Testing123" ); },
            function(){ page.sendEvent( 'keypress', page.event.key.Tab ); },
            function(){ page.sendEvent( 'keypress', "Testing123" ); },
            function(){ page.sendEvent( 'keypress', page.event.key.Tab ); },
            // ignore title
            function(){ page.sendEvent( 'keypress', page.event.key.Tab ); },
            function(){ page.sendEvent( 'keypress', "John" ); },
            function(){ page.sendEvent( 'keypress', page.event.key.Tab ); },
            function(){ page.sendEvent( 'keypress', "Smith" );},
            function(){ page.sendEvent( 'keypress', page.event.key.Tab ); },
            // ignore mobile number
            function(){ page.sendEvent( 'keypress', page.event.key.Tab ); },
            // ignore day of birth
            function(){ page.sendEvent( 'keypress', page.event.key.Tab ); },
            // ignore month of birth
            function(){ page.sendEvent( 'keypress', page.event.key.Tab ); },
            // ignore year of birth
            function(){ page.sendEvent( 'keypress', page.event.key.Tab ); },
            // ignore screen name
            function(){ page.sendEvent( 'keypress', page.event.key.Tab ); },
            function(){ if ( config.debug ) { page.render( "filled_form.png" ); } },
            function(){ do_click_next_step( page ); }
        ], 500
    );

The technique could also be used with Node.JS.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: