Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation updates for v12 #4880

Merged
merged 20 commits into from
Dec 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
9c0d2ce
re-add websecurity, links to websecurity, and trade-offs guides
AtofStryker Nov 23, 2022
72a55a3
chore: revamp documentation around web security page
AtofStryker Nov 28, 2022
0af2761
chore: update same-origin tradeoff to be new navigation rules, includ…
AtofStryker Nov 28, 2022
efb1390
chore: link to the experimental modify obstructive third party code d…
AtofStryker Nov 28, 2022
6615dcd
chore: update Error Messages section to reflect allowing cross origin…
AtofStryker Nov 29, 2022
5df7fdd
update best practices: visiting external sites
AtofStryker Nov 29, 2022
356853d
remove node 12 from installing cypress section
AtofStryker Nov 29, 2022
d24098b
chore: update key differences to plug session and origin over program…
AtofStryker Nov 29, 2022
757e6b3
Merge branch 'v12' of github.com:cypress-io/cypress-documentation int…
AtofStryker Nov 29, 2022
c71644c
chore: update with suggestions from code review
AtofStryker Dec 1, 2022
b725472
add okta/amazon guide links in trade-offs and update workarounds
AtofStryker Dec 1, 2022
b3c5c6d
feat: add cross origin testing guide
AtofStryker Dec 1, 2022
65db437
Merge branch 'v12' of github.com:cypress-io/cypress-documentation int…
AtofStryker Dec 2, 2022
5ed169d
update image for command time out with visit
AtofStryker Dec 2, 2022
2080412
chore: readd legacy errors and add a Note section to explain that thi…
AtofStryker Dec 2, 2022
a1c9e8b
chore: update suggestions from code review
AtofStryker Dec 2, 2022
9d132e9
chore: add suggestions from code review
AtofStryker Dec 2, 2022
9381771
fix: fix okta alert banner (needed a new line)
AtofStryker Dec 2, 2022
ee157c7
fix: broken image in error messages
AtofStryker Dec 2, 2022
ae995ec
chore: update error header for on link to address https://github.com/…
AtofStryker Dec 2, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions content/_data/sidebar.json
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,14 @@
{
"title": "Cross Browser Testing",
"slug": "cross-browser-testing"
},
{
"title": "Cross Origin Testing",
"slug": "cross-origin-testing"
},
{
"title": "Web Security",
"slug": "web-security"
}
]
},
Expand Down
5 changes: 4 additions & 1 deletion content/api/commands/origin.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ patterns commonly found in framebusting. When using the `cy.origin()` command,
the third party code may also need to be modified for framebusting techniques.
This can be enabled by setting the
[`experimentalModifyObstructiveThirdPartyCode`](/guides/references/experiments)
flag to `true` in the Cypress configuration.
flag to `true` in the Cypress configuration. More information about this
experimental flag can be found on our
[Web Security](/guides/guides/web-security#Modifying-Obstructive-Third-Party-Code)
page.

</Alert>

Expand Down
2 changes: 2 additions & 0 deletions content/faq/questions/using-cypress-faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,8 @@ application under test runs in any way, so you can safely ignore this warning.
The network traffic between Cypress and the backend server still happens via
HTTPS.

See also the [Web Security](/guides/guides/web-security) guide.

## <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.

No. There is not currently a way to run Cypress in `cypress run` with Developer
Expand Down
4 changes: 3 additions & 1 deletion content/guides/end-to-end-testing/okta-authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ e2eSpecific: true
- Programmatically authenticate with [Okta](https://okta.com) via a custom
Cypress command
- Adapting your [Okta](https://okta.com) application for programmatic
authentication during testing </Alert>
authentication during testing

</Alert>

<Alert type="warning">

Expand Down
2 changes: 1 addition & 1 deletion content/guides/guides/cross-browser-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ browser engine), and Firefox.
<strong class="alert-header">Web Security</strong>

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

</Alert>
Expand Down
152 changes: 152 additions & 0 deletions content/guides/guides/cross-origin-testing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
---
title: Cross Origin Testing
---

<Alert type="info">

<strong class="alert-header"> Note </strong>

As of Cypress [v12.0.0](https://on.cypress.io/changelog#12-0-0), Cypress has the
capability to visit multiple origins in a single test via the
[cy.origin()](https://on.cypress.io/origin) command!

</Alert>

Cypress limits each test to visiting domains that share the same superdomain. If
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cypress limits each test to visiting domains that share the same superdomain

This statement feels like it counters the above statement

As of Cypress v12.0.0, Cypress has the
capability to visit multiple origins in a single test

I assume you're trying to something like: "by default, cypress runs the commands in a test agains a single origin".
(or is is super domain? this always confuses me since its cy.origin not cy.superdomain)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's confusing, but it is superdomain. Once its outside of same superdomain, you have to use origin(), which we want an exact match of the said origin and not the origins superdomain. Not exactly easy to digest, but I think what we have gives a pretty good summary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without the full context I think it does counter it, but the next sentence If a navigation occurs that does not meet the same superdomain rule, the [cy.origin](/api/commands/origin) command must be used to execute Cypress commands inside the newly navigated origin. I feel like addresses that explanation

a navigation occurs that does not meet the same superdomain rule, the
[`cy.origin()`](/api/commands/origin) command must be used to execute Cypress
commands inside the newly navigated origin.

But what is same superdomain? It is actually very similar to that of same
origin! Two URLs have the same origin if the protocol, port (if specified), and
host match. Cypress automatically handles hosts of the same superdomain by
injecting the
[`document.domain`](https://developer.mozilla.org/en-US/docs/Web/API/Document/domain)
property into the visited `text/html` pages. This is why navigations without the
use of the [`cy.origin()`](/api/commands/origin) command are solely scope to the
same superdomain.

We understand this is a bit complicated to understand, so we have built a nifty
chart to help clarify the differences!

### Parts of a URL

```
┌───────────────────────────────────────────────────────────────────────────────────────┐
│ href │
├──────────┬──┬─────────────────────────────────────┬───────────────────────────┬───────┤
│ protocol │ │ host │ path │ hash │
│ │ ├──────────────────────────────┬──────┼──────────┬────────────────┤ │
│ │ │ hostname │ port │ pathname │ search │ │
| | ├───────────┬──────────────────┤ │ │ │ │
│ │ │ subdomain │ superdomain (sd) │ │ │ │ │
| | ├───────────┼─────────┬────────┤ │ ├─┬──────────────┤ │
│ │ │ │ domain │ TLD │ │ │ │ query │ │
│ │ │ │ │ │ │ │ │ │ │
" https: // sub . example . com : 8080 /p/a/t/h ? query=string #hash "
│ │ │ │ │
│ origin │ | │ │
├─────────────┬───────────┬─────────────────────────┤ │ │ │
│ (sd) origin │ │ (sd) origin │ │ │ │
└─────────────┴───────────┴─────────────────────────┴──────────┴────────────────┴───────┘
```

Given the URLs below, all have the same superdomain compared to
`https://www.cypress.io`.

- `https://cypress.io`
- `https://docs.cypress.io`
- `https://example.cypress.io/commands/querying`

The URLs below, however, will have different superdomains/origins compared to
`https://www.cypress.io`.

- `http://www.cypress.io` (Different protocol)
- `https://docs.cypress.io:81` (Different port)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the default port when one isn't explicitly called out in a url? is there one?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

usually 80

- `https://www.auth0.com/` (Different host of different superdomain)

The `http://localhost` URLs differ if their ports are different. For example,
the `http://localhost:3000` URL is considered to be a different origin from the
`http://localhost:8080` URL.

The rules are:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe When to use cy.origin()?
I thought this was rules of what made superdomain

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rules should still apply even without cy.origin()


- <Icon name="exclamation-triangle" color="red"></Icon> You **cannot**
[visit](/api/commands/visit) two domains of different superdomains in the same
test and continue to interact with the page without the use of the
[`cy.origin()`](/api/commands/origin) command.
- <Icon name="check-circle" color="green"></Icon> You **can**
[visit](/api/commands/visit) two or more domains of different origin in
**different** tests without needing [`cy.origin()`](/api/commands/origin).

For practical purposes, this means the following:

```javascript
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test will run without error:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed in a1c9e8b

// This test will run without error
it('navigates', () => {
cy.visit('https://www.cypress.io')
cy.visit('https://docs.cypress.io')
cy.get('selector') // yup all good
})
```

```javascript
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The following test changes super-domain mid-test and results in an error after navigation:

// this will error because stackoverflow.com doesn't match the cypress.io superdomain
it('navigates', () => {
cy.visit('https://www.cypress.io')
cy.visit('https://stackoverflow.com')
cy.get('selector')
})
```

To fix the above cross-origin error, use `cy.origin()` to indicate which origin
the sequential command should run against:

```javascript
it('navigates', () => {
cy.visit('https://www.cypress.io')
cy.visit('https://stackoverflow.com')
cy.origin('https://stackoverflow.com', () => {
cy.get('selector') // yup all good
})
})
Comment on lines +105 to +112
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
```javascript
it('navigates', () => {
cy.visit('https://www.cypress.io')
cy.visit('https://stackoverflow.com')
cy.origin('https://stackoverflow.com', () => {
cy.get('selector') // yup all good
})
})
To fix the above cross-origin error, use `cy.origin()` to indicate which origin the sequential command should run against:
```diff
it('navigates', () => {
cy.visit('https://www.cypress.io')
cy.visit('https://stackoverflow.com')
+ cy.origin('https://stackoverflow.com', () => {
cy.get('selector') // yup all good
+ })
})

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed in a1c9e8b. I didn't implement the diff because the contract looks strange next to the other javascript snippets, but added the explainer

```

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should move this passing example to be the second example?

The following tests will success without issue because each test visits a unique superdomain:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so. I don't think this is a pattern we usually want to encourage, but we want to showcase the exception and let users see if its applicable to their use case or not

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess not so much discourage but just not common I think

```javascript
it('navigates', () => {
cy.visit('https://www.cypress.io')
})

// split visiting different origin in another test
it('navigates to new origin', () => {
cy.visit('https://stackoverflow.com')
cy.get('selector') // yup all good
})
```

This limitation exists because Cypress switches to the domain under each
specific test when it runs. For more information on this, please see our Web
Security page regarding
[Different superdomain per test requires cy.origin command](/guides/guides/web-security#Different-superdomain-per-test-requires-cy-origin-command).

#### Other workarounds

There are other ways of testing the interaction between two superdomains. The
browser has a natural security barrier called `origin policy` this means that
state like `localStorage`, `cookies`, `service workers` and many other APIs are
not shared between them anyways. Cypress does offer APIs around `localStorage`,
`sessionStorage`, and `cookies` that are not limited to this restriction.

As a best practice, you should not visit or interact with a 3rd party service
not under your control. However, there are exceptions! If your organization uses
Single Sign On (SSO) or OAuth then you might involve a 3rd party service other
than your superdomain, which can be safely tested with
[`cy.origin()`](/api/commands/origin).

We've written several other guides specifically about handling this situation.

- [Best Practices: Visiting external sites](/guides/references/best-practices#Visiting-external-sites)
- [Web Security: Common Workarounds](/guides/guides/web-security#Common-Workarounds)
- [Recipes: Logging In - Single Sign On](/examples/examples/recipes#Logging-In)
- [Guides: Amazon Cognito Authentication](/guides/end-to-end-testing/amazon-cognito-authentication)
- [Guides: Okta Authentication](/guides/end-to-end-testing/okta-authentication)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add Auth0 guide here plz

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep I'll do a follow up to address these comments!

Loading