@@ -7,12 +7,12 @@ 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) with [ Okta ] ( https://okta.com ) via a custom Cypress command
12
- - If needed, adapting your [ Okta] ( https://okta.com ) application for programmatic
13
- authentication during testing
14
-
15
- </Alert >
10
+ - Login to [ Okta ] ( https://okta.com ) through the UI with
11
+ [ ` cy.origin() ` ] ( /api/commands/origin )
12
+ - Programmatically authenticate with [ Okta] ( https://okta.com ) via a custom
13
+ Cypress command
14
+ - Adapting your [ Okta ] ( https://okta.com ) application for programmatic
15
+ authentication during testing </Alert >
16
16
17
17
<Alert type =" warning " >
18
18
@@ -28,11 +28,16 @@ other identity providers.
28
28
<strong class =" alert-header " >Authenticate by visiting a different domain with
29
29
[ ` cy.origin() ` ] ( /api/commands/origin ) </strong >
30
30
31
- Programmatic authentication for Okta is now considered a legacy recommendation.
32
- As of Cypress [ v12.0.0] ( https://on.cypress.io/changelog#12-0-0 ) , you can easily
31
+ Typically, logging in a user within your app by authenticating via a third-party
32
+ provider requires visiting a login page hosted on a different domain. Before
33
+ Cypress [ v12.0.0] ( https://on.cypress.io/changelog#12-0-0 ) , Cypress tests were
34
+ limited to visiting domains of the same origin, making programmatic login the
35
+ only option for authenticating users with a third-party API. As of Cypress
36
+ [ v12.0.0] ( https://on.cypress.io/changelog#12-0-0 ) , Cypress tests are no longer
37
+ limited to visiting domains of a single origin, meaning you can easily
33
38
authenticate to
34
39
[ Okta Universal Directory] ( https://www.okta.com/products/universal-directory/ )
35
- as Cypress tests are no longer limited to visiting domains of a single origin.
40
+ via the UI!
36
41
37
42
</Alert >
38
43
@@ -71,36 +76,34 @@ require('dotenv').config()
71
76
72
77
## Custom Command for Okta Authentication
73
78
74
- There are two ways you can authenticate to Okta
79
+ There are two ways you can authenticate to Okta:
75
80
76
81
- [ Login with cy.origin()] ( /guides/end-to-end-testing/okta-authentication#Login-with-cy-origin )
77
- - [ Legacy Programmatic Access] ( /guides/end-to-end-testing/okta-authentication#Programmatic-Login-Legacy )
82
+ - [ Programmatic Access] ( /guides/end-to-end-testing/okta-authentication#Programmatic-Login )
78
83
79
84
### Login with [ ` cy.origin() ` ] ( /api/commands/origin )
80
85
81
- Next, we'll write a command to perform a login to [ Okta] ( https://okta.com ) using
82
- [ ` cy.origin() ` ] ( /api/commands/origin ) to navigate to Okta, inputting user
83
- credentials, and signing in via OAuth to redirect back to our application, and
84
- caching the results with [ ` cy.session() ` ] ( /api/commands/session ) .
86
+ Next, we'll write a custom command called ` loginByOkta ` to perform a login to
87
+ [ Okta] ( https://okta.com ) . This command will use
88
+ [ ` cy.origin() ` ] ( /api/commands/origin ) to
85
89
86
- In this ` loginByOkta ` command, we redirect to the okta login screen via
87
- navigation guards. We then enter our user credentials and sign in, which
88
- redirects us back to the
89
- [ Cypress Real World App] ( https://github.com/cypress-io/cypress-realworld-app ) .
90
+ 1 . navigate to the Okta origin
91
+ 2 . input user credentials
92
+ 3 . sign in and redirect back to the
93
+ [ Cypress Real World App] ( https://github.com/cypress-io/cypress-realworld-app )
94
+ 4 . cache the results with [ ` cy.session() ` ] ( /api/commands/session )
90
95
91
96
``` jsx
92
97
// cypress/support/auth-provider-commands/okta.ts
93
98
// Okta
94
- Cypress . Commands . add ( ' loginByOkta ' , (username : string , password : string ) => {
99
+ const loginToOkta = (username : string , password : string ) => {
95
100
Cypress .log ({
96
101
displayName: ' OKTA LOGIN' ,
97
102
message: [` 🔐 Authenticating | ${ username} ` ],
98
- // @ts-ignore
99
103
autoEnd: false ,
100
104
})
101
105
102
106
cy .visit (' /' )
103
-
104
107
cy .origin (
105
108
Cypress .env (' okta_domain' ),
106
109
{ args: { username, password } },
@@ -114,6 +117,10 @@ Cypress.Commands.add('loginByOkta', (username: string, password: string) => {
114
117
)
115
118
116
119
cy .get (' [data-test="sidenav-username"]' ).should (' contain' , username)
120
+ }
121
+ // right now our custom command is light. More on this later!
122
+ Cypress .Commands .add (' loginByOkta' , (username : string , password : string ) => {
123
+ return loginToOkta (username, password)
117
124
})
118
125
```
119
126
@@ -133,7 +140,6 @@ is in the
133
140
describe (' Okta' , function () {
134
141
beforeEach (function () {
135
142
cy .task (' db:seed' )
136
-
137
143
cy .loginByOkta (Cypress .env (' okta_username' ), Cypress .env (' okta_password' ))
138
144
})
139
145
@@ -155,28 +161,7 @@ Cypress.Commands.add('loginByOkta', (username: string, password: string) => {
155
161
cy .session (
156
162
` okta-${ username} ` ,
157
163
() => {
158
- Cypress .log ({
159
- displayName: ' OKTA LOGIN' ,
160
- message: [` 🔐 Authenticating | ${ username} ` ],
161
- // @ts-ignore
162
- autoEnd: false ,
163
- })
164
-
165
- cy .visit (' /' )
166
-
167
- cy .origin (
168
- Cypress .env (' okta_domain' ),
169
- { args: { username, password } },
170
- ({ username, password }) => {
171
- cy .get (' input[name="identifier"]' ).type (username)
172
- cy .get (' input[name="credentials.passcode"]' ).type (password, {
173
- log: false ,
174
- })
175
- cy .get (' [type="submit"]' ).click ()
176
- }
177
- )
178
-
179
- cy .get (' [data-test="sidenav-username"]' ).should (' contain' , username)
164
+ return loginToOkta (username, password)
180
165
},
181
166
{
182
167
validate () {
@@ -190,12 +175,7 @@ Cypress.Commands.add('loginByOkta', (username: string, password: string) => {
190
175
191
176
<DocsVideo src =" /img/examples/okta-session-restore.mp4 " ></DocsVideo >
192
177
193
- Unlike programmatic login, authenticating with
194
- [ ` cy.origin() ` ] ( /api/commands/origin ) does not require adapting the application
195
- to work. It simply works without intervention exactly how your users would
196
- consume the application!
197
-
198
- ### Programmatic Login (Legacy)
178
+ ### Programmatic Login
199
179
200
180
Next, we will write a command named ` loginByOktaApi ` to perform a programmatic
201
181
login into [ Okta] ( https://okta.com ) and set an item in localStorage with the
@@ -297,7 +277,7 @@ is in the
297
277
298
278
</Alert >
299
279
300
- #### Adapting an Okta App for Testing
280
+ ### Adapting an Okta App for Testing
301
281
302
282
<Alert type =" info " >
303
283
@@ -307,6 +287,10 @@ The previous sections focused on the programmatic Okta authentication practice
307
287
within Cypress tests. To use this practice it is assumed you are testing an app
308
288
appropriately built or adapted to use Okta.
309
289
290
+ Unlike programmatic login, authenticating with
291
+ [ ` cy.origin() ` ] ( /api/commands/origin ) does not require adapting the application
292
+ to work. This step is only needed if implementing programmatic login.
293
+
310
294
The following sections provides guidance on building or adapting an app to use
311
295
Okta authentication.
312
296
0 commit comments