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

315
Views
Which would be the best choice when having to reuse selectors in Cypress?

I know Cypress team encourage to use actions instead of page objects, so which would be the best choice when having to reuse an element?

I don't want to be updating a selector in many places so I went for this option:

Cypress.Commands.add('getStartDateInput',() => cy.get('input[t-selector="startDate"]');

So If I have to interact with this element in some other places I will just call this new action. Even though this is leading me to have many new 'mini' actions like this in my commands file just in order to not have to update selectors in many places.

Am I doing it right? Any other suggestion?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I use a very simple javascript way to manage and reuse my locators. You can use this too if it works with your use case.

Step 1: Create a pageObject folder(I created it inside the integration folder) and inside that create a js file. Let's name this as selectors.js. Inside the selectors.js file, write all your selectors like this:

export default {
    toDoBtn: 'button[type="submit"]',
    starteDate: 'input[t-selector="startDate"]',
    input: 'form > input',
    listItems: 'ul > li'
};

Step 2: Now import this file into your spec file using the import command.

import selectors from './pageObject/selectors.js';

And then in your test you can use:

cy.get(selectors.starteDate).should('be.visible')
over 4 years ago · Santiago Trujillo 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!