Can Selenium web driver be used to automate tests for Angular 4 applications? Or is Protractor a more reliable option?
3 Answers
Selenium can be used to test any web page, so Angular applications can be perfectly automated.
On top of that, you can easily write custom locators so waiting on typical ng
elements is feasible with any Selenium binding.
Especially if your language is C#, Java or Python, Selenium itself is likely the best choice.
Protractor has some good selling points for Angular apps, which you can find in these answers. If you are skilled in Javascript, this is likely the best choice. However, because it's a wrapper on top of Selenium, Protractor is by definition less reliable because, in addition to Selenium bugs, it has defects of its own.
Note the comment of @alexce in the linked topic: if I were to start our test automation efforts from scratch now, I would go for Python/Selenium. Personally, I also prefer a solid self-built framework around Selenium rather than add another third-party layer.
-
Additional point for Selenium Webdriver: Unless your codebase is pure JavaScript, WebDriver is more flexible choice because you can use preferred language. It allows your team to reuse parts of your other code written in your preferred language (which your team is more proficient, avoiding quirks of JavaScript). Can you call Python or C# code from JavaScript? Yes you can, but is it worth the bother? Dec 5, 2017 at 14:18
-
1Also, Webdriver can be used for other tasks beyond automated tests, because it is browser automation W3C standard (reusing the same pageobjects you use for tests): Custom load tests. Automating interaction with third party websites (we need that). Frameworks like jasmine come and go, W3C standards will last. Dec 5, 2017 at 14:21
-
"If you are skilled in Javascript, this is likely the best choice" - this remark made by @FDM I think it summaries pretty well what's important when considering any frameworks built on top of Selenium WebDriver that use JavaScript bindings, regardless if the implementation is more or less opinionated.
I also consider that if the author of this question/topic or his team have a solid experience in another programming language I'd say go with that and build your selenium/automation framework using your favorite language bindings. Why I'm saying this: if you have a good foundation in Java and Selenium, then moving to JavaScript and Node.js for the first time and dealing with promises, control flow and all the specifics of the JS ecosystem will be a pain (at least in the beginning).
I have used Protractor to test different Angular SPAs, including a large suite of tests in which I have disabled the Selenium Promise Manager implementation and used Async/Await - which is way nicer if you understand Promises very well. Also, quite interesting for me, I have used Protractor on a non-Angular e-commerce app (the reason being that I'm so accustomed to Protractor) and never ran into specific problems.
My opinion is that Protractor is a stable and mature framework and if you have a good understanding && experience in JavaScript it's the go-to tool to test an Angular app - unless you prefer to build your own automation framework using the bindings you love, which is perfectly fine.
Here are a few advantages of using Protractor to test an Angular App:
No need to add waits and sleeps: As the webpage finishes pending tasks, Protractor executes the next steps of the testing process by automatically connecting with the AngularJS application
Supports Angular-specific locator strategies including binding, model, repeater as well as native WebDriver locator strategies
One Config file to rule them all: you can set any global variables, integrate any packages and even execute the tests in cloud providers (SauceLabs, BrowserStack)
If you like Classes, as syntax, rather than using the JS Prototypes, using ES6 will be a breeze
The Protractor syntax is somehow shorter (and nicer) -> http://www.protractortest.org/#/webdriver-vs-protractor
It can take the advantage of the Selenium grid to run multiple browsers at once
It can run test on both real and headless browsers
It can use Jasmine or Mocha or Cucumber to write your test
You can integrate all the e2e tests into your /test directory within the application code-base, which is a huge advantage to have the tests in the same repository as the source code of the app resides
It works in conjunction with Selenium to offer an automated test infrastructure and it makes selenium grid to run multiple browsers all at once
Just a note: I'm not advocating for Protractor or so, I just have 2-3 years of experience using JS Selenium frameworks and I consider Protractor being an excellent candidate when it comes to Angular apps in general. But as I said, if you have a solid experience using Python, Java, or anything else, maybe it'll be better to build on top of your experience, rather than switching to a new language/ecosystem where you'll get often stuck, let's say. But if you have some time, take Protractor for a spin.
Protractor is the better option for Angular 4 automation, after all is a wrapper of Selenium WebDriverJS Api. You can find a lot of materials around the net, paid or free. Of course you need at least the basic single page application knowledge to start testing with protractor.
You can check and Jasmine (https://jasmine.github.io/) it is a BDD framework for testing JS code, does not depend on any other JavaScript frameworks, does not require a DOM. It is really something to check out when you have time to spare.
-
1And it is better WHY? Being a wrapper around Selenium makes it less reliable, in my opinion.– FDMDec 5, 2017 at 9:54
-
1"Protractor is an end-to-end test framework for Angular and AngularJS applications. Protractor runs tests against your application running in a real browser, interacting with it as a user would." It is meant to work with Angular Dec 5, 2017 at 10:41
-
That doesn't make it better because it says so. For me it's useless because we're working in C#, not Javascript.– FDMDec 5, 2017 at 10:45
-
It might be useless to you because of the C#, that doesnt meant it is no the better option. Deepak Thomas doesnt mention any languages.Protractor is handles really well the waits for the elements, it runs in the same js scope and you can get the info from the dev scope. Of course you can advise him something better. Dec 5, 2017 at 10:56
-
1I am not going to argue with you guys, for me if it is JS as language and Angular the logical chooise is Protractor, it handle the spa alot better, but that is me, i just tried to help with something Dec 5, 2017 at 14:32