Skip to content

Commit

Permalink
use more descriptive and accessible query methods in Cypress tests
Browse files Browse the repository at this point in the history
  • Loading branch information
walesch-yan authored and marcus-oscarsson committed Jun 21, 2024
1 parent 9159185 commit 2e491ea
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
6 changes: 3 additions & 3 deletions ui/cypress/e2e/sampleControls.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ describe('3-click centring', () => {

it('3-click centring should not work without sample', () => {
cy.clearSamples();
cy.contains('button', '3-click centring').click();
cy.findByRole('button', { name: '3-click centring' }).click();
cy.findByRole('alert', 'Error: There is no sample mounted').should(
'be.visible',
);
});

it('Each click is rotating the sample by 90 degrees', () => {
cy.mountSample();
cy.contains('button', '3-click centring').click();
cy.findByRole('button', { name: '3-click centring' }).click();
cy.get('.form-control[name="diffractometer.phi"]')
.invoke('val')
.then((value) => {
Expand All @@ -29,7 +29,7 @@ describe('3-click centring', () => {
cy.wait(1000);
cy.reload();
// press the centring button again after reload to stay in the correct mode
cy.contains('button', '3-click centring').click();
cy.findByRole('button', { name: '3-click centring' }).click();
cy.get('.form-control[name="diffractometer.phi"]')
.invoke('val')
.should('equal', omegaValue.toFixed(2));
Expand Down
24 changes: 13 additions & 11 deletions ui/cypress/support.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,44 @@ Cypress.Commands.add('login', (username = 'idtest0', password = '0000') => {
cy.findByRole('button', { name: 'Sign in' }).click();
});

Cypress.Commands.add('takeControl', () => {
Cypress.Commands.add('takeControl', (returnPage = '/datacollection') => {
/* firefox (only firefox) throws an unhandled promise error when executing
this function. Hence, we tell cypress to ignore this, otherwise the tests
fail, when we try to click the observer mode dialog away. */
Cypress.on('uncaught:exception', (err, runnable) => {
return false;
});

// ensure to click away the observer mode dialog box if present
// control only needs to be taken, when observer mode is present
cy.get('body').then(($body) => {
if ($body.text().includes('Observer mode')) {
cy.wrap($body.find('.modal-dialog').find('.form-control')).type('test');
cy.findByText('OK').click();
cy.findByRole('button', { name: 'OK' }).click();
cy.findByText('Remote').click();
cy.findByRole('button', { name: 'Take control' }).click();
cy.visit(returnPage);
}
});
cy.request('POST', '/mxcube/api/v0.1/ra/take_control');

// tell cypress to listen to any uncaught:execptions again
Cypress.on('uncaught:exception', (err, runnable) => {
return true;
});
cy.reload();
});

Cypress.Commands.add('mountSample', (sample = 'test', protein = 'test') => {
cy.visit('/datacollection');
cy.findByRole('button', { name: /Queued Samples/u }).click();
cy.findByText('Create new sample').click();
cy.findByRole('button', { name: 'Create new sample' }).click();
cy.findByLabelText('Sample name').type(sample);
cy.findByLabelText('Protein acronym').type(protein);
cy.findByText('Mount').click();
cy.findByRole('button', { name: 'Mount' }).click();
// reload for button changes to take effect
cy.reload();
});

Cypress.Commands.add('clearSamples', () => {
cy.request('PUT', '/mxcube/api/v0.1/queue/clear');
cy.reload();
Cypress.Commands.add('clearSamples', (returnPage = '/datacollection') => {
cy.findByText('Samples').click();
cy.findByRole('button', { name: /Clear sample list/u }).click('left');
cy.findByRole('button', { name: 'Ok' }).click();
cy.visit(returnPage);
});

0 comments on commit 2e491ea

Please sign in to comment.