ag-Grid Testing
Testing ag-Grid with Protractor & Jasmine
Information on testing in an Angular application can be found in the Angular More Details section.
End to End (e2e) Testing
We will walk through how you can use Protractor and Jasmine to do End to End (e2e) testing
with ag-Grid in this section.
These recipes below are suggestions - there are many ways to do End to End testing, what we document below is
what we use here at ag-Grid.
We do not document how to use either Protractor and Jasmine in depth here - please see either the
Protractor or
Jasmine for information around either of these tools.
We only describe how these tools can be used to test ag-Grid below.
End to End testing can be fragile. If you change something trivial upstream it can have a big impact on an End to End test,
so we recommend using End to End tests in conjuction with unit tests. It's often easier to find and fix a problem at the unit
testing stage than it is in the end to end stage.
Testing Dependencies
npm install protractor webdriver-manager --save-dev
// optional dependencies - if you're using TypeScript
npm install @types/jasmine @types/selenium-webdriver --save-dev
Note you can install protractor and webdriver-manager globally if you'd prefer,
which would allow for shorter commands when executing either of these tools.
We now need to update the webdriver:
./node_modules/.bin/webdriver-manager update
This can be added to your package.json for easier packaging and repeatability:
"scripts": {
"postinstall": "webdriver-manager update"
}
Selenium Server
You can either start & stop your tests in a script, or start the Selenium server seperately, running your tests against it.
Remember that the interaction between your tests and the browser is as follows:
[Test Scripts] < ------------ > [Selenium Server] < ------------ > [Browser Drivers]
We'll run the server separately to begin with here:
./node_modules/.bin/webdriver-manager start
Sample Configuration
// conf.js
exports.config = {
framework: 'jasmine',
specs: ['spec.js']
}
Here we specify the Jasmine testing framework as well as our test to run.
Sample Test
If you're testing against a non-Angular application then you need to tell Protractor
not to wait for Angular by adding this to either your configuration or your tests: browser.ignoreSynchronization = true;
For this test we'll testing a simple JavaScript based grid which can be found at the Getting Started -> JavaScript Section:
Checking Headers
Let's start off by checking the headers are the ones we're expecting. We can do this by retrieving all div's that
have the ag-header-cell-text class:
// spec.js
describe('ag-Grid Protractor Test', function () {
// not an angular application
browser.ignoreSynchronization = true;
beforeEach(() => {
browser.get("https://www.ag-grid.com/example-runner/vanilla.php?section=javascript-getting-started&example=hello-world");
});
it('should have expected column headers', () => {
element.all(by.css(".ag-header-cell-text"))
.map(function (header) {
return header.getText()
}).then(function (headers) {
expect(headers).toEqual(['Make', 'Model', 'Price']);
});
});
});
We can now run our test by executing the following command:
./node_modules/.bin/protractor conf.js
// or if protractor is installed globally
protractor conf.js
Checking Grid Data
We can match grid data by looking for rows by matching div[row="<row id>"] and then column
values within these rows by looking for div's with a class of .ag-cell-value:
it('first row should have expected grid data', () => {
element.all(by.css('div[row="0"] div.ag-cell-value'))
.map(function (cell) {
return cell.getText();
})
.then(function (cellValues) {
expect(cellValues).toEqual(["Toyota", "Celica", "35000"]);
});
});
We can add this to spec.js and run the tests as before.
ag-Grid Testing Utilities
These utilities scripts should still be considered beta and are subject to change. Please provide feedback to
the GitHub repository.
Here at ag-Grid we use a number of utility functions that make it easier for us to test ag-Grid functionality.
The utilities can be installed & imported as follows:
Installing:
npm install ag-grid-testing --save-dev
Importing:
let ag_grid_utils = require("ag-grid-testing");
verifyRowDataMatchesGridData
Compares Grid data to provided data. The order of the data provided should correspond to the order within the grid.
The property names should correspond to the colId's of the columns.
ag_grid_utils.verifyRowDataMatchesGridData(
[
// first row
{
"name": "Amelia Braxton",
"proficiency": "42%",
"country": "Germany",
"mobile": "+960 018 686 075",
"landline": "+743 1027 698 318"
},
// more rows...
]
);
verifyCellContentAttributesContains
Userful when there is an array of data within a cell, each of which is witing an attribute (for example an image).
ag_grid_utils.verifyCellContentAttributesContains(1, "3", "src", ['android', 'mac', 'css'], "img");
allElementsTextMatch
Verifies that all elements text (ie the cell value) matches the provided data. Usf
ag_grid_utils.allElementsTextMatch(by.css(".ag-header-cell-text"),
['#', 'Name', 'Country', 'Skills', 'Proficiency', 'Mobile', 'Land-line']
);
clickOnHeader
Clicks on a header with the provided headerName.
ag_grid_utils.clickOnHeader("Name");
getLocatorForCell
Provides a CSS Locator for a grid cell, by row & id and optionally a further CSS selector.
ag_grid_utils.getLocatorForCell(0, "make")
ag_grid_utils.getLocatorForCell(0, "make", "div.myClass")
getCellContentsAsText
Returns the cell value (as text) for by row & id and optionally a further CSS selector.
ag_grid_utils.getCellContentsAsText(0, "make")
.then(function(cellValue) {
// do something with cellValue
});
ag_grid_utils.getCellContentsAsText(0, "make", "div.myClass")
.then(function(cellValue) {
// do something with cellValue
});