Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Cypress function problem while write different it function

While i write the code on different function it shows error like this?

enter image description here

output

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

this is the code.

Is there any solution?

>Solution :

When Cypress switches from one test case (‘it’ block) to another, it clears all the data and resets the current page to its default state, which is blank or empty. This behavior is intentional to ensure that each test case starts with a clean slate and doesn’t depend on previous test outcomes or interactions.

To address the issue of not finding the element using cy.get(‘h2’), you’ll need to manually set up the necessary data or state before each test case. Cypress provides a hook called beforeEach, which runs before each test case. You can use this hook to set up the required data or actions to ensure your test starts in the expected state.

I advise you to utilize the beforeEach hook and set up any necessary preconditions there. This will help you have a consistent starting point for each test, avoiding interference from previous tests and ensuring reliable and repeatable test results.

describe('Counter Test', () => {
  before(() => {
    console.log('This will run once before all the test cases.');
    // You can perform any setup tasks that need to run only once here
  });

  beforeEach(() => {
    console.log('This will run before each test case.');
    // You can perform any setup tasks that need to run before each test here
  });

  after(() => {
    console.log(
      'This will run once after all the test cases.',
      'Generally used to manually clean the data sent to the backend or perform any specific actions.'
    );
    // You can perform any cleanup tasks that need to run only once here
  });

  afterEach(() => {
    console.log(
      'This will run after each test case.',
      'You can perform any cleanup tasks that need to run after each test here.'
    );
    // You can perform any cleanup tasks that need to run after each test here
  });
});

FYI : it’s not related to you question but I can see you’re using cy.get('#someId') which is pretty a bad practice, I let you know more about cypress Cypress Best practices, source (you can check Cypress documentation also if you want) about cypress hooks : before() and beforeEach() …

So after navigating to the same page inside of your ‘beforeEach()’, you should find the element that you’re looking for inside of the second ‘it’ block!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading