动态调用方法和子方法

时间:2018-09-18 16:36:44

标签: javascript node.js typescript puppeteer

我是NodeJ和TypeScript的新手,我正在尝试使用makeup的“ puppeteer”来为我的Web应用程序生成文档。

所以我的测试中有一个json,如下面所示:

const puppeteer = require('puppeteer')

(async () => {
  const browser = await puppeteer.launch({
    headless: false,
  })
  const page = await browser.newPage()

  var tests = test.scenarios
  var step = tests[0].steps[0]
  var step2 = tests[0].steps[1]
  await page.goto(tests[0].url)
  var fn = await getMethod(step2, page)

  await console.log(fn)

  if (typeof page['keyboard']['type'] === 'function') {
    await fn.call(step2.value)
  }
  //browser.close()
})()

function getMethod(step, page) {
  var methodArray = step.action.split('.')
  console.log(methodArray)
  if (methodArray.size == 1) return page[methodArray[0]]

  return page[methodArray[0]][methodArray[1]]
}



var test = {
  name: 'Login',
  description: 'Test to login as diferent users',
  scenarios: [
    {
      name: 'Login as common user',
      url:
        'myurl',
      steps: [
        {
          action: 'click',
          element: '#usuario',
          value: '',
        },
        {
          action: 'keyboard.type',
          element: '[name="usuario"]',
          value: 'joaoc',
        },
        {
          action: 'click',
          element: 'button.btn',
          value: '',
        },
      ],
    },
  ],
}

当我有诸如page.keyboard.type(arg)之类的嵌套方法时,代码将中断。不管我有多少嵌套,都可以动态进行此调用吗?

0 个答案:

没有答案