Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

613
Vistas
Cypress - import and export functions

How can I better organize my cypress code for testing so that I only need to import some functions?

I want to start by creating a file in which to authenticate on the page, and then I want to import it in a test with several functions.

I tried the following export code and it seems to have been incorrect, with errors:

export function login() {
    cy.visit('https://*********')
    cy.get('input[name="Parameter.UserName"]').type('*****')
    cy.get('input[name="Parameter.Password"]').type('*****')
    cy.contains('Login').click()

}

export default {login};

And in test :

import {login} from 'elements/pages/login.js'
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Your import needs a relative URL

import {login} from '../elements/pages/login.js'  // relative from cypress/integration

or if under cypress/suport/elements

import {login} from '../support/elements/pages/login.js'  

Absolute imports (where path has no leading ./ or ../) are presumed to be library packages in node_modules.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Cypress provides something called the Custom commands for this purpose, you can read about it here.

Go to cypress/support/commands.js and write all your code here like:

Cypress.Commands.add('login', () => {
  cy.visit('https://*********')
  cy.get('input[name="Parameter.UserName"]').type('*****')
  cy.get('input[name="Parameter.Password"]').type('*****')
  cy.contains('Login').click()
})

And then in your any of your test, you can directly add:

cy.login()
about 4 years ago · Juan Pablo Isaza Denunciar

0

all cys must be excuted in testing content, maybe u can try like this:

export default {

    // props
    visit: (url)=> { return cy.visit(url) }
    get: (el)=> { return cy.get(el) }

    // methods
    login(){
      cy.visit('https://*********')
      cy.get('input[name="Parameter.UserName"]').type('*****')
      cy.get('input[name="Parameter.Password"]').type('*****')
      cy.contains('Login').click()
    }

}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda