4. Testing an app in 7+ easy steps
1. Installing and running Protractor
2. First tests
3. Creating test scenarios
4. Real tests
5. Synchronicity and Asynchronicity
6. Page object pattern
7. Debugging, screen capture, and other tips
8. Discussion items and gotchas
6. Kinds of testing
• Unit testing
• Integration testing
• System testing (or Functional testing)
– UI Testing Using Protractor
A simplification!
7. What is Protractor?
• “End-to-End testing”
– Well, not really. Just UI testing
• Built to work with AngularJS
• Built on top of Selenium
• Is a node app
9. When should I use Protractor?
• UI stable
• Use cases well
established
• Already unit-tested
• Uses AngularJS
• UI unstable
• Use cases in flux
• Not unit-tested
• Doesn’t use
AngularJS
Do it Avoid
24. This throws an exception:
This does not:
var text = element(
by.css(‘#my-input’)).getText();
expect(text.split(‘,’)[0]).toBe(‘sumthin’);
var text = element(
by.css(‘#my-input’)).getText();
expect(text).toContain(‘sumthin’);
25. JavaScript Promises
• (Eventually) contains result of computation
• Asynchronous programming
• Composable, chainable, and…FUN!
• A standard
promise.then(function(result) {
verify(result);
}).then(function(result) {
display(result);
});
28. Protractor and `expect`
• expect accepts promises
– delays execution until promise is resolved
var text = element(
by.css(‘#my-input’)).getText();
expect(text.split(‘,’)[0]).toBe(‘sumthin’);
var text = element(
by.css(‘#my-input’)).getText();
expect(text).toContain(‘sumthin’);
29. Let’s Code!
• Getting started
– git checkout qia-step-5-before
• If you get stuck:
– git checkout qia-step-5-after
32. PageObject Big Idea
• Extract CSS ugliness
– element(by.css('#login')).click();
• Into semantically meaningful actions
– loginPage.openDialog();
33. Tests do not touch DOM directly
var loginPage = new require(‘./LoginPage)();
loginPage.login(‘a.b@gmail.com’, ‘secret’);
expect(loginPage.isLoggedIn().toBeTruthy();
loginPage.logout();
34. Let’s Code!
• Page Object Skeleton
– git checkout qia-step-6-before
• Getting started
– git checkout qia-step-6-middle
• If you get stuck:
– git checkout qia-step-6-after
42. Jasmine reporters
• Hook into jasmine spec lifecycle
– before/after test/suite/jasmine
• Things to do
– Format test results
– Grab screenshot on failure
– Cleanup before/after tests
– Start/stop resources
– …
43. Do something on failure
jasmine.getEnv().addReporter({
specDone: function(result) {
if (result.failedExpectations
.length > 0) {
console.log(‘I am a failure…’);
}
}
});
49. It ain’t perfect
• Timing problems
• Difficult to debug
• Inconsistent behavior
• Failures hard to reproduce
• Expensive to maintain
• Tests can be slow
50. Protractor is appropriate
• When…
– Using AngularJS
– UI is mature
– Front end is well unit-tested
– Use cases well described
• Until then…
52. Protractor is…
• For UI testing
• AngularJS-aware
• Secretly powered by Selenium
• Asynchronous and event-based
• A powerful and clean way to test UI
But…
• Expensive to maintain