@@ -7,23 +7,27 @@ e2eSpecific: true
7
7
8
8
## <Icon name =" graduation-cap " ></Icon > What you'll learn
9
9
10
- - Authenticate with [ ` cy.origin() ` ] ( /api/commands/origin ) or programmatically
11
- (legacy) to [ Amazon Cognito ] ( https://aws.amazon.com/cognito ) via a custom
12
- Cypress command
13
- - If needed, adapting your [ Amazon Cognito] ( https://aws.amazon.com/cognito )
14
- application for programmatic authentication during testing
15
-
16
- </Alert >
10
+ - Login to [ Amazon Cognito ] ( https://aws.amazon.com/cognito ) through the UI with
11
+ [ ` cy.origin() ` ] ( /api/commands/origin )
12
+ - Programmatically authenticate with
13
+ [ Amazon Cognito] ( https://aws.amazon.com/cognito ) via a custom command Cypress
14
+ command
15
+ - Adapting your [ Amazon Cognito ] ( https://aws.amazon.com/cognito ) application for
16
+ programmatic authentication during testing </Alert >
17
17
18
18
<Alert type =" success " >
19
19
20
20
<strong class =" alert-header " >Authenticate by visiting a different domain with
21
21
[ ` cy.origin() ` ] ( /api/commands/origin ) </strong >
22
22
23
- Programmatic authentication for Cognito is now considered a legacy
24
- recommendation. As of Cypress [ v12.0.0] ( https://on.cypress.io/changelog#12-0-0 ) ,
25
- you can easily authenticate to federated AWS Cognito as Cypress tests are no
26
- longer limited to visiting domains of a single origin.
23
+ Typically, logging in a user within your app by authenticating via a third-party
24
+ provider requires visiting a login page hosted on a different domain. Before
25
+ Cypress [ v12.0.0] ( https://on.cypress.io/changelog#12-0-0 ) , Cypress tests were
26
+ limited to visiting domains of the same origin, making programmatic login the
27
+ only option for authenticating users with a third-party API. As of Cypress
28
+ [ v12.0.0] ( https://on.cypress.io/changelog#12-0-0 ) , Cypress tests are no longer
29
+ limited to visiting domains of a single origin, meaning you can easily
30
+ authenticate to federated AWS Cognito via the UI!
27
31
28
32
</Alert >
29
33
@@ -158,33 +162,30 @@ const awsConfig = require('./aws-exports-es5.js')
158
162
159
163
## Custom Command for Amazon Cognito Authentication
160
164
161
- There are two ways you can authenticate to AWS Cognito
165
+ There are two ways you can authenticate to AWS Cognito:
162
166
163
167
- [ Login with cy.origin()] ( /guides/end-to-end-testing/amazon-cognito-authentication#Login-with-cy-origin )
164
- - [ Legacy Programmatic Access] ( /guides/end-to-end-testing/amazon-cognito-authentication#Programmatic-Login-Legacy )
168
+ - [ Programmatic Access] ( /guides/end-to-end-testing/amazon-cognito-authentication#Programmatic-Login )
165
169
166
170
### Login with [ ` cy.origin() ` ] ( /api/commands/origin )
167
171
168
- Next, we'll write a command to perform a login to
169
- [ Amazon Cognito] ( https://aws.amazon.com/cognito ) using
170
- [ ` cy.origin() ` ] ( /api/commands/origin ) to navigate to the cognito origin,
171
- inputting a user credentials, and signing in via OAuth to redirect back to our
172
- application, and caching the results with
173
- [ ` cy.session() ` ] ( /api/commands/session ) .
172
+ Next, we'll write a custom command called ` loginByCognito ` to perform a login to
173
+ [ Amazon Cognito] ( https://aws.amazon.com/cognito ) . This command will use
174
+ [ ` cy.origin() ` ] ( /api/commands/origin ) to
174
175
175
- In this ` loginByCognito ` command, we redirect to the cognito login screen by
176
- authenticating with ` Sign in with AWS ` . We then enter our user credentials and
177
- sign in, which redirects us back to the realworld app.
176
+ 1 . navigate to the Cognito origin
177
+ 2 . input user credentials
178
+ 3 . sign in and redirect back to the
179
+ [ Cypress Real World App] ( https://github.com/cypress-io/cypress-realworld-app )
180
+ 4 . cache the results with [ ` cy.session() ` ] ( /api/commands/session )
178
181
179
182
``` jsx
180
183
// cypress/support/auth-provider-commands/cognito.ts
181
-
182
- // / Amazon Cognito
183
- Cypress .Commands .add (' loginByCognito' , (username , password ) => {
184
+ // Amazon Cognito
185
+ const loginToCognito = (username : string , password : string ) => {
184
186
Cypress .log ({
185
187
displayName: ' COGNITO LOGIN' ,
186
188
message: [` 🔐 Authenticating | ${ username} ` ],
187
- // @ts-ignore
188
189
autoEnd: false ,
189
190
})
190
191
@@ -202,21 +203,27 @@ Cypress.Commands.add('loginByCognito', (username, password) => {
202
203
},
203
204
},
204
205
({ username, password }) => {
205
- // cognito log in page has some elements of the same id but are off screen. we only want the visible elements to log in
206
+ // Cognito log in page has some elements of the same id but are off screen.
207
+ // We only want the visible elements to log in
206
208
cy .get (' input[name="username"]:visible' ).type (username)
207
209
cy .get (' input[name="password"]:visible' ).type (password, {
210
+ // use log: false to prevent your password from showing in the Command Log
208
211
log: false ,
209
212
})
210
213
cy .get (' input[name="signInSubmitButton"]:visible' ).click ()
211
214
}
212
215
)
213
216
214
217
// give a few seconds for redirect to settle
215
- // eslint-disable-next-line cypress/no-unnecessary-waiting
216
218
cy .wait (2000 )
217
219
218
220
// verify we have made it passed the login screen
219
221
cy .contains (' Get Started' ).should (' be.visible' )
222
+ }
223
+
224
+ // right now our custom command is light. More on this later!
225
+ Cypress .Commands .add (' loginByCognito' , (username , password ) => {
226
+ return loginToCognito (username, password)
220
227
})
221
228
```
222
229
@@ -259,47 +266,13 @@ Lastly, we can refactor our login command to take advantage of
259
266
have to reauthenticate with everything test.
260
267
261
268
``` jsx
269
+ // cypress/support/auth-provider-commands/cognito.ts
262
270
// Amazon Cognito
263
271
Cypress .Commands .add (' loginByCognito' , (username , password ) => {
264
272
cy .session (
265
273
` cognito-${ username} ` ,
266
274
() => {
267
- Cypress .log ({
268
- displayName: ' COGNITO LOGIN' ,
269
- message: [` 🔐 Authenticating | ${ username} ` ],
270
- // @ts-ignore
271
- autoEnd: false ,
272
- })
273
-
274
- cy .visit (' /' )
275
- cy .contains (' Sign in with AWS' , {
276
- includeShadowDom: true ,
277
- }).click ()
278
-
279
- cy .origin (
280
- Cypress .env (' cognito_domain' ),
281
- {
282
- args: {
283
- username,
284
- password,
285
- },
286
- },
287
- ({ username, password }) => {
288
- // cognito log in page has some elements of the same id but are off screen. we only want the visible elements to log in
289
- cy .get (' input[name="username"]:visible' ).type (username)
290
- cy .get (' input[name="password"]:visible' ).type (password, {
291
- log: false ,
292
- })
293
- cy .get (' input[name="signInSubmitButton"]:visible' ).click ()
294
- }
295
- )
296
-
297
- // give a few seconds for redirect to settle
298
- // eslint-disable-next-line cypress/no-unnecessary-waiting
299
- cy .wait (2000 )
300
-
301
- // verify we have made it passed the login screen
302
- cy .contains (' Get Started' ).should (' be.visible' )
275
+ return loginToCognito (username, password)
303
276
},
304
277
{
305
278
validate () {
@@ -314,12 +287,7 @@ Cypress.Commands.add('loginByCognito', (username, password) => {
314
287
315
288
<DocsVideo src =" /img/examples/cognito-session-restore.mp4 " ></DocsVideo >
316
289
317
- Unlike programmatic login, authenticating with
318
- [ ` cy.origin() ` ] ( /api/commands/origin ) does not require adapting the application
319
- to work. It simply works without intervention exactly how your users would
320
- consume the application!
321
-
322
- ### Programmatic Login (Legacy)
290
+ ### Programmatic Login
323
291
324
292
Next, we'll write a command to perform a programmatic login into
325
293
[ Amazon Cognito] ( https://aws.amazon.com/cognito ) and set items in localStorage
@@ -419,7 +387,17 @@ describe('Cognito', function () {
419
387
})
420
388
```
421
389
422
- #### Adapting an Amazon Cognito App for Testing
390
+ ### Adapting an Amazon Cognito App for Testing
391
+
392
+ <Alert type =" info " >
393
+
394
+ <strong class =" alert-header " >Programmatic Login</strong >
395
+
396
+ Unlike programmatic login, authenticating with
397
+ [ ` cy.origin() ` ] ( /api/commands/origin ) does not require adapting the application
398
+ to work. This step is only needed if implementing programmatic login.
399
+
400
+ </Alert >
423
401
424
402
The
425
403
[ Cypress Real World App] ( https://github.com/cypress-io/cypress-realworld-app ) is
0 commit comments