Professional Documents
Culture Documents
QAI Jasmine Karma Training-1
QAI Jasmine Karma Training-1
QAI Jasmine Karma Training-1
with
Jasmine & Karma
Testing Angular JS Applications with Jasmine & Karma Training Workshop
This material may not be reproduced, displayed, modified or distributed without the
express prior written permission of Zenergy Technologies, Inc. For permission, contact
info@zenergytechnologies.com.
If you have comments or questions regarding this document please send them via e-mail
to: chris.lawson@zenergytechnologies.com
Ø Introduction
• Overview of course
• Logistics
• Expectations
Ø Course Overview
Ø Logistics
Ø Expectations
Ø Functional
Ø Exploratory
Ø Test-Driven Development is a
programming technique that
requires you to write actual code
and automated test code
simultaneously. This ensures that
you test your code and enables
you to retest your code quickly
and easily, since it's automated.
• Test-Driven Development
process means taking the
following steps:
• Red: Write your test first and
see it fail
• Green: Write just enough
code to make your test pass
• Refactor: Using your tests to
keep you safe, refactor your
code to be more elegant,
understandable and
maintainable.
Copyright © 2016 Zenergy Technologies, Inc.
TDD Process A Testing Partner You Can Trust
Ø TDD invests in
automated unit tests
Ø BDD invests in
automated
acceptance tests
Ø Enter – npm
Ø The finished file will be saved as karma.conf.js – find and load in IDE
Ø Write expectations
Ø Uses matchers
Ø The anonymous
function block can
either be more
specific with nested
describe blocks or an
it function
Ø Go to:
• http://jasmine.github.io/2.4/introduction.html
• http://tryjasmine.com
Ø A spy (aka mock) is a fake object that poses as the real thing in order to
satisfy the inherent dependencies without having to go through the
overhead of creating the real object
Ø Spies can create a proxy object that takes the place of the real object
Ø We can define what methods are called and their returned values from
within our test method
• spyOn() - can only be used when the method already exists on the object
Ø You will need the angular mocks library Note: The file order
does matter, load
Ø Install the files in your command window with:
angular libraries first,
• npm install angular –save followed by your
• npm install angular-mocks --save-dev filter, and then the
file you wish to test
// list of files / patterns to load in the browser
'/Users/TempUser/Desktop/test/Filters/test.js’
],
Copyright © 2016 Zenergy Technologies, Inc.
Tes4ng An Angular App A Testing Partner You Can Trust
Ø Using the Angular library you are able to test Angular applications
Ø Example of what a basic Angular Application looks like:
• Using Angular you have to import the Angular Mocks library in order to
test
• The libraries need to be imported before the application and the
application needs to be inserted before the test
Controllers
Where we define the function/applications behavior by
defining functions and values (Where we add behavior)
Expression
How values get displayed in the page
Direc4ves
Common directive commands:
• ng-app : attach application module
• ng-controller : attach a controller function to the page
• ng-show / hide : controls display based on the value
• ng-repeat : cycles through an array
Copyright © 2016 Zenergy Technologies, Inc.
Direc4ves A Testing Partner You Can Trust
<html ng-app="gemStore">
<head>
{{store.product.name}}
</body>
Copyright © 2016 Zenergy Technologies, Inc.
</html>
Filters A Testing Partner You Can Trust
Controller Test
angular.module('app', []) describe('PasswordController', function() {
beforeEach(module('app'));
} else if (size > 3) { it('sets the strength to "strong" if the password length is >8 chars', function() {
$scope.password = 'longerthaneightchars';
$scope.strength = 'medium'; $scope.grade();
expect($scope.strength).toEqual('strong');
} else { });
it('sets the strength to "weak" if the password length <3 chars', function() {
$scope.strength = 'weak'; $scope.password = 'a';
$scope.grade();
} expect($scope.strength).toEqual('weak');
});
}; });
});
}); Copyright © 2016 Zenergy Technologies, Inc.
Tes4ng a Filter A Testing Partner You Can Trust
Filter Test
myModule.filter('length', function() { describe('length filter', function() {
return function(text) {
return ('' + (text || '')).length; var $filter;
}
}); beforeEach(inject(function(_$filter_){
$filter = _$filter_;
}));
Directive Test
app.directive('aGreatEye', function () { describe('Unit testing great quotes', function() {
return { var $compile,
$rootScope;
restrict: 'E',
replace: true, // Load the myApp module, which contains the directive
template: '<h1>lidless, wreathed in flame, {{1 + 1}} beforeEach(module('myApp'));
times</h1>'
}; // Store references to $rootScope and $compile
// so they are available to all tests in this describe block
}); beforeEach(inject(function(_$compile_, _$rootScope_){
// The injector unwraps the underscores (_) from around the parameter names
when matching
$compile = _$compile_;
$rootScope = _$rootScope_;
}));
// spec.js
browser.get('http://juliemr.github.io/protractor-demo/');
element(by.model('first')).sendKeys(1);
element(by.model('second')).sendKeys(2);
element(by.id('gobutton')).click();
expect(element(by.binding('latest')).getText()).
toEqual('3');
});
});
Copyright © 2016 Zenergy Technologies, Inc.
Protractor Framework A Testing Partner You Can Trust
Ø Less code
duplication
Ø Readable
Ø Maintainable
Ø Should only
access web
elements
using page
object
functions
Ø findElement
• requires a single By parameter
• returns a single WebElement
• if multiple matches exist, returns the first match
• if no matches exist, throws NoSuchElementException
Ø findElements
• requires a single By parameter
• returns a list of WebElements
• if no matches are found, returns an empty list
Copyright © 2016 Zenergy Technologies, Inc.
Loca4ng Elements A Testing Partner You Can Trust
<body>
<div id=“navigation” class=“navBar” name=”top-nav-bar”>
<a id=“searchBtn” class=“search” name=”searchBar”>
Shop Now
</a>
</ul>
</body>
Ø By ID
• Find elements using the HTML “id” attribute
• This (along with “name”) is the preferred way of locating elements
• Beware of non-unique and dynamic IDs when using this strategy
<body>
<div id=“navigation” class=“navBar” name=”top-nav-bar”>
<a id=“searchBtn” class=“search” name=”searchBar”>
Shop Now
</a>
</ul>
</body>
Ø By Name
• Find input elements using the HTML “name” attribute
• This (along with “id”) is the preferred way of locating elements
<body>
<div id=“navigation” class=“navBar” name=”top-nav-bar”>
<a id=“searchBtn” class=“search” name=”searchBar”>
Shop Now
</a>
</ul>
</body>
Ø By Class Name
• Find input elements using the HTML “class” attribute
• Class attributes are much less likely to be unique than ids and names
• If multiple elements have the same class attribute, findElement will return the
first instance and findElements will return all instances
Copyright © 2016 Zenergy Technologies, Inc.
“By” Strategies – Tag Name A Testing Partner You Can Trust
<body>
<div id=“navigation” class=“navBar” name=”top-nav-bar”>
<a id=“searchBtn” class=“search” name=”searchBar”>
Shop Now
</a>
</ul>
</body>
Ø By Tag Name
• Find elements by their tag name in the DOM
• Tag names will rarely be unique, but this strategy can be helpful for
gathering an iterable list of a certain type of tag
<body>
<div id=“navigation” class=“navBar” name=”top-nav-bar”>
<a id=“searchBtn” class=“search” name=”searchBar”>
Shop Now
</a>
</ul>
</body>
Ø By Link Text
• Find the link element with matching visible text
• This strategy is only useful for finding links (i.e. <a> tags)
• Beware: An element’s link text is often more likely to change than the other locator
types
Copyright © 2016 Zenergy Technologies, Inc.
“By” Strategies – Par4al Text A Testing Partner You Can Trust
<body>
<div id=“navigation” class=“navBar” name=”top-nav-bar”>
<a id=“searchBtn” class=“search” name=”searchBar”>
Shop Now
</a>
</ul>
</body>
Ø By Partial Link Text
• Find the link element with partial matching visible text
• This strategy is only useful for finding links (i.e. <a> tags)
• A partial link text locator is less likely to change than using the full link text
Ø By CSS
• uses CSS selectors to precisely locate
elements
• this strategy allows for more precision
than the previous ones, but is slower to
execute
Selector Description
li All <li> elements
li a <a> elements inside an <li> elements
a[name=‘home’] <a> elements with name = “home”
div > p <p> element whose immediate parent is a <div> element
li[class^=‘nav’] <li> whose “class” attribute starts with “nav”
a[href$=‘.pdf’] <a> whose “href” attribute ends with “.pdf”
div[id*=‘product’] <div> whose “id” attribute contains the substring “product”
li:nth-child(3) Every <li> that is the 3rd child of its parent
li:nth-of-type(3) Every <li> that is the 3rd child of type <li>
a:not(.disabled) Every <a> element that does not have class = “disabled”
Ø By Xpath
• Xpath is a language used for selecting elements from a DOM structure
• Xpath is the most precise locator strategy, but also the slowest to execute
• For browsers that don’t have native Xpath support, Selenium provides its
own implementation, which can lead to some unexpected behavior unless
you are aware of the differences in the various Xpath engines
<body>
<div id=“navigation” class=“navBar” name=”top-nav-bar”>
<a id=“searchBtn” class=“search” name=”searchBar”>
Shop Now
</a>
</ul>
</body>
<body>
<div id=“navigation” class=“navBar” name=”top-nav-bar”>
<a id=“searchBtn” class=“search” name=”searchBar”>
Shop Now
</a>
</ul>
</body>
Ø Firebug is a Firefox plugin that provides tools for close inspection of the
DOM.
Ø It is available for free at http://getfirebug.com/
WebElement methods:
Ø click – mouse clicks on the selected element
Ø getText – returns the text present in the element
Ø sendKeys – enters text into a text input element
Ø clear – if element accepts text input, clears the value
Ø getAttribute – returns the value of an HTML attribute
Ø getCssValue – returns the value a CSS property for the element
Ø isDisplayed – returns a Boolean stating whether or not the element is displayed
Ø isSelected – returns a Boolean stating whether or not the element is selected;
must be an input element, such as a checkbox or radio btn
Ø getLocation – returns the point of the top left corner of the element
• Synchronization problems
• Stale elements
• Implicit Waits
• Explicit Waits
Ø Implicit waits are an ideal solution for handling page load delays
Ø Explicit waits define a specific condition that should occur before WebDriver
continues to execute the script
Ø Configure the driver to use an implicit wait; this will catch a lot of
synchronization issues
Ø For synchronization problems that are not solved by an implicit wait, use an
explicit wait
Ø Zero-out the implicit wait before using an explicit wait; otherwise, both
waits will be polling the DOM and performance can be severely impacted
Ø Set the implicit wait to the shortest effective timeout and use explicit waits
for longer delays; just a few seconds can make a big difference on suite run
times
Ø Continuous integration
Ø Code coverage
Ø Remote/distributed testing
seleniumhq.org
codeproject.com
wikipedia.org
developsense.com
automatedtestinginstitute.com
code.google.com
msdn.com
w3schools.com
w3.abcd.harvard.edu
agiletestingframeworks.com
angularjs.org
karma-runner.github.io
jasmine.github.io
protractortest.org
Istqb.org