Skip to content

Commit a43db8c

Browse files
authored
Documentation updates for v12 (#4880)
* re-add websecurity, links to websecurity, and trade-offs guides * chore: revamp documentation around web security page * chore: update same-origin tradeoff to be new navigation rules, including our SD chart, to help paint users a clear picture with cy.origin * chore: link to the experimental modify obstructive third party code doc in web security from origin * chore: update Error Messages section to reflect allowing cross origin visiting * update best practices: visiting external sites * remove node 12 from installing cypress section * chore: update key differences to plug session and origin over programmatic login * chore: update with suggestions from code review * add okta/amazon guide links in trade-offs and update workarounds * feat: add cross origin testing guide * update image for command time out with visit * chore: readd legacy errors and add a Note section to explain that this is only for cypress v11 and under * chore: update suggestions from code review * chore: add suggestions from code review * fix: fix okta alert banner (needed a new line) * fix: broken image in error messages * chore: update error header for on link to address cypress-io/cypress-services#5040 (comment)
1 parent fa0e7a8 commit a43db8c

File tree

15 files changed

+795
-52
lines changed

15 files changed

+795
-52
lines changed
Loading

content/_data/sidebar.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,14 @@
324324
{
325325
"title": "Cross Browser Testing",
326326
"slug": "cross-browser-testing"
327+
},
328+
{
329+
"title": "Cross Origin Testing",
330+
"slug": "cross-origin-testing"
331+
},
332+
{
333+
"title": "Web Security",
334+
"slug": "web-security"
327335
}
328336
]
329337
},

content/api/commands/origin.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ patterns commonly found in framebusting. When using the `cy.origin()` command,
2222
the third party code may also need to be modified for framebusting techniques.
2323
This can be enabled by setting the
2424
[`experimentalModifyObstructiveThirdPartyCode`](/guides/references/experiments)
25-
flag to `true` in the Cypress configuration.
25+
flag to `true` in the Cypress configuration. More information about this
26+
experimental flag can be found on our
27+
[Web Security](/guides/guides/web-security#Modifying-Obstructive-Third-Party-Code)
28+
page.
2629

2730
</Alert>
2831

content/faq/questions/using-cypress-faq.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,8 @@ application under test runs in any way, so you can safely ignore this warning.
919919
The network traffic between Cypress and the backend server still happens via
920920
HTTPS.
921921

922+
See also the [Web Security](/guides/guides/web-security) guide.
923+
922924
## <Icon name="angle-right"></Icon> Is there an option to run Cypress in CI with Developer Tools open? We want to track network and console issues.
923925

924926
No. There is not currently a way to run Cypress in `cypress run` with Developer

content/guides/end-to-end-testing/okta-authentication.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ e2eSpecific: true
1212
- Programmatically authenticate with [Okta](https://okta.com) via a custom
1313
Cypress command
1414
- Adapting your [Okta](https://okta.com) application for programmatic
15-
authentication during testing </Alert>
15+
authentication during testing
16+
17+
</Alert>
1618

1719
<Alert type="warning">
1820

content/guides/guides/cross-browser-testing.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ browser engine), and Firefox.
1414
<strong class="alert-header">Web Security</strong>
1515

1616
Tests that require the
17-
[`chromeWebSecurity` configuration option to be disabled](/guides/references/configuration#Browser)
17+
[`chromeWebSecurity` configuration option to be disabled](/guides/guides/web-security#Disabling-Web-Security)
1818
may experience issues in non-Chromium based browsers.
1919

2020
</Alert>
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
---
2+
title: Cross Origin Testing
3+
---
4+
5+
<Alert type="info">
6+
7+
<strong class="alert-header"> Note </strong>
8+
9+
As of Cypress [v12.0.0](https://on.cypress.io/changelog#12-0-0), Cypress has the
10+
capability to visit multiple origins in a single test via the
11+
[cy.origin()](https://on.cypress.io/origin) command!
12+
13+
</Alert>
14+
15+
Cypress limits each test to visiting domains that share the same superdomain. If
16+
a navigation occurs that does not meet the same superdomain rule, the
17+
[`cy.origin()`](/api/commands/origin) command must be used to execute Cypress
18+
commands inside the newly navigated origin.
19+
20+
But what is same superdomain? It is actually very similar to that of same
21+
origin! Two URLs have the same origin if the protocol, port (if specified), and
22+
host match. Cypress automatically handles hosts of the same superdomain by
23+
injecting the
24+
[`document.domain`](https://developer.mozilla.org/en-US/docs/Web/API/Document/domain)
25+
property into the visited `text/html` pages. This is why navigations without the
26+
use of the [`cy.origin()`](/api/commands/origin) command are solely scope to the
27+
same superdomain.
28+
29+
We understand this is a bit complicated to understand, so we have built a nifty
30+
chart to help clarify the differences!
31+
32+
### Parts of a URL
33+
34+
```
35+
┌───────────────────────────────────────────────────────────────────────────────────────┐
36+
│ href │
37+
├──────────┬──┬─────────────────────────────────────┬───────────────────────────┬───────┤
38+
│ protocol │ │ host │ path │ hash │
39+
│ │ ├──────────────────────────────┬──────┼──────────┬────────────────┤ │
40+
│ │ │ hostname │ port │ pathname │ search │ │
41+
| | ├───────────┬──────────────────┤ │ │ │ │
42+
│ │ │ subdomain │ superdomain (sd) │ │ │ │ │
43+
| | ├───────────┼─────────┬────────┤ │ ├─┬──────────────┤ │
44+
│ │ │ │ domain │ TLD │ │ │ │ query │ │
45+
│ │ │ │ │ │ │ │ │ │ │
46+
" https: // sub . example . com : 8080 /p/a/t/h ? query=string #hash "
47+
│ │ │ │ │
48+
│ origin │ | │ │
49+
├─────────────┬───────────┬─────────────────────────┤ │ │ │
50+
│ (sd) origin │ │ (sd) origin │ │ │ │
51+
└─────────────┴───────────┴─────────────────────────┴──────────┴────────────────┴───────┘
52+
```
53+
54+
Given the URLs below, all have the same superdomain compared to
55+
`https://www.cypress.io`.
56+
57+
- `https://cypress.io`
58+
- `https://docs.cypress.io`
59+
- `https://example.cypress.io/commands/querying`
60+
61+
The URLs below, however, will have different superdomains/origins compared to
62+
`https://www.cypress.io`.
63+
64+
- `http://www.cypress.io` (Different protocol)
65+
- `https://docs.cypress.io:81` (Different port)
66+
- `https://www.auth0.com/` (Different host of different superdomain)
67+
68+
The `http://localhost` URLs differ if their ports are different. For example,
69+
the `http://localhost:3000` URL is considered to be a different origin from the
70+
`http://localhost:8080` URL.
71+
72+
The rules are:
73+
74+
- <Icon name="exclamation-triangle" color="red"></Icon> You **cannot**
75+
[visit](/api/commands/visit) two domains of different superdomains in the same
76+
test and continue to interact with the page without the use of the
77+
[`cy.origin()`](/api/commands/origin) command.
78+
- <Icon name="check-circle" color="green"></Icon> You **can**
79+
[visit](/api/commands/visit) two or more domains of different origin in
80+
**different** tests without needing [`cy.origin()`](/api/commands/origin).
81+
82+
For practical purposes, this means the following:
83+
84+
```javascript
85+
// This test will run without error
86+
it('navigates', () => {
87+
cy.visit('https://www.cypress.io')
88+
cy.visit('https://docs.cypress.io')
89+
cy.get('selector') // yup all good
90+
})
91+
```
92+
93+
```javascript
94+
// this will error because stackoverflow.com doesn't match the cypress.io superdomain
95+
it('navigates', () => {
96+
cy.visit('https://www.cypress.io')
97+
cy.visit('https://stackoverflow.com')
98+
cy.get('selector')
99+
})
100+
```
101+
102+
To fix the above cross-origin error, use `cy.origin()` to indicate which origin
103+
the sequential command should run against:
104+
105+
```javascript
106+
it('navigates', () => {
107+
cy.visit('https://www.cypress.io')
108+
cy.visit('https://stackoverflow.com')
109+
cy.origin('https://stackoverflow.com', () => {
110+
cy.get('selector') // yup all good
111+
})
112+
})
113+
```
114+
115+
```javascript
116+
it('navigates', () => {
117+
cy.visit('https://www.cypress.io')
118+
})
119+
120+
// split visiting different origin in another test
121+
it('navigates to new origin', () => {
122+
cy.visit('https://stackoverflow.com')
123+
cy.get('selector') // yup all good
124+
})
125+
```
126+
127+
This limitation exists because Cypress switches to the domain under each
128+
specific test when it runs. For more information on this, please see our Web
129+
Security page regarding
130+
[Different superdomain per test requires cy.origin command](/guides/guides/web-security#Different-superdomain-per-test-requires-cy-origin-command).
131+
132+
#### Other workarounds
133+
134+
There are other ways of testing the interaction between two superdomains. The
135+
browser has a natural security barrier called `origin policy` this means that
136+
state like `localStorage`, `cookies`, `service workers` and many other APIs are
137+
not shared between them anyways. Cypress does offer APIs around `localStorage`,
138+
`sessionStorage`, and `cookies` that are not limited to this restriction.
139+
140+
As a best practice, you should not visit or interact with a 3rd party service
141+
not under your control. However, there are exceptions! If your organization uses
142+
Single Sign On (SSO) or OAuth then you might involve a 3rd party service other
143+
than your superdomain, which can be safely tested with
144+
[`cy.origin()`](/api/commands/origin).
145+
146+
We've written several other guides specifically about handling this situation.
147+
148+
- [Best Practices: Visiting external sites](/guides/references/best-practices#Visiting-external-sites)
149+
- [Web Security: Common Workarounds](/guides/guides/web-security#Common-Workarounds)
150+
- [Recipes: Logging In - Single Sign On](/examples/examples/recipes#Logging-In)
151+
- [Guides: Amazon Cognito Authentication](/guides/end-to-end-testing/amazon-cognito-authentication)
152+
- [Guides: Okta Authentication](/guides/end-to-end-testing/okta-authentication)

0 commit comments

Comments
 (0)