Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

181
Visualizações
Is it possible to create Dynamic Tests in Cypress with functions containing cypress commands

I'm trying to create multiple tests based off the number of functions in an array that I pass in, so that I can have one describe block and create tests as our project grows.

I've found multiple examples (here, real-world app examples) but all these examples create a simple array without any Cypress commands invoked. I need to pass in an array of functions that invoke Cypress (NOT Custom Commands) commands like cy.get()

What I'd like to work:

//loginPageController.js

export default class LoginPageController {
    thingOne() {
        cy.get('some things')
        expect(some assertion)
    }
    thingTwo() {
        cy.get('some other things')
        expect(another assertion)
    }
    getAllThings() {
        return [this.thingOne(), this.thingTwo()]
    }
}
//login.spec.js

import LoginPageController from '../../support/Controllers/loginPageController'
const lPC = new LoginPageController

    describe('Login Tests', function() {

        beforeEach(function() {
            cy.visit('myapp')
        })
        lPC.getAllThings().forEach(function() {
            it('tries each scenario', function() {
               // do stuff
            })
        })

Error message

The following error originated from your test code, not from Cypress.

  > Cannot call cy.get() outside a running test.

This usually happens when you accidentally write commands outside an it(...) test.

What I've tried

  1. it.forEach(getAllTheThings)('it should do the things', function(){})
  2. Setting the function as a variable in a before block const lPC.getAllThings() = allThings which actually just executes the function successfully in the before
  3. Aliasing/wrapping like cy.wrap(lPC.getAllThings()).as('allTheThings) (which is probably invalid but I'm just taking swings at this point)
  4. Using a fixture file but .js doesn't seem to be easily supported currently and I'll be passing in an array of functions, not a simple JSON object.

I'm just trying to keep things DRY and only write one describe that spits out as many tests as functions I include in that lPC.getAllThings()

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda