datopian / ckan-integration-tests

Cypress toolkit to run integration tests against a CKAN instance
https://tech.datopian.com/ckan/
MIT License
4 stars 3 forks source link
ckan cypress integration-testing

CKAN Integration Tests

This package uses Cypress which is a JavaScript based end-to-end testing framework, and its BDD (Behaviour Driven Development) interface (describe(), it() etc.).

This package can be NPM-installed to automatize different test related to a CKAN platform.

Thanks to Jari for the effort in converting core CKAN front-end tests to Cypress.

Install

Requires NodeJS 13.2 or newer, with npm:

$ npm install https://github.com/datopian/ckan-integration-tests

Usage

Start with a regular Cypress repository, then:

  1. Create a test.js file to wrap-up your Cypress call, for

    import cypress from "cypress";
    import { CKANIntegrationTests } from "ckan-integration-tests";
    
    const assets = new CKANIntegrationTests();
    assets.addAllSpecs();
    
    cypress
     .run(assets.options)
     .then(console.log)
     .catch(console.error)
     .finally(() => assets.cleanUp());
  2. Add test.js as your test script in the package.json (and make sure you are using ES modules):
    {
       …
       "type": "module",
       "scripts": {
           …,
           "test": "node test.js"
       },
       …
    }
  3. Copy cypress.sample.json as cypress.json and configure the CKAN URL, its front-end URL, user name, password and API key — for example:

    {
     "chromeWebSecurity": false,
     "baseUrl": "https://ckan.myserver.org/",
     "pageLoadTimeout": 120000,
     "env": {
       "FRONTEND_URL": "https://ckan.myserver.org/",
       "API_KEY": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
       "CKAN_USERNAME": "joanne.doe",
       "CKAN_PASSWORD": "joannes-very-secret-password",
       "ORG_NAME_SUFFIX": "_organization_test",
       "DATASET_NAME_SUFFIX": "_dataset_test"
     }
    }

Now npm test will run:

Also, custom commands and fixtures from both repositories are available for your tests too.

API

The example setup above includes basic tests for your CKAN application. You can run them all or select which ones make sense for your instance using CKANIntegrationTests.addSpecs instead of CKANIntegrationTests.addAllSpecs.

CKANIntegrationTests.addSpec(source, specs)

CKANIntegrationTests.addAllSpecs()

Adds all test specs from this repository and from your repository. It is a shortcut for:

assets.addSpecs(assets.src);
assets.addSpecs(".");

Import Tests as JavaScript Functions

Wrapped tests can be found in files (like ckan-classic-ui-tests and ckan-datastore-tests) into the cypress/support/ directory. These JavaScript functions can be imported and used outside the ckan-integration-tests module.

Example:

// cypress/support/ckan-datastore-tests.js

function createDatastoreTableNewResource(datasetName){
  it('Create a DataStore table with a new CKAN resource', () => {
    cy.get('#field-name').type(datasetName)
    cy.get('.btn-primary').click()
    ...
  })
}

module.exports.createDatastoreTableNewResource = createDatastoreTableNewResource;

Then in the spec, we can call the defined function:

// cypress/integration/ckan-datastore.js

describe('CKAN DataStore Actions', () => {
  beforeEach(function () {
    cy.clearCookies()
    ...
  })
  afterEach(() => {
    ...
  })
  ...
  createDatastoreTableNewResource(datasetName)
  ...
})

Following the same principle, you can import the functions you want in your project set of tests.

import { createDatastoreTableNewResource } from "ckan-integration-tests/cypress/support/ckan-datastore-tests"

describe('CKAN DataStore Actions', () => {
  beforeEach(function () {
    cy.clearCookies()
    ...
  })
  afterEach(() => {
    ...
  })
  ...
  createDatastoreTableNewResource(datasetName)
  ...
})

Design

Cypress

Cypress is a next-generation front end testing tool constructed for modern web applications. Most testing tools (like Selenium) operate by running outside of the browser and executing remote commands across the network. But the Cypress engine directly operates inside the browser. It enables Cypress to listen and modify the browser behavior at run time by manipulating DOM and altering Network requests and responses on the fly.

Check Cypress's documentation to learn more.

Inheritance

Inheritance in Cypress is done using commands.

If a test requires a common thing such as login, download file, register. It should be added to the commands.js file. For example, the login in cypress/support/commands.js is a command used in different places.

Commands are always loaded before any test runs.

As mentioned below in best practices please make sure your tests are always isolate even if that means waiting a few extra seconds.

Best Practices