Angularjs 根据cypress环境变量中设置的值设置特定变量

Angularjs 根据cypress环境变量中设置的值设置特定变量,angularjs,cypress,Angularjs,Cypress,我有一个名为Cypress.Commands.add(“createCustomer”)的自定义命令 我有一个环境变量,指定将在哪个国家创建此客户。 但问题是,在createCustomer函数中,我想根据实际创建客户的国家设置其他值。对于特定的国家,有不同的货币,因此我希望它们像do while函数一样进行检查,而不必更改所有其他环境变量 if(Cypress.env('tenant')) = company_denmark{ Cypress.env('invoiceCurrency') =

我有一个名为Cypress.Commands.add(“createCustomer”)的自定义命令

我有一个环境变量,指定将在哪个国家创建此客户。 但问题是,在createCustomer函数中,我想根据实际创建客户的国家设置其他值。对于特定的国家,有不同的货币,因此我希望它们像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';
        // ...
    }
}

您可以发布自定义命令createCustomer的代码吗?我不确定为什么这些需要成为环境变量。你能不能把国家传递给命令,然后计算出命令中的货币?从命令行我设置了环境,我们有4个环境和4个国家。为了使这个自定义命令具有通用性,为了在这16种不同的可能组合中工作,我希望它检查它当前所在的国家/地区。我仍然不确定为什么要在命令中将它们设置为env变量。启动Cypress时设置country via和env变量,然后在命令中使用该变量将货币设置为普通JS变量。@SimonD,发票货币不需要是env变量,我只是认为这是最简单的。但是如何在cy.createCustomer()中检查设置了什么租户,然后使用它设置正确的“invoiceCurrency”?这是我想到的,但它不喜欢代码;如果(Cypress.env('tenant')='Norweigan Krone',{invoiceCurrency='Norweigan Krone'})如果(Cypress.env('tenant')='Danish Krone',{invoiceCurrency='Danish Krone'})如果(Cypress.env('tenant')='FINLAND',{invoiceCurrency='Euro'})是的,这是一种更明智的方法。这是完美的工作,谢谢你!