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

JMeter CSS断言失败排查:有效令牌下无法检测到目标按钮

JMeter断言失败问题排查

我一整天都在学习JMeter,碰到了个棘手问题。我正在编写一个负载测试,流程是访问指定URL并点击几个按钮,URL格式如下:

https://MYSITE?access_token=my_token

问题在于,即便access_token过期,站点依然返回200状态码,所以我尝试添加断言来检测CSS路径为button#save-button的元素。

当前测试计划结构:

Test Plan
└── Thread Group
    ├── HTTP Request (访问目标URL)
        ├── CSS/Extractor (变量名: saveButtonId, 表达式: button#save-button, 属性: id, 匹配序号: 1)
        ├── JSR223 Assertion (检查saveButton是否未找到)
    └── View Results Tree

手动在Chrome中粘贴含有效令牌的URL时,能看到目标保存按钮,且CSS选择器是正确的,但在JMeter脚本中使用相同URL时,断言始终失败。

HTTP Request配置

  • 协议: https
  • 服务器名称或IP: 留空
  • 端口号: 留空
  • 请求方法: GET
  • 路径: https://MYSITE?access_token=my_token

CSS选择器提取器配置

  • 变量名: saveButtonId
  • CSS选择器表达式: button#save-button
  • 属性: id
  • 匹配序号: 1
  • 默认值: 留空

JSR223断言配置

脚本内容:

Thread.sleep(10000); // 等待页面加载完成后再查找保存按钮

if (vars.get("saveButtonId") == null || vars.get("saveButtonId").isEmpty()) {
    AssertionResult.setFailure(true);
    AssertionResult.setFailureMessage("页面上未找到保存按钮");
} else {
    AssertionResult.setFailure(false);
}

我添加了sleep,但确认不是加载问题——Chrome中页面1秒内就能显示按钮。运行测试时,即使使用有效令牌,断言也始终失败,报错信息如下:

Assertion error:false
Assertion failure:true
Assertion failure message:Save button not found on the page

HTTP Request返回结果为200:

Thread Name:Thread Group 1-1
Sample Start:2024-10-02 20:15:38 BST
Load time:105
Connect Time:68
Latency:105
Size in bytes:11121
Sent bytes:342
Headers size in bytes:1500
Body size in bytes:9621
Sample Count:1
Error Count:1
Data type ("text"|"bin"|""):text
Response code:200
Response message:OK


HTTPSampleResult fields:
ContentType: text/html
DataEncoding: UTF-8

请问我哪里操作出错了?


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 17:48:27