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

แชร์
ฝัง
  • เผยแพร่เมื่อ 18 มี.ค. 2023
  • 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.io/api/commands/...

ความคิดเห็น • 57

  • @alexeyhimself
    @alexeyhimself วันที่ผ่านมา

    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!

  • @envo2199
    @envo2199 10 หลายเดือนก่อน +6

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

    • @CommitQuality
      @CommitQuality  10 หลายเดือนก่อน

      Thank you! Glad I can help!

  • @arthurivex2298
    @arthurivex2298 9 หลายเดือนก่อน +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!

    • @CommitQuality
      @CommitQuality  9 หลายเดือนก่อน

      Great job! Happy to help!

  • @samuelcacador
    @samuelcacador 10 หลายเดือนก่อน

    Great explanation! Thank you very much!

  • @user-ny8do6uc8z
    @user-ny8do6uc8z ปีที่แล้ว +3

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

    • @CommitQuality
      @CommitQuality  ปีที่แล้ว

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

  • @user-jb9ck3zw8h
    @user-jb9ck3zw8h 3 หลายเดือนก่อน +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  3 หลายเดือนก่อน

      Love to read these type of comments! Thank you

  • @AmanUllah-ps1nk
    @AmanUllah-ps1nk ปีที่แล้ว

    Thankyou for this great effort !!

  • @5906646
    @5906646 3 หลายเดือนก่อน

    Perfectly done. Thanks a lot, mate!

  • @JuanJoseCortesGuzman333
    @JuanJoseCortesGuzman333 29 วันที่ผ่านมา

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

  • @oleksandrostapchuk5752
    @oleksandrostapchuk5752 ปีที่แล้ว

    Great video and explanation! Thank you!

  • @user-ye9iw9ki4n
    @user-ye9iw9ki4n ปีที่แล้ว

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

    • @CommitQuality
      @CommitQuality  ปีที่แล้ว

      Thanks for the kind comment. Glad to have helped

  • @princePCR
    @princePCR 9 หลายเดือนก่อน

    thanks for this!

  • @himansrivastava
    @himansrivastava 7 หลายเดือนก่อน

    Awesome! Thanks!

  • @matthew_jw1318
    @matthew_jw1318 3 หลายเดือนก่อน

    Nice video. So easily explained compared to other videos.

    • @CommitQuality
      @CommitQuality  3 หลายเดือนก่อน

      Thank you for the great comment :)

  • @anaisry
    @anaisry 3 หลายเดือนก่อน

    Amazing, Thanks a lot u safe my test

  • @69treta
    @69treta ปีที่แล้ว

    Thanks!

  • @chinthakawagawaththage467
    @chinthakawagawaththage467 หลายเดือนก่อน

    This is a Gem

  • @Another0neTime
    @Another0neTime ปีที่แล้ว

    Great videos my friend. Thanks for sharing!

    • @CommitQuality
      @CommitQuality  ปีที่แล้ว

      Thank you for the kind words and support!

  • @cartagenadaniel1973
    @cartagenadaniel1973 5 หลายเดือนก่อน

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

  • @gabrielbatocchiocondedeoli8395
    @gabrielbatocchiocondedeoli8395 16 วันที่ผ่านมา

    nice video bro

  • @user-oz6nb1vw7w
    @user-oz6nb1vw7w 8 หลายเดือนก่อน

    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!!!

  • @sharmak2746
    @sharmak2746 4 หลายเดือนก่อน

    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  3 หลายเดือนก่อน

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

    • @tharunv3952
      @tharunv3952 หลายเดือนก่อน

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

    • @sharmak2746
      @sharmak2746 หลายเดือนก่อน

      @@tharunv3952 no, not yet unfortunately.

  • @amritharavikumar9760
    @amritharavikumar9760 17 วันที่ผ่านมา

    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

  • @jopadjr
    @jopadjr หลายเดือนก่อน

    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.

  • @metarun3
    @metarun3 ปีที่แล้ว

    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  ปีที่แล้ว

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

  • @user-kc2wv9bc4t
    @user-kc2wv9bc4t ปีที่แล้ว +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 2 หลายเดือนก่อน

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

  • @BrandonChawane
    @BrandonChawane 2 หลายเดือนก่อน

    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

  • @saravanakumara3932
    @saravanakumara3932 22 วันที่ผ่านมา

    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

  • @shahulshas79
    @shahulshas79 11 หลายเดือนก่อน

  • @schmoukerclement3927
    @schmoukerclement3927 ปีที่แล้ว

    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  ปีที่แล้ว +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

  • @dr9mm72
    @dr9mm72 11 หลายเดือนก่อน

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

  • @Top10ShortsMania
    @Top10ShortsMania ปีที่แล้ว

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

    • @CommitQuality
      @CommitQuality  ปีที่แล้ว

      Have you set that flag in your config?

  • @LM_ID-sb3ky
    @LM_ID-sb3ky 4 หลายเดือนก่อน

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

    • @CommitQuality
      @CommitQuality  3 หลายเดือนก่อน

      What is the error?

  • @ranvijayupadhyay3987
    @ranvijayupadhyay3987 ปีที่แล้ว

    Can u share me code

  • @penguinCoder-kt9bn
    @penguinCoder-kt9bn 3 หลายเดือนก่อน

    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('/')

  • @sufiyanali3144
    @sufiyanali3144 ปีที่แล้ว +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  ปีที่แล้ว

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