本文介绍了如何使用JMeter进行token传递,适合JMeter初学者。文章详细讲解了JMeter的基本概念和功能,并指导读者如何下载和安装JMeter。同时,文章还提供了在JMeter中传递token的具体方法,涵盖从创建测试计划到实战演练的全过程。JMeter传递token学习入门教程将帮助读者掌握这一关键技能。
Apache JMeter 是一个开源的压力测试工具。它主要用于测试Web应用程序的性能,支持多种协议,如HTTP、HTTPS、FTP、LDAP等,也可以测试数据库、WebSocket、JMS、Java对象、OS进程等。
JMeter的主要功能包括:
应用场景:
bin
目录下的 jmeter.bat
(Windows)或 jmeter
(Linux/Mac)文件启动。# Windows 下载示例 wget https://downloads.apache.org/jmeter/binaries/apache-jmeter-5.4.1.zip # 解压 unzip apache-jmeter-5.4.1.zip # 启动JMeter cd apache-jmeter-5.4.1/bin jmeter.bat
Token是一种用于身份验证的凭证。在OAuth、JWT等协议中,token用于代替传统的Cookie登录方式。它可以在请求中传递,用以标识请求发起者的身份。Token通常由服务端生成,并在客户端存储,用于后续请求中的身份验证。
<!-- 示例测试计划XML配置 --> <TestPlan> <ThreadGroup> <HTTPSampler> <HTTPSamplerTest> <HTTPSamplerTestElement> <!-- 请求参数配置 --> </HTTPSamplerTestElement> </HTTPSamplerTest> </HTTPSampler> </ThreadGroup> </TestPlan>
<ThreadGroup> <elementProp name="Threads" elementType="jp.keizo.jmeter.gui.ThreadGroupGui"> <ThreadGroup> <ThreadGroup guiclass="jp.keizo.jmeter.gui.ThreadGroupGui" testclass="ThreadGroup" testname="Thread Group" enabled="true"> <stringProp name="ThreadGroup.num_threads">5</stringProp> <stringProp name="ThreadGroup.ramp_time">1</stringProp> <boolProp name="ThreadGroup.scheduler">false</boolProp> <stringProp name="ThreadGroup.duration"></stringProp> <stringProp name="ThreadGroup.delay"></stringProp> </ThreadGroup> </ThreadGroup> </elementProp> </ThreadGroup>
<HTTPSampler> <HTTPSamplerTest> <HTTPSamplerTestElement> <stringProp name="HTTPSampler.domain">example.com</stringProp> <stringProp name="HTTPSampler.port">80</stringProp> <stringProp name="HTTPSampler.path">/login</stringProp> <stringProp name="HTTPSampler.method">POST</stringProp> <elementProp name="HTTPsampler.Arguments" elementType="Arguments"> <Arguments> <elementProp name="username" elementType="Argument"> <Argument> <stringProp name="Argument.name">username</stringProp> <stringProp name="Argument.value">user1</stringProp> <stringProp name="Argument.metadata">=</stringProp> </Argument> </elementProp> <elementProp name="password" elementType="Argument"> <Argument> <stringProp name="Argument.name">password</stringProp> <stringProp name="Argument.value">pass123</stringProp> <stringProp name="Argument.metadata">=</stringProp> </Argument> </elementProp> </Arguments> </elementProp> </HTTPSamplerTestElement> </HTTPSamplerTest> </HTTPSampler>
<Listener> <ResultCollector> <ResultCollector guiclass="jp.keizo.jmeter.results.dashboard.gui.ResultCollectorGui" testclass="ResultCollector" testname="View Results Tree" enabled="true"> <boolProp name="ResultCollector.error_logging">false</boolProp> <objProp> <name>saveConfig</name> <value class="jp.keizo.jmeter.results.dashboard.config.ResultCollectorSaveConfig"> <objProp> <name>filename</name> <value class="java.lang.String">jmeter_results.jtl</value> </objProp> </value> </objProp> <objProp> <name>filterString</name> <value class="java.lang.String"></value> </objProp> <objProp> <name>filename</name> <value class="java.lang.String">${JMeterHome}/bin/jmeter_results.jtl</value> </objProp> </ResultCollector> </ResultCollector> </Listener>
可以在HTTP请求中直接添加token作为请求头或查询参数。
<HTTPSampler> <HTTPSamplerTest> <HTTPSamplerTestElement> <stringProp name="HTTPSampler.domain">example.com</stringProp> <stringProp name="HTTPSampler.port">80</stringProp> <stringProp name="HTTPSampler.path">/protected</stringProp> <stringProp name="HTTPSampler.method">GET</stringProp> <elementProp name="HTTPsampler.Arguments" elementType="Arguments"> <Arguments> <elementProp name="token" elementType="Argument"> <Argument> <stringProp name="Argument.name">Authorization</stringProp> <stringProp name="Argument.value">Bearer ${__property(TOKEN)}</stringProp> <stringProp name="Argument.metadata">=</stringProp> </Argument> </elementProp> </Arguments> </elementProp> </HTTPSamplerTestElement> </HTTPSamplerTest> </HTTPSampler>
HTTP请求默认管理器可以设置全局的请求头,这样所有使用该管理器的HTTP请求都会包含这些默认设置。
<HTTPSampler> <HTTPSamplerTest> <HTTPSamplerTestElement> <stringProp name="HTTPSampler.domain">example.com</stringProp> <stringProp name="HTTPSampler.port">80</stringProp> <stringProp name="HTTPSampler.path">/protected</stringProp> <stringProp name="HTTPSampler.method">GET</stringProp> </HTTPSamplerTestElement> </HTTPSamplerTest> <HTTPDefaults> <HTTPDefaultsTestElement> <boolProp name="HTTPsampler.defaultHeaders">true</boolProp> <elementProp name="HTTPsampler.defaultHeaders" elementType="HTTPHeader"> <HTTPHeader> <HeaderElement> <stringProp name="Header.name">Authorization</stringProp> <stringProp name="Header.value">Bearer ${__property(TOKEN)}</stringProp> </HeaderElement> </HTTPHeader> </elementProp> </HTTPDefaultsTestElement> </HTTPDefaults> </HTTPSampler>
使用PostProcessor中的正则表达式提取器或JSON提取器自动从响应中提取Token,并设置为全局变量。
<HTTPSampler> <HTTPSamplerTest> <HTTPSamplerTestElement> <stringProp name="HTTPSampler.domain">example.com</stringProp> <stringProp name="HTTPSampler.port">80</stringProp> <stringProp name="HTTPSampler.path">/login</stringProp> <stringProp name="HTTPSampler.method">POST</stringProp> </HTTPSamplerTestElement> </HTTPSamplerTest> <PostProcessor> <RegularExpressionExtractor> <RegularExpressionExtractorTestElement> <stringProp name="referenceName">USER_TOKEN</stringProp> <stringProp name="matchNo">1</stringProp> <stringProp name="defaultValue"></stringProp> <stringProp name="regexp">"token":"(.*?)",</stringProp> <stringProp name="scope">body</stringProp> </RegularExpressionExtractorTestElement> </RegularExpressionExtractor> </PostProcessor> </HTTPSampler>
示例:假设响应体中包含如下JSON数据:
{ "success": true, "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9...", "message": "Login successful" }
可以使用如下的正则表达式提取Token:
<HTTPSampler> <HTTPSamplerTest> <HTTPSamplerTestElement> <stringProp name="HTTPSampler.domain">example.com</stringProp> <stringProp name="HTTPSampler.port">80</stringProp> <stringProp name="HTTPSampler.path">/login</stringProp> <stringProp name="HTTPSampler.method">POST</stringProp> </HTTPSamplerTestElement> </HTTPSamplerTest> <PostProcessor> <RegularExpressionExtractor> <RegularExpressionExtractorTestElement> <stringProp name="referenceName">USER_TOKEN</stringProp> <stringProp name="matchNo">1</stringProp> <stringProp name="defaultValue"></stringProp> <stringProp name="regexp">"token":"(.*?)",</stringProp> <stringProp name="scope">body</stringProp> </RegularExpressionExtractorTestElement> </RegularExpressionExtractor> </PostProcessor> </HTTPSampler>
<HTTPSampler> <HTTPSamplerTest> <HTTPSamplerTestElement> <stringProp name="HTTPSampler.domain">example.com</stringProp> <stringProp name="HTTPSampler.port">80</stringProp> <stringProp name="HTTPSampler.path">/login</stringProp> <stringProp name="HTTPSampler.method">POST</stringProp> <elementProp name="HTTPsampler.Arguments" elementType="Arguments"> <Arguments> <elementProp name="username" elementType="Argument"> <Argument> <stringProp name="Argument.name">username</stringProp> <stringProp name="Argument.value">user1</stringProp> <stringProp name="Argument.metadata">=</stringProp> </Argument> </elementProp> <elementProp name="password" elementType="Argument"> <Argument> <stringProp name="Argument.name">password</stringProp> <stringProp name="Argument.value">pass123</stringProp> <stringProp name="Argument.metadata">=</stringProp> </Argument> </elementProp> </Arguments> </elementProp> </HTTPSamplerTestElement> </HTTPSamplerTest> </HTTPSampler>
使用正则表达式提取器从登录响应中提取Token,并将其保存为变量。
<HTTPSampler> <HTTPSamplerTest> <HTTPSamplerTestElement> <stringProp name="HTTPSampler.domain">example.com</stringProp> <stringProp name="HTTPSampler.port">80</stringProp> <stringProp name="HTTPSampler.path">/login</stringProp> <stringProp name="HTTPSampler.method">POST</stringProp> </HTTPSamplerTestElement> </HTTPSamplerTest> <PostProcessor> <RegularExpressionExtractor> <RegularExpressionExtractorTestElement> <stringProp name="referenceName">USER_TOKEN</stringProp> <stringProp name="matchNo">1</stringProp> <stringProp name="defaultValue"></stringProp> <stringProp name="regexp">"token":"(.*?)",</stringProp> <stringProp name="scope">body</stringProp> </RegularExpressionExtractorTestElement> </RegularExpressionExtractor> </PostProcessor> </HTTPSampler>
设置后续请求的请求头,使用提取的Token。
<HTTPSampler> <HTTPSamplerTest> <HTTPSamplerTestElement> <stringProp name="HTTPSampler.domain">example.com</stringProp> <stringProp name="HTTPSampler.port">80</stringProp> <stringProp name="HTTPSampler.path">/protected</stringProp> <stringProp name="HTTPSampler.method">GET</stringProp> <elementProp name="HTTPsampler.Arguments" elementType="Arguments"> <Arguments> <elementProp name="token" elementType="Argument"> <Argument> <stringProp name="Argument.name">Authorization</stringProp> <stringProp name="Argument.value">Bearer ${USER_TOKEN}</stringProp> <stringProp name="Argument.metadata">=</stringProp> </Argument> </elementProp> </Arguments> </elementProp> </HTTPSamplerTestElement> </HTTPSamplerTest> </HTTPSampler>
通过查看响应结果,确认请求是否成功。可以检查响应码、响应时间等指标,确保请求正常。
<Listener> <ResultCollector> <ResultCollector guiclass="jp.keizo.jmeter.results.dashboard.gui.ResultCollectorGui" testclass="ResultCollector" testname="View Results Tree" enabled="true"> <boolProp name="ResultCollector.error_logging">false</boolProp> <objProp> <name>saveConfig</name> <value class="jp.keizo.jmeter.results.dashboard.config.ResultCollectorSaveConfig"> <objProp> <name>filename</name> <value class="java.lang.String">${JMeterHome}/bin/jmeter_results.jtl</value> </objProp> </value> </objProp> <objProp> <name>filterString</name> <value class="java.lang.String"></value> </objProp> <objProp> <name>filename</name> <value class="java.lang.String">${JMeterHome}/bin/jmeter_results.jtl</value> </objProp> </ResultCollector> </ResultCollector> </Listener>
通过以上步骤,可以成功使用JMeter进行登录请求,并自动提取和传递Token。这有助于模拟真实用户的行为,进行更准确的压力测试和性能测试。