Testing Angularjs Application with Selenium Testing Angularjs Application with Selenium selenium selenium

Testing Angularjs Application with Selenium


Here are your answers -

  • Yes for AngularJS testing you need to use Protractor as it has inbuilt support to wait for Angular to load.
  • If you use Protractor, then you need to write code in JavaScript and not jquery.
  • If you use Protractor you cannot use Java as Protractor is built on top of Selenium WebDriverJS.

Advantage would be that you can write Javascript code (which is simpler than Java) to test your angular app and you don't have to worry about how the AngularJS works on your page. Only thing that might confuse you is using promises which is taken from WebdriverJS. Hope this helps.


Here are some of the techniques I use to test my AngularJS Applications :

  1. Look into Karma framework. It is very good and has good documentation as well.https://karma-runner.github.io/0.8/plus/AngularJS.html
  2. If you're more into end-to-end testing please use Protractor as it is proven time and time again that it is the best.https://angular.github.io/protractor/#/


I think angular has rendered your element and selenium is finding it, but it is not clickable yet. You can find it out by getting some properties for the element (i.e. text). The problem is with angular not finished with $http calls (and probably the $digest cycle). One think you can do is to wait until angular is done with pending $http requests and then find your desired element and call .Click() on it. Below is a working code snippet I use for our test project in C#.

 new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(angularIsDoneLoading);//Add the rest of your code in here(like finding element, clicking on it etc.)         Func<IWebDriver, bool> angularIsDoneLoading = (IWebDriver drv) =>                {                    string ngFinishedAllRequests =                         @"var injector = window.angular.element('body').injector();                         var $http = injector.get('$http');                         return ($http.pendingRequests.length === 0)";                    return ((IJavaScriptExecutor)drv).ExecuteScript(ngFinishedAllRequests).ToString() == "True";                };