简体   繁体   中英

Wait for Websocket Rails event to finish in test environment

I am using the Websocket Rails gem in my Rails application to send an event that triggers a refresh of part of the page. All is working fine, but in my testing (I am using Rspec + Capybara + Selenium) I can't seem to find a way to wait for the page to respond to the Websocket event. I am triggering the event in my test with:

WebsocketRails[@michael.id].trigger "refresh_task", {task: @michaels_task, modified_by: @archer}

The event is bound as per the below. The ajax call returns a script that is actually another ajax call.

channel.bind('refresh_task', function(task_user) {
        var task = task_user["task"];
        var modified_by = task_user["modified_by"];
        if ($('span[data-mahi-task-id="' + task.id +'"]').length > 0) {//if the task is currently visible
            if ($('span[data-mahi-task-id="' + task.id +'"] div.modal').length > 0 ) { //the edit is open, so prompt the user 
                refresh_task = confirm(modified_by.name + " has modified this task, do you want to re-load? (if you don't re-load you risk overwriting their changes)");
            } else {
                refresh_task = true;
            }
            if (refresh_task == true) {
                //hide the modal if it exists
                $('span[data-mahi-task-id="' + task.id +'"] div.modal').modal('hide');

                //grey out the task while we load => this will get removed in ready.js
                $('span[data-mahi-task-id="' + task.id +'"]').addClass('ajax-disabled');
                // fake a "cancel" button on the update user task which will trigger a re-load of the task
                $.ajax({
                  url: "/users/" + task.created_by_id + "/tasks/" + task.id,
                  data: {task: task, update_details_cancel_button: "", _method:'put'}, //need to teel rails this is a put - not all browsers support put
                  dataType: 'script', //what format we expect the response to be - i.e. we want to execupte the resulting script
                  complete : ajaxComplete(task,modified_by),
                  method: 'POST' //not all browsers support put so send a POST with the data _method:'put' as above
                });
            }
        }
    });

Once the ajax starts I can tell its running and wait for it to finish, but I need my test to wait between triggering the event and starting the ajax somehow?

I solved this by writing a helper method that accepts a block of code. The helper method records the current time, then adds a function to the ajax start event that records the time that the ajax started in a sessionStorage variable. I can then run the code block passed to the method and wait until the ajax starts as follows:

def wait_for_ajax_to_start
  start_time = Time.now.to_f * 1000 #number of milliseconds
  #attach a hander on ajaxstart to store the time ajax started in a sessionStorage variable called 'ajaxStarted'
  page.evaluate_script('$(document).ajaxStart(function() {sessionStorage.ajaxStarted = $.now()})')

  #run the code block passed to this method
  yield

  #wait until the ajax has started or timeout
  Timeout.timeout(Capybara.default_wait_time) do #timeout after the default wait time
    loop until (page.evaluate_script('sessionStorage.ajaxStarted') != nil) && (page.evaluate_script('sessionStorage.ajaxStarted').to_i >= start_time)
  end
end

I call the helper method like this:

wait_for_ajax_to_start {
  WebsocketRails[@michael.id].trigger "refresh_task", {task: @michaels_task, modified_by: @archer}  
}

This seems to work for me so far....

您的测试应等待刷新导致页面上的可见更改

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM