Transcript for:
Life Cycle Hooks in Cypress

hi all welcome back in this video we will see what are the life cycle uh hooks that are present in a Cyprus test execution uh how those life cycle methods will help us to reduce less number of uh code and how we can reuse the code in each spec file okay so if you see there are four life cycle hooks uh present in a Cyprus test execution the first one is before and the second one is uh before each and third one is after each and then fourth one is after so the before uh life cycle hooks um will be executed one time for each spec file and before each life cycle um uh hook will be executed um before every uh test and after each I will be executed after each test and then after will be executed only once for um our spec file okay so there are four and all these four uh we'll see in action now so let's go to our browser our Visual Studio code and for that uh let's create a another spec file I will call as a life cycle hooks.cy.js okay and then need to write a describe so I press life cycle hooks okay and then right arrow function and then we need to see this life cycle hooks here so the first one is before and then before each and after each and the fourth one is uh after okay so for before as I mentioned before will be executed only uh one time for spec file okay so the Syntax for before will be before and then we need to pass a Arrow function here so in this uh I'm going to use the Cy DOT log which is which will be logged in the Cyprus test Runner so before I will be executed only once so uh this life cycle hook will be executed once before once okay and then the second one is uh before each okay so before each also takes a function here also I am writing Cy DOT log this life cycle Loop will be executed before each test case okay and then after after each so it is also same and um I'm going to copy pasting this life cycle hook will be executed after each test and then for the fourth one after so I'm again copy pasting this life cycle will be executed once at the end okay so to prove this I'm going to create a some more a couple of test cases here so let's write a couple of tests test case one what I am going to do is see why that we see it and from the previous [Music] spec file I am going to copy the login path uh so basically let's copy the entire one okay so what what I'm doing here is uh I'm visiting the web page and then I'm getting a username input field and then add a typing admin at the uh and uh next to I'm getting a password field and I am typing the password and I am submitting the form here okay see here I am entering only uh the URL here the base URL I have configured it in um cyprus.config.js file Okay so let me write one more test case and then I'm I'm writing I'm using the same test case here then I will rename the test case to test case two okay so let me save this okay and we can see here before before each and two test cases test case one and test case two after each and then after okay if I if we go to our test Runner uh we can see that the life cycle hook Cy dot JS got added added now and if I click on it see there are two test cases both test cases got executed and if you see um for each test and you see there is one with the before all and before each and then after each and in the second test case you will see there is no before all uh there is only before each and then the test case and then after each and at the end you will see the after all okay this is after a life cycle hook but if you see the first one there is um uh no after uh our life cycle hook okay so the before life's life cycle hook got executed only once and before each life cycle uh who got executed um uh two times for each test uh one one time for each test total two times before each for the test uh test case one and before each for the test case two uh similar similarly so after each got executed two times for test case for test case one one time one for test case two two times and after all got executed uh once uh which is after test case two so uh in this way we can use the life cycle hooks so if you wanted to execute anything before each test you can write that logic in before each and if you wanted to execute anything after each test case that you can write in after each so if you wanted to execute um anything uh that should be executed before uh any test uh at uh one time then we can use before at the same at the same in the same way so we can use after if you wanted to run um anything uh after all test cases got executed okay in this way we can use the life cycle um hooks in Cyprus thank you