Dear future Masters of Angular 2 E2E Testing.
This is it. At the end of this lesson you will finally have every tools to create functional tests for your Angular 2 applications. No more excuses: You’ve got the power!
We will first get familiar with Locators then we will see how awesome Page Objects can be, followed by how to handle asynchronousness and finally the easiest part that is triggering Actions.
If you don’t have the basics right, head there otherwise you will be a little bit lost.
One if not the most important thing in E2E testing is finding the elements that you want to test in your view. Most of the time, you will ask yourself the question “why can’t I find this **** element?” followed by a “oooohhhhh”.
You have many ways to do this. Most of them depend of your application. In order to get your elements, you will use a range of Locators.
Most of the time, you can find your elements by:
- Class name.
For the upcoming tests, we will use this template.html:
Let’s focus on “nested-class” and “nested-id”.
We grab here our element using element(by.css(selector)) then we test if it’s present or not.
We grab here our element using element(by.id(elem-id)) then we test if it’s present or not.
The following locators a very useful in AngularJS but not yet implemented in Angular 2 so just keep them on the side for now.
In order to debug your tests, you can just add “browser.pause()” in your test, you will be able to run all the previous instructions and stop wherever you want.
Once it reaches your pause, you will have a screen similar to this one:
As you can see in this image, in order to get into the driver’s seat you need to type repl.
Now let’s say you don’t have any test and you just want to play around with Protractor.
You can just type in your terminal:
If you have some issues with your path, you can type:
No need to type repl this time, just use the browser object and you are ready to sail!
What if you have tests that share the same scenario? Are you going to copy paste your code everywhere? What if this scenario changes? Now you have to go through every files and make the modifications.
That’s where Pages Objects become very useful. Write Once, Share Everywhere!
A Page Object is an object that contains the elements and scenarios concerning a page.
First a very simple Page Object:
Then we use it in our test:
We import the Page Object, create an instance then use it’s property.
Now my friends, you can access any elements on your page! I mean almost :).
There will be some special cases.
If you were (un)lucky enough to work with jQuery, you must be familiar with document.ready().
In order to handle that, you will have to use a feature of Protractor in order to wait for your element to be ready before looking for it.
Here is an example displaying a button after a timeout expiration.
First the class that contains the timeout:
Then the template.html.
We have here a button that will trigger our timeout, once the timeout expires, the text is displayed.
Here are the tests.
We find our button, click on it, try to find our text BUT we expect it not to be present.
And here is the way we handle this case ;).
We use “browser.wait” combined with “browser.isElementPresent” testing our Locator. After 5 seconds the rest of the code is executed. Don’t forget to put a timer here otherwise your tests will be blocked forever!
So if one of your element is supposed to be here but you can’t get it, your gut instinct should tell you to remember what you have just read here.
Now that you have your precious element, you have done 90% of the work!
If you can’t find it, it’s either that your application doesn’t work as it should (that’s why we do tests) or you need to get better at testing and spend less time on Pokemon GO hunting dratinis near a river at 1am (we have all been there …).
Let’s keep going with the actions that you can use on an element. Here they are:
- elem.sendKeys: Type something in an input.
- elem.click: Click.
- elem.clear: Erase everything in an input.
- elem.getAttribute(‘attrName’): Return a specific attribute of the element.
- elem.submit: Submit a form.
- elem.isPresent: Test if the element is present.
We are using Protractor which relies on WebDriver. If you need more fancy actions, you can have a look at what is available here.
Let’s test some of them.
For the template.html:
We will play a bit with the text input. Quick quiz, what does this test do (don’t read the description)?
Solution: We get our input, type “Some text” inside, test that we got the value set, clear it and finally test that the value is empty.
You can do anything that you want with an element. The best way to test it is to use the matchers that are available. Why? Because they are elegant and close to the human language, example:
This was the final post of this series, right now you have 90% of the skills to test your Angular 2 applications. You will get the other 10% by experience. Remember, first getting your elements using Locators, then putting the one that might be replicated in a Page Object, if you can’t find your element and you are sure that it is here, it might be might because it hasn’t appeared yet and you need to handle the asynchronousness. Finally test the **** out of your elements with Actions.