Skip to content

Commit

Permalink
template: cypress test
Browse files Browse the repository at this point in the history
Implements Cypress to about template create and utilisation.

Co-Authored-by: Renaud Michotte <renaud.michotte@gmail.com>
  • Loading branch information
zannkukai committed Oct 28, 2020
1 parent 753e3db commit 6e1e31e
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 15 deletions.
11 changes: 11 additions & 0 deletions tests/e2e/cypress/cypress/fixtures/templates.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"templateA": {
"name": "Cypress template",
"document": {
"type": "sound",
"title": {
"mainTitle": "Template title"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/// <reference types="Cypress" />
/*
RERO ILS
Copyright (C) 2020 RERO
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

before(function () {
cy.fixture('users').then(function (userData) {
this.users = userData;
})
cy.fixture('common').then(function (commonData) {
this.common = commonData;
})
cy.fixture('templates').then(function (templateData) {
this.templates = templateData;
})
cy.server();
cy.route({method: 'GET', url:'/api/templates/?q=*'}).as('api_template_search')
cy.route({method: 'GET', url:'**/professional/records/document**)'}).as('document_editor')
cy.route({method: 'POST', url:'/api/templates/'}).as('api_template_create')
})

describe('Templates: Create and use template for a document', function() {

before('Setting defaults', function() {
cy.visit('')
cy.setLanguageToEnglish()
})

beforeEach('Login as admin and clean templates', function() {
const template = this.templates.templateA
cy.apiLogout()
cy.apiLogin(this.users.librarians.spock, this.common.uniquePwd)
cy.apiDeleteResources('templates', 'name:"'+template.name+'"')
})

after('Logout user', function() {
cy.apiLogout()
})

it('Create a new template and use it in the editor', function() {
const template = this.templates.templateA

cy.visit('/professional/records/documents/new')
cy.wait(2000)
cy.get('ng-core-editor #type').select(template.document.type)
cy.get('#title-0-mainTitle-0-value').type(template.document.title.mainTitle, {force: true})
cy.get('#editor-save-button-split').click()
cy.get('#editor-save-button-dropdown-split')
.find('li a.dropdown-item:nth-child(1)') // TODO: Find a better way to retrieve the correct link to click
.click()
cy.get('.modal-content #name').type(template.name)
cy.get('.modal-content button:submit').click()
cy.wait('@api_template_create')
cy.url().should('include', 'records/templates/detail')

cy.visit('/professional/records/documents/new')
cy.wait(2000)
cy.get('#editor-load-template-button').click()
cy.wait('@api_template_search')
cy.get('.modal-content #template').select(template.name)
cy.get('.modal-content button:submit').click()

cy.url(5000).should('include', '?source=templates&pid=')
cy.get('ng-core-editor #type').invoke('val').should('eq', template.document.type)
cy.get('#title-0-mainTitle-0-value').invoke('val').should('eq', template.document.title.mainTitle)
cy.log('Template loaded successfully !')
})
})
75 changes: 75 additions & 0 deletions tests/e2e/cypress/cypress/support/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
RERO ILS
Copyright (C) 2020 RERO
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

/** API call to logout method ============================================== */
Cypress.Commands.add('apiLogout', () => {
cy.request({
method: 'GET',
url: '/signout/',
followRedirect: false
})
})

/** API call to login method ==================================================
* :param user - object: a user object from the 'users' fixtures file
* :param password - string: the password to log in
*/
Cypress.Commands.add('apiLogin', (user, password) => {
cy.request('/signin')
.its('body')
.then(body => {
const $html = Cypress.$(body)
const csrf_token = $html.find('input[name=csrf_token]').val()
cy.log(csrf_token)

cy.request({
method: 'POST',
url: '/signin/',
followRedirect: false,
body: {
'email': user.email,
'password': password,
'csrf_token': csrf_token
}
}).then(response => {
cy.visit('/')
cy.get('#logout-menu') // raise an error if not found
})
})
})


/** API call to delete items ==================================================
* Delete resource items based on a query
* :param resourceName - string: the resource type
* :param query - string: criteria to find items to delete
*/
Cypress.Commands.add('apiDeleteResources', (resourceName, query) => {
const q = encodeURI('/api/'+resourceName+'/?q='+query)
cy.request(q).its('body.hits.hits', {timeout:2000}).then(hits => {
hits.forEach(hit => {
const pid = hit.metadata.pid
cy.request({
method: 'DELETE',
url: '/api/'+resourceName+'/'+pid
})
cy.log('Template#'+pid+' deleted')
})
})
})
1 change: 1 addition & 0 deletions tests/e2e/cypress/cypress/support/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// ***********************************************************

// Import commands.js using ES2015 syntax:
import './api'
import './commands'
import './utils'
import './circulation'
Expand Down
33 changes: 18 additions & 15 deletions tests/e2e/cypress/cypress/support/record.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,28 @@ Cypress.Commands.add("deleteRecordFromDetailView", () => {
});

Cypress.Commands.add("populateSimpleRecord", (document) => {
cy.fixture('documents').then(function (documents) {
this.documents = documents;
});
// Choose type
cy.get('ng-core-editor-select-with-sort-type.ng-star-inserted > #type').select(document.type);

if (document.hasOwnProperty('type')) {
cy.get('ng-core-editor-select-with-sort-type.ng-star-inserted > #type').select(document.type);
}
// Enter title
cy.get('#title-0-mainTitle-0-value').type(document.title.mainTitle);

if (document.hasOwnProperty('title')) {
if (document.title.hasOwnProperty('mainTitle')) {
cy.get('#title-0-mainTitle-0-value').type(document.title.mainTitle);
}
}
// Enter provision activity
cy.get('#provisionActivity-0-type').select(document.provisionActivity.type);
cy.get('#provisionActivity-0-startDate').type(document.provisionActivity.publicationDate1);
cy.get('#provisionActivity-0-statement-0-label-0-value').type(document.provisionActivity.statement.place);
cy.get('#provisionActivity-0-statement-1-label-0-value').type(document.provisionActivity.statement.agent);
cy.get('#provisionActivity-0-statement-2-label-0-value').type(document.provisionActivity.statement.date);

if (document.hasOwnProperty('provisionActivity')) {
cy.get('#provisionActivity-0-type').select(document.provisionActivity.type);
cy.get('#provisionActivity-0-startDate').type(document.provisionActivity.publicationDate1);
cy.get('#provisionActivity-0-statement-0-label-0-value').type(document.provisionActivity.statement.place);
cy.get('#provisionActivity-0-statement-1-label-0-value').type(document.provisionActivity.statement.agent);
cy.get('#provisionActivity-0-statement-2-label-0-value').type(document.provisionActivity.statement.date);
}
// Choose language
cy.get('#language-0-value').select(document.language1);

if (document.hasOwnProperty('language1')) {
cy.get('#language-0-value').select(document.language1);
}
// Choose mode of issuance
// TODO find a way to use custom id for oneOf
});
Expand Down

0 comments on commit 6e1e31e

Please sign in to comment.