pk786
pk786

Reputation: 2400

Cypress: Filter tests using tags

I have 100 test cases some belongs to smoke, regression or both. As cypress itself doesn't provide any tagging mechanism how do i filter test them?

I've tried

  1. https://www.npmjs.com/package/cypress-select-tests
  2. https://www.npmjs.com/package/cucumber-tag-expressions

but none of them are working for me. If someone has working example of these package, please guide.

Any other way to filter tests?

Upvotes: 2

Views: 4525

Answers (1)

Alapan Das
Alapan Das

Reputation: 18619

Create a module called test-filter.ts which basically filters your Cypress tests based on what tag or tags are provided.

/// <reference types="Cypress" />

const TestFilter = (definedTags: string[], runTest: Function) => {
  if (Cypress.env('TEST_TAGS')) {
    const tags = Cypress.env('TEST_TAGS').split(',');
    const isFound = definedTags.some(definedTag => tags.includes(definedTag));

    if (isFound) {
      runTest();
    }
  }
};

export default TestFilter;

Import the above file into your spec file:

/// <reference types="Cypress" />

import TestFilter from '../../test-filter';

TestFilter(['smoke', 'test'], () => {
  describe('Taboola', () => {
    beforeEach(() => {
      cy.viewport('macbook-13');
    });

    it('should exist on an article page', () => {
      cy.visit(Cypress.env('TEST_ARTICLE'));
      cy.waitForAdRequest();

      cy.get('div[data-mode="Feeder"]').should('exist');
      cy.get('div[data-mode="alternating-thumbnails-a"]').should('exist');
    });
  });
});

Execute Tests as:

CYPRESS_TEST_TAGS=smoke npm run cy:run:local:dev

Referenced from the article - https://www.mariedrake.com/post/using-tags-to-filter-your-cypress-tests

Upvotes: 1

Related Questions