Изучение Cypress.currentTest: подробное руководство по текущим относительным путям

Cypress — это популярная среда комплексного тестирования JavaScript, предоставляющая широкий спектр мощных функций для тестирования веб-приложений. Одной из таких функций является Cypress.currentTest, которая позволяет получить доступ к информации о текущем тесте. В этой статье мы углубимся в Cypress.currentTestи рассмотрим различные методы использования текущих относительных путей в ваших тестах Cypress. Мы предоставим примеры кода для демонстрации каждого метода, которые помогут вам понять, как максимально эффективно использовать эту функцию.

it('should validate the title of the page', () => {
  const currentTestTitle = Cypress.currentTest.title;
  cy.log(`Running test: ${currentTestTitle}`);
  // Perform test assertions
});

Метод 2: получение полного имени текущего теста

it('should perform an action on the current test', () => {
  const currentTestFullName = Cypress.mocha.getRunner().suite.ctx.currentTest.fullTitle();
  cy.log(`Running test: ${currentTestFullName}`);
  // Perform test actions
});

Метод 3: извлечение текущего относительного пути теста

it('should perform an action on the current test file', () => {
  const currentTestFile = Cypress.spec.relative;
  cy.log(`Running test in file: ${currentTestFile}`);
  // Perform test actions
});

Метод 4: получение имени текущего тестового файла

it('should perform an action on the current test file', () => {
  const currentTestFileName = Cypress.spec.name;
  cy.log(`Running test in file: ${currentTestFileName}`);
  // Perform test actions
});

Метод 5: доступ к родительскому набору текущего теста

describe('Test Suite', () => {
  it('should access the parent suite of the current test', () => {
    const parentSuiteTitle = Cypress.mocha.getRunner().suite.ctx.currentTest.parent.title;
    cy.log(`Running test in parent suite: ${parentSuiteTitle}`);
    // Perform test actions
  });
});

Метод 6: переход к наборам предков текущего теста


describe('Parent Suite', () => {
  describe('Child Suite', () => {
    it('should navigate to the ancestor suites of the current test', () => {
      const ancestorSuiteTitles = [];
      let currentSuite = Cypress.mocha.getRunner().suite.ctx.currentTest.parent;
      while (currentSuite) {
        ancestorSuiteTitles.push(currentSuite.title);
        currentSuite = currentSuite.parent;
      }
      cy.log(`Ancestor suites: ${ancestorSuiteTitles.join(' > ')}`);
      // Perform test actions
    });
  });
});


In this article, we explored the various methods available to utilize `Cypress.currentTest` and access current relative paths within your Cypress tests. By leveraging these methods, you can gather information about the currently running test, including the test title, file name, and parent suite. Understanding and utilizing these features can enhance your test automation strategies and improve the overall quality of your web applications.

Remember to make the most of `Cypress.currentTest` in your Cypress test suite, as it provides valuable insights into the structure and execution of your tests. Happy testing!