根据赛普拉斯环境变量中设置的值设置特定变量

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

我有一个名为 Cypress.Commands.add("createCustomer")

的自定义命令

我有一个环境变量,用于指定将在哪个国家/地区创建此客户。 但问题是在 createCustomer 函数中,我想根据实际创建客户的国家/地区设置其他值。对于特定国家/地区,存在不同的货币,因此不必更改所有其他货币 env.variables 我希望它们像 do-while 函数一样进行检查。

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

我应该把它放在 'createCustomer' 函数中,还是我可以把这个 IF 语句放在 cypress.env 中?

这是创建客户函数;

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('test@mail.com')

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('test@mail.com')
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')
    })
}
})

我认为没有必要将发票货币和账单周期设置为环境变量。在命令的开头,您可以将值设为局部常量:

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

此代码将 运行 同步,并且相应的值将在整个命令代码的其余部分可用。 getTenantCurrency()getTenantBillCycle() 可以在其他任何地方实施。

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