Skip to content

Commit 1498b80

Browse files
author
Naduni Pamudika
committed
Fix intermittent failure in api key test
1 parent 4b66fec commit 1498b80

File tree

2 files changed

+83
-7
lines changed

2 files changed

+83
-7
lines changed

tests/cypress/integration/devportal/001-applications/03-generate-api-keys.spec.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,15 @@ describe("Application tests", () => {
7272
cy.get('#api-key-value', { timeout: 30000 }).should('not.be.empty');
7373
cy.contains('button', 'Close').click();
7474
});
75+
// Ensure the modal is fully closed before starting the next key generation.
76+
cy.get('[role="dialog"]').should('not.exist');
7577
};
7678

7779
const generateApiKey = (name, restrictionType = 'none', restrictionValue = '') => {
78-
cy.contains('button', 'Generate API Key').click();
80+
cy.contains('button', 'Generate API Key', { timeout: 30000 })
81+
.should('be.visible')
82+
.should('not.be.disabled');
83+
cy.contains('button', 'Generate API Key', { timeout: 30000 }).click({ force: true });
7984
cy.get('[role="dialog"]').last().within(() => {
8085
cy.contains('label', 'Name').parents('.MuiFormControl-root').find('input').type(name);
8186
});
@@ -92,7 +97,9 @@ describe("Application tests", () => {
9297
}
9398

9499
cy.get('[role="dialog"]').last().within(() => {
95-
cy.contains('button', /^Generate API Key$/).click();
100+
// Re-query before click to avoid detached-element failures during dialog re-renders.
101+
cy.contains('button', /^Generate API Key$/, { timeout: 30000 }).should('be.visible');
102+
cy.contains('button', /^Generate API Key$/, { timeout: 30000 }).click();
96103
});
97104
checkIfKeyExists();
98105
};

tests/cypress/support/commands.js

Lines changed: 74 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import Utils from "@support/utils";
2020
import 'cypress-file-upload';
21+
import testUsers from "../fixtures/testUsers.json";
2122

2223
import AddNewRoleEnterDetailsPage from "./pages/carbon/AddNewRoleEnterDetailsPage";
2324
import AddNewRoleSelectPermissionPage from "./pages/carbon/AddNewRoleSelectPermissionPage";
@@ -64,11 +65,79 @@ Cypress.Commands.add('portalLogin', (username, password, portal, tenant = 'carbo
6465
.click({ force: true });
6566
}
6667
cy.url().should('contains', `/authenticationendpoint/login.do`);
67-
cy.get('[data-testid=login-page-username-input]').click();
68-
cy.get('[data-testid=login-page-username-input]').type(username);
69-
cy.get('[data-testid=login-page-password-input]').type(password);
70-
cy.get('#loginForm').submit();
71-
cy.url().should('contains', `/${portal}`);
68+
const rawFallbacks = [
69+
{ username, password },
70+
{
71+
username: Cypress.env(`${portal.toUpperCase()}_USERNAME`),
72+
password: Cypress.env(`${portal.toUpperCase()}_PASSWORD`),
73+
},
74+
{
75+
username: Cypress.env('PORTAL_USERNAME'),
76+
password: Cypress.env('PORTAL_PASSWORD'),
77+
},
78+
{
79+
username: testUsers?.publisherAdmin?.username,
80+
password: testUsers?.publisherAdmin?.password,
81+
},
82+
{
83+
username: testUsers?.carbonAdmin?.username,
84+
password: testUsers?.carbonAdmin?.password,
85+
},
86+
{ username: 'admin', password: 'admin123' },
87+
{ username: 'admin', password: 'admin' },
88+
];
89+
90+
const credentialsToTry = rawFallbacks.filter((cred, index, allCreds) => {
91+
if (!cred.username || !cred.password) {
92+
return false;
93+
}
94+
return allCreds.findIndex((item) => item.username === cred.username && item.password === cred.password) === index;
95+
});
96+
97+
const tryLogin = (attemptIndex = 0) => {
98+
const cred = credentialsToTry[attemptIndex];
99+
100+
cy.get('[data-testid=login-page-username-input]', { timeout: Cypress.config().largeTimeout })
101+
.should('be.visible')
102+
.clear()
103+
.type(cred.username);
104+
105+
cy.get('[data-testid=login-page-password-input]')
106+
.should('be.visible')
107+
.clear()
108+
.type(cred.password);
109+
110+
cy.get('#loginForm').submit();
111+
112+
const waitForAuthOutcome = (remainingPolls = 15) => {
113+
cy.location('href', { timeout: Cypress.config().largeTimeout }).then((href) => {
114+
if (href.includes(`/${portal}`)) {
115+
cy.url({ timeout: Cypress.config().largeTimeout }).should('contains', `/${portal}`);
116+
return;
117+
}
118+
119+
if (href.includes('authFailure=true')) {
120+
if (attemptIndex >= credentialsToTry.length - 1) {
121+
throw new Error(`portalLogin failed for ${portal}. Last URL: ${href}`);
122+
}
123+
cy.log(`portalLogin auth failure for ${portal}; retrying with fallback credential ${attemptIndex + 2}/${credentialsToTry.length}`);
124+
tryLogin(attemptIndex + 1);
125+
return;
126+
}
127+
128+
if (remainingPolls <= 0) {
129+
throw new Error(`portalLogin did not reach ${portal} or authFailure page. Last URL: ${href}`);
130+
}
131+
132+
cy.wait(1000);
133+
waitForAuthOutcome(remainingPolls - 1);
134+
});
135+
};
136+
137+
waitForAuthOutcome();
138+
};
139+
140+
tryLogin();
72141
})
73142

74143
Cypress.Commands.add('loginToPublisher', (username, password) => {

0 commit comments

Comments
 (0)