Cypress import spec file

Viewed 1123

I would like to import spec files to the one main test

import heroBannerTest from './hero-banner.spec'
import fabTest from './fab.spec'
import dealerLocatorTest from './dealer-locator.spec'

let urls = getUrls()
urls.forEach((url) => {
  describe(url, () => {
    it(`Visit Page`, () => {
      cy.visit(url, { timeout: 30000 })
    })
    it(`Hero Banner`, () => {
      heroBannerTest()
    })
    it(`FAB button`, () => {
      fabTest()
    })
    it(`Dealer Locator Link`, () => {
      dealerLocatorTest()
    })
  })
})

but spec file need to look like

export default function() {
  cy.get('.main-nav-list', { timeout: 3000 })
  cy.get('.main-nav-list [class^=icon-map-pin] a', { timeout: 10000 })
  .click()
  cy.get('#cms20 .results-sidebar.map-locations > ul li')
    .should('exist')
  })
}

and I can't run this test separately any1 can help ?

2 Answers

Hi I have found solution for this

import dealerLocatorTest from './dealer-locator.spec'
import getUrls from '../fixtures/get-urls'

export default function() {
  cy.get('.main-nav-list', { timeout: 3000 })
  cy.get('.main-nav-list [class^=icon-map-pin] a', { timeout: 10000 })
  .click()
  cy.get('#cms20 .results-sidebar.map-locations > ul li')
    .should('exist')
  }

let urls = getUrls()

urls.forEach((url) => {
  describe(url, () => {
    it(`Dealer Locator Test`, () => {
      cy.visit(url, { timeout: 30000 })
      dealerLocatorTest()
    })
  })
})

it allows me to import test from here and run test separately

Suggestion: from cypress convetional testing, i would suggest you put test which you want to reuse/concept of modularization into CYPRESS COMMANDS (THE FILE IS cypress/support/commands.js). That way, you will store a test as a custom command and just call it using "cy.commandName()" where ever you want in your project.

Related