The Petclinic application is a demo implementation. This means there are some anomalies that real applications might not have. One such is the missing function of deleting an owner in the angular front end.

When testing the front end this leads to the anomaly, that the owner list grows until the rest server is reset and all previous data is lost.

A walk around is the use of the owners DELETE api from the Rest application server.

After-Hook.png

 

Here we query for all owners, filter the correct owner by comparing first name and last name, et the owners database id and then delete the owner.

 

Warning : Does this delete pets and visits of the owner?

 

For this we create a helper class RestApiClient.js where we implement the query and delete

functionality

 

 
class RestAPIClient {
    findOwnerByFirstNameAndLastName(firstName, lastName) {
        return cy.request({
            method: 'GET',
            url: 'http://localhost:9966/petclinic/api/owners'
        }).then((response) => {
            let owners = response.body;
            return owners.find(owner => owner.firstName === firstName && owner.lastName === lastName);
        });
    }
    deleteOwnerByFirstNameAndLastName(firstName, lastName) {
        this.findOwnerByFirstNameAndLastName(firstName, lastName).then((owner) => {
            cy.log(" ### Delete - " + owner.id + " " + owner.firstName + " " + owner.lastName); 
            if (owner === undefined) {
                return;
            }
            if (owner.id === undefined) {
                return;
            }
            cy.request({
                method: 'DELETE',
                url: 'http://localhost:9966/petclinic/api/owners/' + owner.id
            });
        });
}
}
export default RestAPIClient;

Then we implement an After Hook in the Owner.js file.

After(() => {
    restAPIClient.deleteOwnerByFirstNameAndLastName("Travis","Coleman");
})

To use this approach it is necessary, that the owner name is different from the default owner names. This is especially true with the search functionality, where the owners are searched via the start of the last name. Test data need to use unique beginnings of the last names so that the test cases can run concurrently.