-1

我正在尝试创建一个将评估标记输入系统的工具。我目前可以使用它,但是代码非常重复。我有一个带有学生分数的 JSON 文件。然后我遍历数据的长度并将值输入到相应的输入中。完成后,我提交并进入下一个评估。我怎样才能减少重复的数量?

const data = require('data.json')

module.exports = {
  '@tags': ['Assessment'],
  'Enter data for each page'(browser) {
    const courseActionLink =
      '#DataModel_DataLinks_YourAssessments__Data_1__Items_READYTOMARK__Value'
    const submitBtn = '[aria-label="Submit Results"]'
    const nextAssessmentBtn = '[aria-label="Next Assessment"]'
    const alertBtn = '#alert-close'

    for (let d = 0; d < data.length; d++) {
      // Mark Assessment Results
      const personCode = `#DataModel_DataLinks_LearnerAssessments__Data_${d}__Items_RESULT_MARKSOUTOF__Value`
      browser
        .clearValue(personCode)
        .setValue(personCode, data[d].a1total)
        .pause(5000)
    }

    browser
      .click(submitBtn)
      .pause(5000)
      .click(alertBtn)
      .pause(5000)
      .click(nextAssessmentBtn)
      .pause(5000)

    for (let d = 0; d < data.length; d++) {
      // Mark Assessment Results
      const personCode = `#DataModel_DataLinks_LearnerAssessments__Data_${d}__Items_RESULT_MARKSOUTOF__Value`
      browser
        .clearValue(personCode)
        .setValue(personCode, data[d].sbatotal)
        .pause(5000)
    }

    browser
      .click(submitBtn)
      .pause(5000)
      .click(alertBtn)
      .pause(5000)
      .click(nextAssessmentBtn)
      .pause(5000)

    for (let d = 0; d < data.length; d++) {
      // Mark Assessment Results
      const personCode = `#DataModel_DataLinks_LearnerAssessments__Data_${d}__Items_RESULT_MARKSOUTOF__Value`
      browser
        .clearValue(personCode)
        .setValue(personCode, data[d].practicalstotal)
        .pause(5000)
    }

    browser
      .click(submitBtn)
      .pause(5000)
      .click(alertBtn)
      .pause(5000)
      .click(nextAssessmentBtn)
      .pause(5000)

    for (let d = 0; d < data.length; d++) {
      // Mark Assessment Results
      const personCode = `#DataModel_DataLinks_LearnerAssessments__Data_${d}__Items_RESULT_MARKSOUTOF__Value`
      browser
        .clearValue(personCode)
        .setValue(personCode, data[d].a2total)
        .pause(5000)
    }

    browser
      .click(submitBtn)
      .pause(5000)
      .click(alertBtn)
      .pause(5000)
  }
}
4

1 回答 1

1

我看到了 2 个很好的重构机会:

1)创建此功能:

const clickButtons = function(clickNext) {
  browser
    .click(submitBtn)
    .pause(5000)
    .click(alertBtn)
    .pause(5000)
  if(clickNext) {
    browser
      .click(nextAssessmentBtn)
      .pause(5000)
  }
}

显然,您可以随意命名它。然后,您可以用 替换那些单击和暂停的代码块,clickButtons(true)最后一个除外,即clickButtons(false).

2)那些 for 循环的唯一区别是您从哪个成员中获取data[d]。您也可以通过将该成员引用为字符串来轻松地对其进行参数化:

const markAssessment = function(memberName) { 
  for (let d = 0; d < data.length; d++) {
    // Mark Assessment Results
    const personCode = `#DataModel_DataLinks_LearnerAssessments__Data_${d}__Items_RESULT_MARKSOUTOF__Value`
    browser
      .clearValue(personCode)
      .setValue(personCode, data[d][memberName])
      .pause(5000)
  }
}

有了这两个,你的代码看起来像

markAssessment('a1total')
clickButtons(true)
markAssessment('sbatotal')
clickButtons(true)
markAssessment('practicalstotal')
clickButtons(true)
markAssessment('a2total')
clickButtons(false)

您也可以调用clickButtonsinside markAssessment,将clickNext第二个参数添加到markAssessment. 从那里,您可以将 memberNames 存储在一个数组中,并memberNameArray.forEach根据您的需要为您完成大部分繁重的工作,但这只是在这一点上去掉了一些额外的行。

于 2019-12-06T17:51:25.767 回答