var http = require('http');
var q = require('q');
var request = require('request');

describe('HTTP', function() {

  var encode = function(command) {
    return 'http://localhost:13000/testSelector?popupInput=' +
        encodeURIComponent(command);
  };

  var callElementor = function(command) {
    var deferred = q.defer();

    var url = encode(command);
    request(url, function(error, response, body) {
      if (error) {
        console.log("Got error: " + error);
        return deferred.reject(error);
      }
      deferred.resolve(JSON.parse(body));
    });

    return deferred.promise;
  };

  it('should navigate to protractor website', function(done) {
    var url = 'browser.get(\'http://angular.github.io/protractor/#/api\')';

    // Given that you navigate to the protractor website.
    callElementor(url).then(function() {
      // When you get the current URL.
      return callElementor('browser.getCurrentUrl()');
    }).then(function(response) {
      // Then ensure the URL has changed.
      expect(response).toEqual({
        results: {
          'browser.getCurrentUrl()': 'http://angular.github.io/protractor/#/api'
        }
      });
      done();
    });
  });

  it('should transform by input into count expression', function(done) {
    // When you select by css.
    callElementor('by.css(\'#title\')').then(function(response) {
      // Then ensure the input is turned into count expression.
      expect(response).toEqual({
        results: {
          'element.all(by.css(\'#title\')).count()': 1
        }
      });
      done();
    });
  });

  it('should get element text', function(done) {
    // When you get an element's text.
    callElementor('element.all(by.css(\'.navbar li\')).first().getText()')
        .then(function(response) {
          expect(response).toEqual({
            results: {
              'element.all(by.css(\'.navbar li\')).first().getText()': 'Home'
            }
          });
          done();
        });
  });
});

@@ -1673,30 +1673,30 @@
 xt.%0A    
-callElementor(
+var command = 
 'element
@@ -1746,18 +1746,36 @@
 t()'
-)
+;
 %0A    
-    
+callElementor(command)
 .the
@@ -1793,28 +1793,24 @@
 response) %7B%0A
-    
       expect
@@ -1834,28 +1834,24 @@
 l(%7B%0A        
-    
 results: %7B%0A 
@@ -1855,28 +1855,24 @@
 %7B%0A          
- 
-   
 'element.all
@@ -1931,26 +1931,18 @@
         
-    %7D%0A    
+%7D%0A
       %7D)
@@ -1949,28 +1949,24 @@
 ;%0A      
-    
 done();%0A
         
@@ -1957,20 +1957,16 @@
 done();%0A
-    
     %7D);%0A

reformat code