cristian
cristian

Reputation: 11

Cypress function call with several options

Cypress.Commands.add('Login', (env,username) => {
        env(staging) = cy.visit('LINK') 
        env(live) = cy.visit('LINK') 
        username(practitioner1) = {
            cy.get('input[name="Parameter.UserName"]').type('practitioner1')
            cy.get('input[name="Parameter.Password"]').type('pass')
        }

    cy.contains('Login').click()
  })

I don't know why i have ',' expected ts(1005) error

I want to call from another file something like cy.Login(staging,practitioner1) , or cy.Login(live,practitioner2) so that I don't have to hard code the link, username and password every time. Here is a prinscreen with errors: pasteboard.co/0bYOL4Dx7mnE.png

Upvotes: 0

Views: 912

Answers (2)

Alapan Das
Alapan Das

Reputation: 18650

  1. Go to your cypress.json and add all your links there.
{
  //Other cypress.json elements
  "env": {
    "staging": "www.staging.com",
    "live": "www.live.com"
  }
}
  1. In your support/commands.js add the custom command
Cypress.Commands.add('Login', (environment, username, password) => {
  cy.visit(Cypress.env(environment))
  cy.get('input[name="Parameter.UserName"]').type(username)
  cy.get('input[name="Parameter.Password"]').type(password)
  cy.contains('Login').click()
})
  1. In your test write:
cy.Login('staging', 'username', 'password')

Upvotes: 0

Alex Izbas
Alex Izbas

Reputation: 625

I would suggest the following:

Cypress.Commands.add('login', (env, user) => {
    const practitionerUser1 = { username: 'foo', password: 'myPasswordFoo#'};
    const practitionerUser2 = { username: 'zoo', password: 'myPasswordZoo#'};

    env = 'staging' ? cy.visit('staging_link') : cy.visit('production_link')


    user = 'practitionerUser1'
        ? (cy.get('input[name="email"]').type(practitionerUser1.username),
          cy.get('input[name="password"]').type(practitionerUser1.password))
        : (cy.get('input[name="email"]').type(practitionerUser2.username),
          cy.get('input[name="password"]').type(practitionerUser2.password));

    cy.contains('login').click()
});

and use it in your tests like:

cy.login('staging', 'practitionerUser1');

P.S. Because the above command interacts with the UI, for performance reasons I would suggest for actions like login to use api requests. It is more robust and reliable over time ;)

Upvotes: 1

Related Questions