title: Is there something like jQuery.active in angularjs?
body: <p>I am using selenium to test my application.  I have lots of ajax calls that use $resource or $http.  It would be nice if there was a way to poll any active ajax requests in angular so that selenium could wait until those requests were completed.</p>
  
  <p>I supposed I could place an element on the page (for selenium to look for) and hook it up to some flag that gets set on success but that could get pretty messy.</p>
  
  <p>There is a very effective way to do this when using jQuery as described <a href="https://stackoverflow.com/questions/6504402/selenium-ajax-testing">here.</a></p>
  
  <p>Or does selenium have a way to do this that I haven't found?</p>
  
  <p>Couldn't find anything in the docs?  Any suggestions? Thanks.</p>
  
  <p><strong>EDIT:</strong>
  Caleb Boyd's answer is correct and a great solution to the problem of detecting angular ajax calls while using selenium web driver.  Here is quick implementation of how I use this.  I actually used a variation of caleb's code from this <a href="https://stackoverflow.com/questions/20062493/interceptor-for-all-http-requests-in-angularjs-1-0-x">link</a>, which included ajax errors.  It is however, essentially the same thing.  Thanks Caleb.</p>
  
  <p>Add this script and element to the bottom of your page.  Just remove before deployment:</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;&lt;!--My Angular Scripts--&gt;&lt;/head&gt;
  &lt;body ng-app="MyApp"&gt;
  &lt;!--Your Html --&gt;
  &lt;script&gt;
              MyApp.config(function($httpProvider) {
                  $httpProvider.interceptors.push('requestInterceptor');
              })
              .factory('requestInterceptor', function($q, $rootScope) {
                  $rootScope.pendingRequests = 0;
                  return {
                      'request': function(config) {
                          $rootScope.pendingRequests++;
                          return config || $q.when(config);
                      },
                      'requestError': function(rejection) {
                          $rootScope.pendingRequests--;
                          return $q.reject(rejection);
                      },
                      'response': function(response) {
                          $rootScope.pendingRequests--;
                          return response || $q.when(response);
                      },
                      'responseError': function(rejection) {
                          $rootScope.pendingRequests--;
                          return $q.reject(rejection);
                      }
                  };
              });
      &lt;/script&gt;
      &lt;span id="http-status"&gt;{{pendingRequests}}&lt;/span&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I use NUnit for my testing framework.</p>
  
  <pre><code>[TestFixture]
  public class MyTestClass
  {
    [Setup}
    public void Setup()
    {
      _webDriver = new ChromeDriver(@"...path to chromedriver.exe")
      //any other properties you need
     }
  
     [TearDown]
     public void TearDown()
     {
       if(_webDriver == null)
          return;
       _webDriver.Quit();
       _webDriver.Dispose();
      }
  
      [Test]
      public void Test_my_page_functionality()
      {
        var pageBtn = _webDriver.FindElement(By.Id("my-btn-id"));
        pageBtn.Click();
        _webDriver.WaitForAjax();//see extension below
        //test whatever result you want after ajax request has come back
       }
  }
  </code></pre>
  
  <p>Here is the WaitForAjax Extension</p>
  
  <pre><code>public static class WebDriverExtensions
  {
    public static void WaitForAjax(this IWebDriver webDriver)
          {
              while (true)
              {
                  //Note: FindElement is another extension that uses a timer to look for an element
                  //It is NOT the one that selenium uses - the selenium extension throws exceptions on a null element
                  var ajaxIsComplete = webDriver.FindElement(By.Id("http-status"), 5);
                  if (ajaxIsComplete != null &amp;&amp; ajaxIsComplete.Text.Equals("0"))
                  {
                      //optional wait for angularjs digest or compile if data is returned in ajax call
                      Thread.Sleep(1000);
                      break;
                  }
                  Thread.Sleep(100);
              }
          }
  }
  </code></pre>
  
  <p>Try testing the WaitForAjax extension by putting a Thread.Sleep(5000) at the bottom of your controller method.
  Hope this helps someone.  Thanks again Caleb.</p>