Why can't we use Selenium Webdriver to test Angular JS sites?



  • If we look at the Protractor architecture, Protractor uses WebDriver JS to call Selenium Webdriver which ultimately calls browser driver and then browser. Now since Selenium is passing commands to browser driver why can't we just use Selenium to automate Angular pages ?



  • I do use Webdriver to use Angular pages. So it IS possible, and OP's assumption is wrong.

    But it is not easy - not as straightforward as using WebDriver to automate more traditional web pages (which do not use "single page application" concept and DOM manipulation so heavily), where is trivial to add id or name to any element you want.

    Angular uses IDs heavily for interaction and DOM manipulation. As a result, WebDriver tests cannot rely on IDs to locate elements, and locating by name is bit more hassle (non-unique etc). But you can still write tests using WebDriver (that's how Protractor does it after all, under the skin).

    WebDriver tests are Angular-agnostic, they know nothing about Angular and the complicated machinery on the page it uses to manipulate DOM.

    Protractor understands Angular and this allows developers to write Protractor-based tests bit easier - and as always, for the price.

    In this case, price is writing tests in JavaScript, quite a quirky language full of hidden traps, which many prefer to avoid 🙂 For a good summary of the traps, see JavaScript Garden

    I would assume (based on our experience) developers choose to develop low-level tests in Protractor/JavaScript (to use internals of Angular), but e2e tests in some other less quirky language (in our case, Python), to increase productivity and decrease flakiness. Usually, they already have (or better should have) framework/approach to write e2e tests for non-Angular pages, so pure WebDriver tests for Angular pages are consistent with other e2e tests.

    Edit (April 2018): Angular2+ is a significant if sneaky change over AngularJS. Angular2+ does not support some AngularJS-specific locator strategies anymore (and has no plans to implement them), so there is even less incentives to use JS (beyond JS unit-tests of Angular stuff). So when talking/reading about "the Angular" you need to be aware of which version we are talking about, they are significantly different.

    Our current strategy is to implement Angular2+ unit-tests (model, basic manipulation), and also single happy-path UI test in JS. More comprehensive UI tests will be in Python/WebDriver. Yes, it is slight duplication of work (locators are both in JS and Python, but locators are trivial). We prefer Python, have lots of code in it, including testing tools/fixtures, which are easier to call from Python than from JS, so we think it is the best compromise.

    That is why I asked OP about any hands-on experience. For any person with hands-on experience with writing both WebDriver and Protractor tests, it is a matter of taste and preferences.

    If someone loves JavaScript, or JavaScript in main/preferred language for the tested application, there is no reason to avoid Protractor. If you prefer NOT to use JavaScript, you know why you want to avoid it, so you do. 🙂

    ... And I am involved in yet another Holy War, people who do like JavaScript are downvoting this answer because I am not equally in love with in their preferred language. Even if I explained what are the exact reason I decided to skip over Protractor (and missing on the benefit from some of it's good features), while using WebDriver to test Angular pages, exactly as OP asked. I thought that the first paragraph will cover it, but apparently it was not enough. 😕



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2