Skip to content

Commit

Permalink
Beef up flaky extension banner test
Browse files Browse the repository at this point in the history
- banner should be visibile when test starts
- banner is visible if v3 setting is not 'true' and there is a missing helm repo
- previously confirmed the repo is missing, now confirm the v3 setting is correct
- split rancher api commands out from base commands, added get/set commands
  • Loading branch information
richard-cox committed Jul 21, 2023
1 parent 3eb29b7 commit ff065e5
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 49 deletions.
16 changes: 16 additions & 0 deletions cypress/e2e/tests/pages/extensions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,22 @@ describe('Extensions page', { tags: '@adminUser' }, () => {
});

it('New repos banner should only appear once (after dismiss should NOT appear again)', () => {
cy.getRancherResource('v3', 'setting', 'display-add-extension-repos-banner', null).then((resp: Cypress.Response<any>) => {
const notFound = resp.status === 404;
const requiredValue = resp.body?.value === 'true';

if (notFound || requiredValue) {
cy.log('Good test state', '/v3/setting/display-add-extension-repos-banner', resp.status, JSON.stringify(resp?.body || {}));
} else {
cy.log('Bad test state', '/v3/setting/display-add-extension-repos-banner', resp.status, JSON.stringify(resp?.body || {}));

return cy.setRancherResource('v3', 'setting', 'display-add-extension-repos-banner', {
...resp.body,
value: 'true'
});
}
});

const appRepoList = new ReposListPagePo('local', 'apps');

// Ensure that the banner should be shown (by confirming that a required repo isn't there)
Expand Down
5 changes: 4 additions & 1 deletion cypress/globals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ declare namespace Cypress {
setGlobalRoleBinding(userId: string, role: string): Chainable;
setClusterRoleBinding(clusterId: string, userPrincipalId: string, role: string): Chainable;
setProjectRoleBinding(clusterId: string, userPrincipalId: string, projectName: string, role: string): Chainable;
getProject(clusterId: string, projectName: string): Chainable;
getProjectByName(clusterId: string, projectName: string): Chainable;

getRancherResource(prefix: 'v3' | 'v1', resourceType: string, resourceId: string, expectedStatusCode: string): Chainable;
setRancherResource(prefix: 'v3' | 'v1', resourceType: string, resourceId: string, body: string): Chainable;

/**
* Wrapper for cy.get() to simply define the data-testid value that allows you to pass a matcher to find the element.
Expand Down
52 changes: 52 additions & 0 deletions cypress/support/commands/commands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Matcher } from '@/cypress/support/types';

/**
* Get input field for given label
*/
Cypress.Commands.add('byLabel', (label) => {
return cy.get('.labeled-input').contains(label).siblings('input');
});

/**
* Wrap the cy.find() command to simplify the selector declaration of the data-testid
*/
Cypress.Commands.add('findId', (id: string, matcher?: Matcher = '') => {
return cy.find(`[data-testid${ matcher }="${ id }"]`);
});

/**
* Wrap the cy.get() command to simplify the selector declaration of the data-testid
*/
Cypress.Commands.add('getId', (id: string, matcher?: Matcher = '') => {
return cy.get(`[data-testid${ matcher }="${ id }"]`);
});

Cypress.Commands.add('keyboardControls', (triggerKeys: any = {}, count = 1) => {
for (let i = 0; i < count; i++) {
cy.get('body').trigger('keydown', triggerKeys);
}
});

/**
* Intercept all requests and return
* @param {array} intercepts - Array of intercepts to return
* return {array} - Array of intercepted request strings
* return {string} - Intercepted request string
*/
Cypress.Commands.add('interceptAllRequests', (method = '/GET/POST/PUT/PATCH/', urls = ['/v1/*']) => {
const interceptedUrls: string[] = urls.map((cUrl, i) => {
cy.intercept(method, cUrl).as(`interceptAllRequests${ i }`);

return `@interceptAllRequests${ i }`;
});

return cy.wrap(interceptedUrls);
});

Cypress.Commands.add('iFrame', () => {
return cy
.get('[data-testid="ember-iframe"]', { log: false })
.its('0.contentDocument.body', { log: false })
.should('not.be.empty')
.then((body) => cy.wrap(body));
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { LoginPagePo } from '@/cypress/e2e/po/pages/login-page.po';
import { Matcher } from '@/cypress/support/types';
import { CreateUserParams } from '@/cypress/globals';

// This file contains commands which makes API requests to the rancher API.
// It includes the `login` command to store the `token` to use

let token: any;

/**
Expand Down Expand Up @@ -164,7 +166,7 @@ Cypress.Commands.add('setClusterRoleBinding', (clusterId, userPrincipalId, role)
*
*/
Cypress.Commands.add('setProjectRoleBinding', (clusterId, userPrincipalId, projectName, role) => {
return cy.getProject(clusterId, projectName)
return cy.getProjectByName(clusterId, projectName)
.then((project: any) => cy.request({
method: 'POST',
url: `${ Cypress.env('api') }/v3/projectroletemplatebindings`,
Expand All @@ -187,7 +189,7 @@ Cypress.Commands.add('setProjectRoleBinding', (clusterId, userPrincipalId, proje
/**
* Get the project with the given name
*/
Cypress.Commands.add('getProject', (clusterId, projectName) => {
Cypress.Commands.add('getProjectByName', (clusterId, projectName) => {
return cy.request({
method: 'GET',
url: `${ Cypress.env('api') }/v3/projects?name=${ projectName }&clusterId=${ clusterId }`,
Expand All @@ -204,27 +206,6 @@ Cypress.Commands.add('getProject', (clusterId, projectName) => {
});
});

/**
* Get input field for given label
*/
Cypress.Commands.add('byLabel', (label) => {
return cy.get('.labeled-input').contains(label).siblings('input');
});

/**
* Wrap the cy.find() command to simplify the selector declaration of the data-testid
*/
Cypress.Commands.add('findId', (id: string, matcher?: Matcher = '') => {
return cy.find(`[data-testid${ matcher }="${ id }"]`);
});

/**
* Wrap the cy.get() command to simplify the selector declaration of the data-testid
*/
Cypress.Commands.add('getId', (id: string, matcher?: Matcher = '') => {
return cy.get(`[data-testid${ matcher }="${ id }"]`);
});

/**
* Override user preferences to default values, allowing to pass custom preferences for a deterministic scenario
*/
Expand Down Expand Up @@ -271,32 +252,41 @@ Cypress.Commands.add('requestBase64Image', (url: string) => {
});
});

Cypress.Commands.add('keyboardControls', (triggerKeys: any = {}, count = 1) => {
for (let i = 0; i < count; i++) {
cy.get('body').trigger('keydown', triggerKeys);
}
});

/**
* Intercept all requests and return
* @param {array} intercepts - Array of intercepts to return
* return {array} - Array of intercepted request strings
* return {string} - Intercepted request string
* Get a v3 / v1 resource
*/
Cypress.Commands.add('interceptAllRequests', (method = '/GET/POST/PUT/PATCH/', urls = ['/v1/*']) => {
const interceptedUrls: string[] = urls.map((cUrl, i) => {
cy.intercept(method, cUrl).as(`interceptAllRequests${ i }`);

return `@interceptAllRequests${ i }`;
});
Cypress.Commands.add('getRancherResource', (prefix, resourceType, resourceId, expectedStatusCode = 200) => {
return cy.request({
method: 'GET',
url: `${ Cypress.env('api') }/${ prefix }/${ resourceType }/${ resourceId }`,
headers: {
'x-api-csrf': token.value,
Accept: 'application/json'
},
})
.then((resp) => {
if (expectedStatusCode) {
expect(resp.status).to.eq(expectedStatusCode);
}

return cy.wrap(interceptedUrls);
return resp;
});
});

Cypress.Commands.add('iFrame', () => {
return cy
.get('[data-testid="ember-iframe"]', { log: false })
.its('0.contentDocument.body', { log: false })
.should('not.be.empty')
.then((body) => cy.wrap(body));
/**
* set a v3 / v1 resource
*/
Cypress.Commands.add('setRancherResource', (prefix, resourceType, resourceId, body) => {
return cy.request({
method: 'PUT',
url: `${ Cypress.env('api') }/${ prefix }/${ resourceType }/${ resourceId }`,
headers: {
'x-api-csrf': token.value,
Accept: 'application/json'
},
body
})
.then((resp) => {
expect(resp.status).to.eq(200);
});
});
3 changes: 2 additions & 1 deletion cypress/support/e2e.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import '@cypress/code-coverage/support';
import './commands';
import './commands/commands';
import './commands/rancher-api-commands.ts';
import registerCypressGrep from '@cypress/grep/src/support';

registerCypressGrep();
Expand Down

0 comments on commit ff065e5

Please sign in to comment.