Cypress Tutorial: Login Once with cy.session() for faster tests

Sdílet
Vložit
  • čas přidán 21. 08. 2024
  • In this Cypress tutorial, we'll explore the powerful capabilities of Cypress.Session for robust test automation, also show you how to make your tests faster by logging in once with cy.session(). How to re-use authentication in cypress. We will cover you how to leverage Cypress.Session to share state between your test cases and speed up your test execution time. You'll learn how to handle scenarios like multi-user interactions and authentication using Cypress.Session. With these techniques, you can create a more reliable and efficient test suite.
    If you're looking for ways to make your tests faster, this cy.session() tutorial is a great way to start! We'll show you how to use this handy Cypress function to make your tests run faster and more smoothly!
    Cache and restore cookies, localStorage, and sessionStorage (i.e. session data) in order to recreate a consistent browser context between tests.
    Code:
    //baseUrl is set in the config file
    cy.visit('/');
    cy.get('#login2').click();
    cy.get('#loginusername').should('be.visible');
    cy.get('#loginusername').clear().type(username)
    cy.get('#loginpassword').clear().type(password)
    cy.get('[onclick="logIn()"]').click();
    cy.get('#logout2').should('be.visible');
    Url: demoblaze.com/
    Session documentation: docs.cypress.i...

Komentáře • 59

  • @envo2199
    @envo2199 Před 11 měsíci +5

    Exactly what i was looking for, it was hard to understand the docs about this. You explained it very well ❤❤

  • @arthurivex2298
    @arthurivex2298 Před 11 měsíci +5

    Thank you, this helped. I had over 600 tests, and now I'm saving a ton of time simply because of cy.session(). Thanks again!

  • @alexeyhimself
    @alexeyhimself Před měsícem

    Instead of cy.visit('/'); in every test we can also add to a login() function right after the session block cy.login('/'); and now we can write tests without that cy.visit('/'); as well. Thank you for tutorial!

  • @user-jb9ck3zw8h
    @user-jb9ck3zw8h Před 4 měsíci +1

    I just LOVE that you showed how to do it "the best practise way". IE: Watching your video, I learned how to use command.js
    Thanks a bunch !

    • @CommitQuality
      @CommitQuality  Před 4 měsíci

      Love to read these type of comments! Thank you

  • @matthew_jw1318
    @matthew_jw1318 Před 4 měsíci

    Nice video. So easily explained compared to other videos.

  • @JuanJoseCortesGuzman333
    @JuanJoseCortesGuzman333 Před 2 měsíci

    Wow, this was really useful, thank you for the video very clear explanation!

  • @user-ny8do6uc8z
    @user-ny8do6uc8z Před rokem +3

    This is high quality content, good job, it helped me a lot!

    • @CommitQuality
      @CommitQuality  Před rokem

      Thank you for the comment, I'm glad it helped :)

  • @cartagenadaniel1973
    @cartagenadaniel1973 Před 7 měsíci

    Perfect video! Just need to expand for Dotenv. No hard coding.

  • @samuelcacador
    @samuelcacador Před 11 měsíci

    Great explanation! Thank you very much!

  • @oleksandrostapchuk5752

    Great video and explanation! Thank you!

  • @anaisry
    @anaisry Před 4 měsíci

    Amazing, Thanks a lot u safe my test

  • @chinthakawagawaththage467
    @chinthakawagawaththage467 Před 2 měsíci

    This is a Gem

  • @AmanUllah-ps1nk
    @AmanUllah-ps1nk Před rokem

    Thankyou for this great effort !!

  • @gabrielbatocchiocondedeoli8395

    nice video bro

  • @5906646
    @5906646 Před 5 měsíci

    Perfectly done. Thanks a lot, mate!

  • @user-ye9iw9ki4n
    @user-ye9iw9ki4n Před rokem

    Great job, it helped me a lot!
    Thanks :)

    • @CommitQuality
      @CommitQuality  Před rokem

      Thanks for the kind comment. Glad to have helped

  • @sharmak2746
    @sharmak2746 Před 5 měsíci

    Thanks a lot for this video and its a great help. Amazing. Can i request one video where we can longin using Microsoft authentication or multi domain and returns to application. Basically application is authenticated using Microsoft authentication. I have reached out multiple places. I feel you are the right one to help. Appreciate of you can share a video on this and it really helps UI automation using Cypress. Thanks again and looking forward for your video

    • @CommitQuality
      @CommitQuality  Před 5 měsíci

      Yes I can try to create one. I would need to find a suitable test website to do this first though

    • @tharunv3952
      @tharunv3952 Před 3 měsíci

      Hello @sharmak2746 , i am also facing the same problem , did your issue resolved ? please do comment, thanks

    • @sharmak2746
      @sharmak2746 Před 3 měsíci

      @@tharunv3952 no, not yet unfortunately.

  • @himansrivastava
    @himansrivastava Před 8 měsíci

    Awesome! Thanks!

  • @Another0neTime
    @Another0neTime Před rokem

    Great videos my friend. Thanks for sharing!

  • @k6Coder
    @k6Coder Před měsícem

    Thank you for the very helpful video.
    How to solve this one?
    So I have website
    I login with UI
    (it adds a cookie)
    I close the Cypress and open again
    Now I don't want to login again, I want to inject the cookie inside the browser and visit my user area
    Could you please share how we can solve this?
    Thank you

  • @princePCR
    @princePCR Před 11 měsíci

    thanks for this!

  • @user-kc2wv9bc4t
    @user-kc2wv9bc4t Před rokem +1

    Tks for the video! But how is session () possible when logging in via API? Because I don't use visit() on my tests.

    • @Sneferu9
      @Sneferu9 Před 3 měsíci

      This is what I'm trying to do. Did you manage to make it?

  • @user-oz6nb1vw7w
    @user-oz6nb1vw7w Před 10 měsíci

    Excellent! I wonder which Cypress version is it? I'm trying to introduce cy.session() in my tests using v10.11 and there are several challenges. Keep isolation:false, experimentalSessionAndOrigin:true, but I can't restore the session in the next spec file. Cypress create a new one. I would appreciate whatever opinion on this. Thanks!!!

  • @69treta
    @69treta Před rokem

    Thanks!

  • @BrandonChawane
    @BrandonChawane Před 4 měsíci

    How would i run tests that depend on each other, sequentially, without logging on before each test? So i execute test1 and then test 2 and so on, without having to logon

  • @jopadjr
    @jopadjr Před 3 měsíci

    Hi, I have a quick question. I am using an internal URL (this is running in the browser) using cy.visit() but Im getting this error :A connection to the upstream proxy could not be established: ERR_EMPTY_RESPONSE: The upstream proxy closed the socket after connecting but before sending a response. Can you please advice on what needs to be done in running an internal url? Thanks in advance.

  • @amritharavikumar9760
    @amritharavikumar9760 Před měsícem

    Hi, When I use npx cypress open if I login once for the other spec files I am logged in automatically. But when I run using npx cypress run the login is happening for every spec files

  • @saravanakumara3932
    @saravanakumara3932 Před 2 měsíci

    hey, if i close the cypress runner and reopen and run the test, it is coming as session is creating but the whole cypress is got refreshed, so I won't be able to properly cache the session, so login got fail, pls help with this

  • @metarun3
    @metarun3 Před rokem

    Thank you so much for this video really helpful. I have a question currently i am using bdd page object model in cypress in typescript and i am passing username in feature file i want that all users which i am goanna use should login first and save the session before any tests run is this something possible ?

    • @CommitQuality
      @CommitQuality  Před rokem

      If you're using cucumber JS as your BDD tool, look into hooks, before all sounds like you may want that one

  • @shahulshas79
    @shahulshas79 Před rokem

  • @Top10ShortsMania
    @Top10ShortsMania Před rokem

    I got this error "experimentalSessionSupport is not enabled. You must enable the experimentalSessionSupport flag in order to use Cypress session commands"

  • @dr9mm72
    @dr9mm72 Před rokem

    How am I not seeing any Describe block in the code ?

  • @schmoukerclement3927
    @schmoukerclement3927 Před rokem

    Hey great but how is having the username and password readable as string in the codebase considered good practice ? Anyone that gains access to your git repo for any reason (pentest or stuff like that) will have those credentials. Any way to get around this issue ?

    • @CommitQuality
      @CommitQuality  Před rokem +1

      It's not best practice, this video is just to show the basics. Ideally you would use your preferred method of secure retrieval. I current use azure keyvault for mine, however you can use anything. Even git secrets may be something for you to look into

  • @LM_ID-sb3ky
    @LM_ID-sb3ky Před 5 měsíci

    can you help me i am facing issue Cy.vist in it block gives error

  • @penguinCoder-kt9bn
    @penguinCoder-kt9bn Před 5 měsíci

    This is very helpful. Was wondering if you know a way to cache cy.visit('/') since going to '/' will take up of resource. Like you mentioned, in each test we need to specify cy.visit('/')

  • @ranvijayupadhyay3987
    @ranvijayupadhyay3987 Před rokem

    Can u share me code

  • @sufiyanali3144
    @sufiyanali3144 Před rokem +2

    hey i don't know why but this isn't working for me, in 'it' block i am adding cy.visit('/') it goes back to login page if i dont add it, it doesn't go anywhere.. help me resolve this

    • @CommitQuality
      @CommitQuality  Před rokem

      Can you provide your full code please? I'm happy to take a look for you