Skip to content
Georg Wiese edited this page Apr 11, 2016 · 32 revisions

Testing

webKnossos support both unit tests and integration/end-to-end (E2E) tests.

Requirements

  • Google Chrome with webdriver support
  • node v0.12+

Running Tests

npm test  # For unit and E2E tests
sbt test  # For E2E tests only

Jasmine

All tests are run as Jasmine tests. Jasmine can be used to write plain old unit tests or rely on Protractor to access DOM elements for E2E tests.

describe("A suite", function() {
  it("contains spec with an expectation", function() {
    expect(true).toBe(true);
  });
});

Unit Tests

All unit tests live in the app/assets/javascripts/test directory and end with .spec.coffee. They are run by jasmine-node inside the node environment, but could just as well be run by protractor.

Here's is a commented unit test skeleton:

# mock-require is used to mock require modules
mockRequire = require("mock-require")
# sinon is used for function mocks
sinon = require("sinon")
# run-async is a helper to run code (e.g. assertions) asynchronously
runAsync = require("../../helpers/run-async")

mockRequire("../../../some/unit/dependency", {
  # Construct mock to be passed to the unit under test...
})

Unit = require("../../../unit/under/test")

describe "Unit", ->

  it "should call x.foo() asynchronously", (done) ->

    x = {foo : sinon.stub()}
    Unit.bar(x)

    runAsync([
      ->
        expect(x.foo.called).toBe(true)
        done()
    ])

End-to-End (E2E) tests

All E2E tests live in the app/assets/javascripts/test directory and end with .2e2.coffee.

Test DB

For the E2E tests, the DB dump stored in testdb/ is imported into the oxalis-testing DB. This happens automatically.

In order to change the test db, perform the following steps:

# Drop the play-oxalis db
tools/dropDB.sh

# Import the testdb into play-oxalis
tools/import_export/import.sh play-oxalis testdb

# Do appropriate changes to the db...

# Export play-oxalis, overwriting the existing testdb dump
tools/import_export/export.sh play-oxalis testdb

Protractor

Link to API.

Protractor is a wedriver/selenium abstraction that allows for easy access to DOM elements. See Helpers section below for advice.

explorativeTab = element(By.css("#tab-explorative"))

Helpers

PageObject Pattern

All major wK pages (e.g. Dashboard, Users List, etc) should be wrapped in Page Class that has convenience methods for navigating to the page and access certain buttons / DOM elements.

class Dashboard extends BaseTestPage

  get : ->
    browser.get '/dashboard'

  clickButton : ->
     return @waitForButton()
      .then (btn) -> btn.click()

All protractor calls to elements should make sure that the element is already rendered / available in the DOM by waiting for it. Selecting an element through a CSS selection is easily done through the helper method XYZ().

element = XYZ("#tab-explorative")
Clone this wiki locally