尝试使用无铬无活性

时间:2018-02-14 14:47:59

标签: javascript chromeless

使用chromeless每5分钟运行一次测试,以验证我是否可以登录网站并且该内容可用。

代码如下所示:

const { Chromeless } = require('chromeless')

async function run() {
  const chromeless = new Chromeless({
    remote: {
      endpointUrl: 'https://abc1234567.execute-api.us-west-2.amazonaws.com/dev',
      apiKey: 'abc123xyz987'
    }
  })

  const screenshot = await chromeless
    .clearCookies()
    .setUserAgent('some-user-agent')
    .goto('https://www.example.com')
    .type('username', 'input[name="username"]')
    .type('super_secret', 'input[name="password"]')
    .click('#loginButton')
    .wait('#TitleTagOnceLoggedIn')
    .screenshot()

  console.log(screenshot)

  const report = await chromeless
    .setUserAgent('some-user-agent')
    .goto('https://www.example.com/only/accessible/if/logged/in')
    .wait('#TitleTagForPageWhenLoggedIn')
    .screenshot()
  console.log(report)

  await chromeless.end()
}

run().catch(console.error.bind(console))

这样可以正常工作,但每运行10-20次,我收到以下错误

Chromeless Proxy disconnected due to inactivity (no commands sent for 30 seconds).

在我的lambda函数的cloudwatch日志中,我得到了类似的错误

Timing out. No requests received for 30 seconds

如果我收到不活动错误,如何将其设置为重试?

1 个答案:

答案 0 :(得分:0)

转换

await chromeless.end()

chromeless.end().catch(callback).then(callback)

并将其移动到非异步功能,例如function end (callback) {...}

然后让代码等待run完成执行,然后使用处理结果的回调执行end。见https://github.com/graphcool/chromeless/issues/259