Skip to content
This repository has been archived by the owner on May 24, 2021. It is now read-only.

Conditional logic based off of currentRetry? #32

Closed
joby-aydin opened this issue Nov 15, 2019 · 9 comments
Closed

Conditional logic based off of currentRetry? #32

joby-aydin opened this issue Nov 15, 2019 · 9 comments
Labels
question Further information is requested

Comments

@joby-aydin
Copy link

The issue is: I want to reload the URL for only my failing tests.

describe(`validate product metadata`, () => {
  before(() => {
    cy.clearCookies();
    cy.visit(Cypress.env('url'));
  });

  beforeEach(() => {
    Cypress.currentTest.retries(4);
    cy.reload();
  });

  context('product title', () => {
    it('should be visible', () => {
      cy.get('title').should('be.visible');
    });
  });
});

When I run with that way, it works. However, it reloads every tests. Is it possible to reload only for retry tests?

@kuceb
Copy link
Owner

kuceb commented Nov 16, 2019

Yes, I believe you can access the currentRetry number off of currentTest

you should be able to use

if (Cypress.currentTest.currentRetry()) {
	cy.reload()
}

It'll be 0 on the first run, and increment for every retry

Let me know if that works!

@kuceb kuceb added the question Further information is requested label Nov 16, 2019
@kuceb kuceb changed the title Is it possible Retry-ability with reloading? Conditional logic based off of currentRetry? Nov 16, 2019
@joby-aydin
Copy link
Author

Unfortunately, it is not working. Acting like there is not any condition and it runs reload() every time even if for the first run.

@kuceb
Copy link
Owner

kuceb commented Nov 17, 2019

@eyup-aydin have you tried logging out what Cypress.currentTest.currentRetry() is? I'm not exactly sure what you've tried and what you don't think is working

@joby-aydin
Copy link
Author

I did this way. Am I doing wrong? Thank you for your time and support.

describe(`validate product metadata`, () => {
  before(() => {
    cy.clearCookies();
    cy.visit(Cypress.env('url'));
  });

  beforeEach(() => {
    Cypress.currentTest.retries();
  });

  context('product title', () => {
    it('should be visible', () => {
      if (Cypress.currentTest.retries()) {
            cy.reload();
      }
      cy.get('title').should('be.visible');
    });
  });
});

@kuceb
Copy link
Owner

kuceb commented Nov 18, 2019

@eyup-aydin yes, you're calling retries instead of currentRetry

I've tested this and it works, so closing for now

@kuceb kuceb closed this as completed Nov 18, 2019
@mattrlong
Copy link

mattrlong commented Aug 24, 2020

Hi @bkucera, do you know if this above logic is available with the native Cypress retries implementation in v5?

Using the same implementation I'm receiving the following error:

Cannot read property 'currentRetry' of undefined
Stacktrace
TypeError: Cannot read property 'currentRetry' of undefined

@kuceb
Copy link
Owner

kuceb commented Aug 25, 2020

@mattrlong good question. If you use function () {} syntax for your test callback you may use this.test.currentRetry().

Otherwise you can use cy.state('test').currentRetry().

@mattrlong
Copy link

this.test.currentRetry() only works within .it statements. Some of our retry handling resides within mocha hooks, which always returns 0. This approach would also mean needing to determine the retry number within the tests and passing into any imported modules, so becomes a quite messy.

cy.state('test').currentRetry() looks to offer what we need though 👍

@dcmariotti
Copy link

Had the same issue here. cy.state('test').currentRetry() solved it. Maybe this Cypress.currentTest.currentRetry deprecation should be better documented on Migration to Cypres 5.0.0.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants