简体   繁体   English

检查预定义值的最佳方法

[英]Best way to check against predefined values

This is a calendar check.这是日历检查。 For example: 100 days of history is available but 101 days back would be disabled for certain market.例如:100 天的历史记录可用,但对于某些市场,101 天前的历史将被禁用。

Code is following:代码如下:

const todaysDate3 = dayjs().subtract(101, 'days').format('DD')
const todaysDate4 = dayjs().subtract(100, 'days').format('DD') //etc
 cy.visit(`http://calendar.whatever/ICN&markettype=ICN`);
cy.get('.calendar-table').click
cy.get('.calendar-table').contains('td',(todaysDate3)).should("have.class","disabled")           
cy.get('.calendar-table').contains('td',(todaysDate4)).should("have.class","enabled")           

What would be the best practice to make such test for 80, 100, 365 etc days as every market.对每个市场进行 80、100、365 等天的此类测试的最佳做法是什么。 Worst case scenario I can think of is something like我能想到的最坏情况是

export const 100days = [{
  "url": (`http://calendar.whatever/ICN`),
  "has100days": true
}]

and like this for every possible value and using if (curr.has100days) //do something } else if (curr.has365days){do something else}并且对每个可能的值都这样并使用if (curr.has100days) //do something } else if (curr.has365days){do something else}

Probably best would be to write some kind of function?可能最好是写一些 function?

thank you for your help!谢谢您的帮助!

You can do something like this:你可以这样做:

cy.get('.calendar-table')
  .find('td')
  .then(($ele) => {
    if ($ele.text().includes(todaysDate3)) {
      cy.wrap($ele).should('have.class', 'disabled')
      //Do Something
    } else if ($ele.text().includes(todaysDate4)) {
      cy.wrap($ele).should('have.class', 'enabled')
      //Do Something
    } else {
      //Do something
    }
  })

Since you visit each market, the data-driven approach you indicate is best.由于您访问了每个市场,因此您指出的数据驱动方法是最好的。

const history = [
  { market: 'ICN', days: 100 },
  { market: 'ZYX', days: 120 },
  ...
]

history.forEach(data => {
  
  cy.log(`Testing ${data.market} with ${data.days} history`)

  cy.visit(`http://calendar.whatever/ICN&markettype=${data.market}`)

  const outsideHistory = dayjs().subtract(data.days+1, 'days')
    .format('D')  // no leading '0'

  const insideHistory = dayjs().subtract(data.days, 'days')
    .format('D')  // no leading '0'

  const outsideHistoryRegex = new RegExp(`^${outsideHistory}`)  // ^ = startsWith
  const insideHistoryRegex = new RegExp(`^${insideHistory}`) 

  cy.get('.calendar-table').click

  cy.get('.calendar-table').contains('td', outsideHistoryRegex)
    .last()
    .should("have.class","disabled")            

  cy.get('.calendar-table').contains('td', insideHistoryRegex)
    .last()
    .should("have.class","enabled")
}

I'm assuming you only want to check the history boundary for each market, but if you want to check multiple dates per market我假设您只想检查每个市场的历史边界,但如果您想检查每个市场的多个日期

const history = [
  { market: 'ICN', days: 85 },
  { market: 'ICN', days: 100 },
  { market: 'ICN', days: 365 },
  { market: 'ZYX', days: 120 },
  ...
]

// Same function...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM