You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

运行Artillery脚本时TypeError: fn is not a function的调试方案

解决Artillery Playwright测试中的TypeError: fn is not a function错误

问题详情

运行Artillery测试时触发错误:⠇ TypeError: fn is not a function,相关配置与代码如下:

测试配置文件(YAML)

target: "https://www.therisecollection.co/"  # Update with your test URL
# Load the Playwright engine:
engines:
  playwright: {}
processor: './javascriptTest.js'
phases:
  - duration: 60
    arrivalRate: 5
    name: Warm up
  - duration: 120
    arrivalRate: 5
    rampTo: 50
    name: Ramp up load
  - duration: 600
    arrivalRate: 50
    name: Sustained load

scenarios:
  - engine: "playwright"
    flowFunction: 'viewHome'
    flow: []

处理器JavaScript代码

async function viewHome(page) {
  await page.goto('https://www.therisecollection.co/');
  await page.getByRole('button', { name: 'Home' }).click();
  await page.getByRole('button', { name: 'About' }).click();
  await page.getByRole('button', { name: 'Home' }).click();
  await page.getByRole('button', { name: 'About' }).click();
  await page.getByRole('button', { name: 'Home' }).click();
  await page.getByRole('button', { name: 'About' }).click();
  await page.getByRole('button', { name: 'Home' }).click();
  await page.getByRole('button', { name: 'About' }).click();
}

module.export = viewHome;

已知未尝试的建议:

  • 在不同位置运行Artillery
  • 重新安装Artillery

调试与解决步骤

1. 修复JavaScript导出语法错误

这是最直接的问题点:代码中使用了错误的Node.js导出语法module.export,正确写法为module.exports(末尾多一个s)。修改后代码末尾应为:

module.exports = viewHome;

Artillery需要从处理器文件中加载有效的函数,导出语法错误会导致它无法识别viewHome为可执行函数,从而抛出fn is not a function错误。

2. 验证处理器文件加载状态

  • 检查processor配置的路径./javascriptTest.js是否正确,确保路径相对于运行Artillery命令的工作目录无拼写错误。
  • 可在JS文件开头添加日志代码,验证文件是否被加载:
    console.log("处理器文件已成功加载");
    async function viewHome(page) {
      // 原有代码
    }
    module.exports = viewHome;
    
    运行测试时若控制台输出该日志,说明文件加载正常,否则需检查路径或文件权限。

3. 确认函数签名合规性

Artillery Playwright的flowFunction要求函数接收两个参数:page(Playwright页面对象)和context(测试上下文对象)。虽然你的函数仅使用page,但可尝试补全参数排除潜在问题:

async function viewHome(page, context) {
  // 原有代码
}

4. 用极简测试验证环境

创建极简测试用例,排查环境是否存在问题:

  • 极简YAML配置:
    target: "https://www.therisecollection.co/"
    engines:
      playwright: {}
    processor: './test-processor.js'
    scenarios:
      - engine: "playwright"
        flowFunction: 'testFlow'
        flow: []
    
  • 极简处理器代码(test-processor.js):
    async function testFlow(page) {
      await page.goto('/');
      console.log("页面加载完成");
    }
    module.exports = testFlow;
    
    若该测试正常运行,说明环境无问题,问题出在原有代码或配置的细节中。

5. 尝试剩余建议

若以上步骤无效,再执行:

  • 切换到处理器文件所在目录运行Artillery命令
  • 重装依赖:
    npm uninstall artillery artillery-engine-playwright
    npm install artillery artillery-engine-playwright
    

内容的提问来源于stack exchange,提问作者Joshua

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 12:33:16