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 ?
-
2What is a base of your claim that we cannot use WebDriver for Angular pages? Do you have hands-on experience writing WebDriver tests? Did you looked at the Angular pages and tried to write plain webdriver test for them? Or is your interest academic? What is your skill level? I certainly did wrote webdriver test for Angular pages. "Matter of simple programming" (TM) :-)– Peter M. - stands for MonicaCommented May 4, 2016 at 19:33
-
Then why is Protractor and WebdriverJS made if we can use selenium for AngularJS pages ?– SujayCommented May 5, 2016 at 3:41
-
3Protractor is a framework, so you have an easier time automating AngularJS pages. But should work with Selenium as well.– FDMCommented May 5, 2016 at 9:01
-
Huh? Why can't we use selenium to test angularJS applications?– RobbenCommented May 6, 2016 at 16:48
-
2Angular pages ARE right pain in the proverbial to test with WebDriver because it produces such messy markup, avoids ID and name attributes necessary for clean page objects, and creating reliable and robust locators.– Martin SpamerCommented Jun 29, 2018 at 5:01
8 Answers
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.
</edit>
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. :-/
-
1Python also has some weird quirks if you ask me as do most other languages. See wiki.theory.org/YourLanguageSucks I really don't understand why you need to bash JavaScript here, while the question is not even about programming languages. Also JavaScript is pretty elegant if you understand how to use it for your use case. I think Protractor and Nightwatch.js exist because some developer do want to write e2e tests in JavaScript and not in Java or Python for that matter. In this case the developers at Google. Commented Jul 4, 2016 at 22:41
-
3I think I did mentioned benefits of using JS and Protractor, not sure how you missed them. :-) I got burned by Perl, so any language with quirky autovivification (including JS) is suspicious in my opinion. Obviously, you did not get burned by JS yet. Also, average developer has mastery of just few languages, and uses those as preferred tool. Mastering JS is NOT easy. I mentioned one reason - variable autovivification. Another one is: JS is not object-oriented, but prototype-based, which creates different set of subtle quirks. But all power to those who love it. Commented Jul 5, 2016 at 14:42
-
For the record, JavaScript is not only better object-oriented but far better functional language compared to its dynamic siblings.The only downside is to see the real hidden beauty first you have to pass somewhere beginner to middle level in JavaScript but IMHO that is far better rewarding ultimately if you are ever going to do more than a click of a button. Commented Apr 28, 2018 at 0:55
Yes, it is possible but highly inefficient with full of hard coded waits!
Which is better, a hammer or a screwdriver on a screw?
One may always use a hammer on a nail but if you are working on a screw then it is better to use a screwdriver if you have one(Protractor!).
I have used both selenium & Protractor to test angular sites and from last couple of years using Protractor extensively to design large scale test automation for Angular/AngualrJS frontend applications.
Based on my experience, using selenium instead Protractor is like Reinventing the wheel, either in an efficient way what Protractor does(which will take a lot of effort, see below) or by the hardcoded waits all over the place which is NOT a professionally mature scale-able efficient solution.
What Protractor does(Over Selenium)
Both Angular and AngularJS when run asynchronous operations expose the special undocumented testability API, which Protractor uses when it wants to synchronize. This API allows one to provide a callback function which is going to be called once all asynchronous operations are completed.
Protractor injects a few functions on the page. One of those is waitForAngular(rootSelector, callback) function. Protractor calls this function every time it wants to synchronize.
This function serves as a synchronization facade – it expects the caller to provide a callback function, which is going to be called once Angular says that there are no asynchronous operations running. Different versions of Angular provide different testability API, so waitForAngular() has to know how to work with all of them.
Conclusion
After considering all this, IMHO using Protractor over Selenium to test angular applications is no brainer.
My guess will be that the creators of Protractor (which is the same team as AngularJS) just didn't want to switch languages for E2E testing, so they made effort to bring Selenium WebDriver to NodeJS.
-
3Actually Protactor is using webdriverjs to bring Selenium to the NodeJS world. WebDriverJS is from the SeleniumHQ team. Protractor is just build to make testing Angular applications easier. Commented Jul 4, 2016 at 22:22
It seems to be best practice to make the automated tests at the GUI level in the same language as the program under test is written in. Or at any level actually, it also applies at the API, business logic, data store, and whatever other layers have been made across the stack.
It just helps in having a shared responsibility. No dev would like their build fail and not being able to comprehend what was going wrong. That and less excuses being made not to maintain the tests because "no one knows the language they are written in".
Selenium WebDriver is a tool/library that enables automation, whereas Protractor is a Test Runner/Test Framework which defines the design/structure of the automation.
If your tests are flaky, it might need a deeper analysis to determine the cause of flakiness. I've been automating AngularJS applications using SerenityBDD and Selenium WebDriver for almost over 3 years now. I run ~2000 test cases on 5 VMs using Selenium Grid which are integrated with CI tool. Flakiness on the development branch is high as we test under development features, whereas on the production branch is extremely low as the build is stable.
Selenium WebDriver might have sync issues with the AngularJS application, but that should not limit/restrict us from functional testing. Flaky tests can be fixed by introducing custom waits, catching known exceptions, soft assertions, and retrying failed test steps a couple of extra times to ensure sync.
-
1Protractor is not a test runner, neither a test framework. Commented Apr 28, 2018 at 0:31
-
I have similar experience. Adding 50ms wait in a right place can turn flaky test to a stable test. Ugly hack but works. I believe it is a browser automation issue, not a Selenium issue. Commented Jun 15, 2018 at 14:09
You can just use plain Selenium for testing Angular applications. Protractor just makes it easier to test Angular application. Its a framework for testing Angular applications.
It has Automatic Waiting for elements and you can access specific Angular elements by.model and by.binding. Also, it implements the Page Objects in the framework making tests more maintainable if used correctly. Probably there are some more enhancements not listed on the front-page of Protractor.
I've been successfully automating angularjs e2e tests with selenium\watir\ruby as my tech stack for a few years now. My first attempt at using protractor\chai\jasmine in 2014 was a train wreck. The stack and angularjs were in their infancy and my team's time was filled with entering bugs and waiting for pull requests. Out of necessity I switched everyone over to the selenium\watir\ruby and finished the project on time. From what I've read on boards like this, Protractor has matured in the 5 plus years since I've last touched it, that said a need hasn't presented itself requiring me to revisit protractor. The next project is Angular 6 and I intend to use selenium\watir\ruby for that as well. If I have to switch I'll post it here :-)
-
And you didn't face any significant sync issues??? Commented Mar 19, 2019 at 10:11
I have a website made in Angular 2 and i'm starting to think that Selenium can not be dependable upon when testing. I have tests that pass one minute and fail another without any change in the code. When it comes to Protractor, with Angular 2, it doesnt deal with the issue of Promises. So it's not dependable as well.
-
1I disagree. See @ZeeshanSiddiqui answer. Adding 50ms wait in a right place can turn flaky test to a stable test. I believe that flakiness is a browser automation issue, not a Selenium issue. Commented Jun 15, 2018 at 14:11