Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

611
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!