code: function parseResponse({ browser, history, document, response }) {
    const window      = document.defaultView;
    window._request   = response.request;
    window._response  = response;
    history.updateLocation(window, response._url);
  
    const done = window._eventQueue.waitForCompletion();
    response
      ._consume()
      .then(function(body) {
  
        const contentType = response.headers.get('Content-Type') || '';
        const html        = getHTMLFromResponseBody(body, contentType);
        response.body     = html;
        document.write(html || '<html></html>');
        document.close();
  
        browser.emit('loaded', document);
        if (response.status >= 400)
          throw new Error(`Server returned status code ${response.status} from ${response.url}`);
        if (!document.documentElement)
          throw new Error(`Could not parse document at ${response.url}`);
  
      })
      .then(function() {
  
        // Handle meta refresh.  Automatically reloads new location and counts
        // as a redirect.
        //
        // If you need to check the page before refresh takes place, use this:
        //   browser.wait({
        //     function: function() {
        //       return browser.query('meta[http-equiv="refresh"]');
        //     }
        //   });
        const refresh = getMetaRefresh(document);
  
        if (refresh) {
          const { refreshTimeout } = refresh;
          const { refreshURL }     = refresh;
  
          window._eventQueue.setTimeout(function() {
            // Allow completion function to run
            window._eventQueue.enqueue(function() {
              window._eventQueue.enqueue(function() {
                // Count a meta-refresh in the redirects count.
                history.replace(refreshURL);
                // This results in a new window getting loaded
                const newWindow = history.current.window;
                newWindow.addEventListener('load', function() {
                  ++newWindow._request._redirectCount;
                });
              });
            });
          }, refreshTimeout * 1000);
        }
  
      })
      .catch(function(error) {
        browser.emit('error', error);
      })
      .then(done);
  }
docs: Parse HTML response and setup document