-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Enable Lifecycle Events to be customizable #686
Comments
One case where tests need to build up state between them is when each test is a step of a cucumber scenario. In that case, each test depends on the previous steps of the same scenario. I'm using cypress-cucumber-preprocessor but I'm currently stuck because of this (I have opened badeball/cypress-cucumber-preprocessor#43 about this). With the feaure discussed in this issues, it would be doable for the preprocessor to manage the state clearing in such a way that state would be cleared in between scenarios but not between steps of the same scenario. I'm guessing this is still a long way down the road, but do you have an estimate on when you may be tackling this ? |
In the meantime, if I wanted to experiment, can anyone point me to the direction of the function(s) or pieces of code that do the clearing ? I'd like to see if this can't be solved in the preprocessor itself. |
@iansjk 😂 I have no idea what that was meant to say. The original seems to reflect this same wording, but obviously this was missing some clause. The Sessions API work is coming along and viewable here: #14350. The WIP documentation is here if you want to see what it enables you do test: cypress-io/cypress-documentation#3427 |
Just arrived here searching for a way for Cypress to cache images and js files to speed up my tests. |
looking at the conversations from start to end .. doesn't seem to have a solution or workaround even after this has been discussed for a while :-( |
Any workaround available to stop cypress clearing localstorage between tests? |
The workaround is easy but ugly. Only have one describe() => it() per spec test. You can use cy.log(message) to achieve a similar feel in the logs. Not ideal but gets around this problem. |
@timini If you to want get rid of this functionality entirely you can use the following lines in Cypress' index.js file
This overwrites the function before your test runs. |
Is there anyway I can delete browser data between test cases? This is not working:
This also are not working:
|
I would really love to see an option to have cache cleared on every test re-run. Not having it is hindering use of Cypress as TDD tool. If for example I'm working on a component which needs to |
Any solution? |
We released The default behavior when using experimentalSessionSupport will also be to clear the entire state between tests, visiting If you have any feedback about |
Is there a way to avoid clearing sessionStorage between each test? This is extremely cumbersome in our e2e suite. It would save a lot of time to not clear the session vs clearing and restoring it with |
What are the recommended steps to stop a request spilling into the next test? This has caused me a lot of headaches particularly when tests run on CircleCI. The big issue is that I can never replicate it with Cypress open and only very few times with Cypress run in my local terminal. Example: If at the end of it("test1"), i send a request to write "apple" into Textbox1. Then in it("test2"), the request can sometimes spill in here so I find that the Textbox contains "apple" before I even start to do anything with this test. The way I have to currently get around it is by waiting at the end of every test for the request to be sent (and response comes back) before moving onto the next it("test2") test. Any suggestions are really appreciated :) |
… On Thu, Nov 25, 2021 at 12:22 PM Hamzah ***@***.***> wrote:
What are the recommended steps to stop a request spilling into the next
test? This has caused me a lot of headaches particularly when tests run on
CircleCI. The big issue is that I can never replicate it with Cypress open
and only very few times with Cypress run in my local terminal.
Example: If at the end of it("test1"), i send a request to write "apple"
into Textbox1. Then in it("test2"), the request can sometimes spill in here
so I find that the Textbox contains "apple" before I even start to do
anything with this test.
The way I have to currently get around it is by waiting at the end of
every test for the request to be sent (and response comes back) before
moving onto the next it("test2") test.
Any suggestions are really appreciated :)
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#686 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAQ4BJVIV6PW3M6UTXDOVNTUNZWF3ANCNFSM4D4GU3FQ>
.
--
Dr. Gleb Bahmutov, PhD
Schedule video chat / phone call / meeting with me via
https://calendly.com/bahmutov
***@***.*** @bahmutov ***@***.***>
https://glebbahmutov.com/ https://glebbahmutov.com/blog
https://github.com/bahmutov
|
That's a really cool approach. Thank you for the time you have taken demonstrate the issue and the steps to solve it in the blog! |
Like @dwilches et al, we would very much like to see the possibility to disable the browser cache clearing between each invocation of |
Hello, at this stage, is it possible to clear browser history using Cypress? This question is related to my now closed ticket here: #8219. Thanks! |
@jennifer-shehane @brian-mann is there any progress on that topic? I'm mostly interested in |
I am having issue #1055 , workaround is not working afterEach(() => { Framework has login helper which is used throughout framework & multiple describe /context blocks per spec file. Any other workarounds ?? Thanks |
Closing this issue as done for what we are wanting to address at this time and tagging for the v12.0.0 release. The main idea this issue captured was the idea of providing clearer insights into what Cypress handles for users between tests to ensure users write independent tests. In v12.0.0 (soon-to-be-released), we are introducing the concept of Test Isolation. There will be two modes of test isolation, When test isolation is
This will prevent DOM and browser state bleed over between tests. The new If it is desired to persist DOM and browser state between a set of tests, you will be able to turn test isolation Also, in the upcoming v12.0.0 release, we are removing the old The ideas outlined this issue that we are not current interested in implementing are:
|
Released in This comment thread has been locked. If you are still experiencing this issue after upgrading to |
Why this is necessary
Cypress automatically executes code in "between" tests. The idea behind this is that Cypress will try to "reset the state" and enable each test to run in its own pure state without other tests affecting or leaking into one another.
Currently it does the following:
localStorage
sessionStorage
cookies
Currently what it does not do:
Why this needs to change
It really makes no sense the way we currently handle this. This is often a source of confusion we've seen from multiple users.
For instance it is:
Because we don't forcibly clear the application state it means that it "appears" nothing has changed between the tests, but under the hood everything that built up the current session was reset.
This often results with users clicking links only to find out they've been magically "logged out".
In addition to sessions being cleared, it is actually technically possible for your application to do things in the tiniest briefest moment that Cypress finishes running one test and "switches" to the new test. Since your application is still active in the window, that means it will continue to run everything it's built to run.
This can cause all kinds of havoc.
Here's a sequence of events that demonstrates the problem:
cy.route
to route XHR'sonerror
handler of your XHR bubbles up and changes your application's stateThe reason we don't hear every user complain about this - is that for one its incredibly rare for this to happen and represents a "perfect storm" of events.
Additionally when we originally built these lifecycle events, they were with the assumption that most users would be visiting their applications before each test.
When visiting before each test (and thus always recreating / rebuilding the sessions and state), you insulate yourself from all of these problems.
What users really want to do
With that said many of our users really want to do one thing: login once in a
before
hook during the first test, and then have all subsequent tests run with this session.While we don't really officially endorse this as the best pattern - it often makes sense in these situations:
Another example users have suggested is when you apply a "unit testing" pattern to integration tests and write a bunch of "tiny" tests with a single assertion. However, we consider this an anti-pattern.
Irrespective of what we believe - the bottom line is that only logging in once before your tests will without a doubt enable all of your tests to run faster. If they have less to do in each test, they will simply complete faster.
Therefore it's imperative that we adequately solve this use case, and in the documentation and guides we provide resources / suggestions / hints / tips / tricks / philosophy to guide you to making these choices.
BTW in case you're curious we believe most of the need for a single login in a
before
hook is mitigated completely by using programatic API's likecy.request
for things like setting state and logging in. By avoiding your UI you skip most of the reasons tests are slow.One thing we absolutely do not endorse and will never support is writing testsEdited by Jennifer Shehane: I have no idea what this was meant to originally sayChanges that need to be made
Clear the App
Enforce clearing the Application at the end of test if it is not the last test but before the next test runs any of its code.
In other words, unless this is the very last test, the moment test code finishes and before any async code runs we MUST blow the application out of the window so no async events, polls, timers, promises, etc, run.
This will 100% prevent application leakage in "between" the tests and ensure the application is torn down properly.
Update the GUI
Display a new "instrument section" that lists each lifecycle event that was applied (or skipped).
Create new commnad
We'll need a new
cy.clearApp()
command which brings into parity the othercy.clear*
commands.This enables you to issue it within a test as a "one-off" command the same way the others work.
Remove old API's
Deprecate / remove the
Cypress.Cookies.preserveOnce
API's since that would be superseded by these Lifecycle events.Other Considerations
Having first class lifecycle events with their own programatic API also means we could turn these completely off when writing unit tests. As it stands Cypress automatically runs the clearing code in between each test. These are asynchronous and involve talking to the automation servers (in the browser and server) and therefore are quite slow (in terms of clock cycles).
Examples of how we could do this
I don't like the idea of making this global configuration in
cypress.json
. Instead I think we should create API's that enable you to control this in code.Since Cypress has to know how you intend to apply lifecycle events this code would have to go before any tests are defined or run.
Likely this is what you want - you'd likely want these changes to propagate to an entire spec file.
Users wanting to apply Lifecycle events globally to all specs would just add the code into a support file.
Use Cases
At first glance it seems as if lifecycle events would be binary: you'd either want to have them all enabled (by default) or turn them all off.
Not so fast Jack - let's take a deeper look and why there's a bit more complexity than this:
Clear Everything (the default)
Pros
Cons
Summary
Clear Nothing (what many users want)
Pros
before
hook, usually establishing the session thereCons
Summary
Clear Some Things (likely what you should do instead)
If you want the benefit of only establishing a session once, but don't want to problems of loosely coupled tests then you likely want this configuration:
Pros
Cons
Summary
The text was updated successfully, but these errors were encountered: