Setting specific variables based on what value is set in cypress environment variables

I have a custom command called Cypress.Commands.add(“createCustomer”)

I have a environment variable that specifies in which country this customer will be created. but the problem is that within the createCustomer function, i would like to set other values based on what country the customer is actually being created. for specific countries there are diffrent currencies, so instead of having to change all the other env.variables i would like them to check like a do-while function.

if(Cypress.env('tenant')) = company_denmark{
Cypress.env('invoiceCurrency') = 'danish-crown',
Cypress.env('billcycle') = 'danish-billcycle-1'
})

Should i place this within the ‘createCustomer’ function, or can i place this IF-statement in cypress.env?

This is the create customer function;

Cypress.Commands.add("createCustomer", (sendEmail, billingIdName, invoiceCurrency) => {

/*
if(Cypress.env('tenant') = 'company_denmark', {
    Cypress.env('invoiceCurrency') = 'danish-crown',
    Cypress.env('billcycle') = 'danish-billcycle-1'
    })
*/

const todaysDate = Cypress.moment().format('DD MMM, YYYY')

cy.contains('Customers', {timeout: 15000}).click().then(($div) => {
    if($div.hasClass('is-expanded')) {
        $div.click().first()
    }
})

cy.contains('Create Customer', {timeout: 15000}).click()
    
cy.get('body').then(($body) => {
if($body.find('div[r6-popover="r6-create-customer-popover"]').length > 0)   {
    cy.get('[r6-permission-context="RETAIL"] > a').click({ force: true })
    }
})
cy.wait(1500)

cy.get('.r6loader', {timeout: 30000}).should('not.be.visible')

//skip to Customer Details
cy.r6WizardNext()

// fill in data
cy.get('#business-type').select('Individual')
cy.get('#industry-type').select('Professional Services')
cy.get('#trading-name').type('Testbolaget Cypress ' + todaysDate)
cy.get('#business-number').type('SE999999999901')

cy.get('select[name=customer-title]').select('Mr')
cy.get('input[name=contact-lastName]').type('Gunnar')
cy.get('input[name=contact-firstName]').type('Svensson')
cy.get('input[name=contact-middleName]').type('testarn')

cy.get('input[name=streetName]').type('Testgatan 21')
cy.get('input[name=postCode]').type('123 11')
cy.get('input[name=suburb]').type('Staden')
cy.get('select[name=country]').select('Sweden')

cy.get('select[name=preferred-contact-method]').select('Email')
cy.get('input[name=phoneNumberDayTime]').type('0822334455')
cy.get('input[name=phoneMobile]').type('467223344')
cy.get('input[name=email]').type('[email protected]')

cy.get('input[name=telia-Id]').type('123456')
cy.get('select[name=company_code] > option')
  .eq(1)
  .then(option =>       cy.get('select[name=company_code]').select(option.val()))

cy.get('select[name=profit-Center-Telia] > option')
  .eq(1)
  .then(option => cy.get('select[name=profit-Center-Telia]').select(option.val()))

  cy.r6WizardNext()


if(invoiceCurrency) {
    cy.get('#invoiceCurrency').select(invoiceCurrency)
} else {
    cy.get('select[name=invoiceCurrency] > option')
      .eq(1)
      .then(option =>       cy.get('select[name=invoiceCurrency]').select(option.val()))
}


 cy.get('select[name=billCycleKey] > option')
  .eq(1)
  .then(option =>           cy.get('select[name=billCycleKey]').select(option.val()))


cy.get('input[name=email-to]').type('[email protected]')
cy.get('select[name=email-type]').select('Telia Email Type')

cy.r6WizardNext()



cy.get('h4.modal-title:contains("Send summary email")', { timeout:       30000 })
if(sendEmail) {
    cy.fillAndSendSummaryEmail()
} else {
    cy.get('div.r6modal-buttons > button:contains("Cancel")').click()
}  


if(billingIdName) {
    cy.get('#r6-customerheader-accountnumber-label').then(($div) => {
        cy.wrap($div.text().trim()).as(billingIdName)
    })
} else {
    cy.get('#r6-customerheader-accountnumber-label').then(($div) => {
        cy.wrap($div.text().trim()).as('billingAccountId')
    })
}
})

Answer

I don’t think it is necessary to set the invoice currency and bill cycle as environment variables. At the beginning of your command you can just make the values local constants:

const tenant = Cypress.env('tenant');
const invoiceCurrency = getTenantCurrency(tenant);
const billcycle = getTenantBillcycle(tenant);

This code will run synchronously and the respective values will be available throughout the rest of the command code. getTenantCurrency() and getTenantBillCycle() can be implemented anywhere else.

function getTenantCurrency(tenant) {
    switch (tenant) {
        case 'company_denmark': return 'danish-crown';
        // ...
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *