在Idea中编写Jmeter JSR223脚本(Groovy)
作者:互联网
-
在Idea中创建gradle项目
-
在gradle.build文件中添加Jmeter相关依赖(以下是整个文件内容)
plugins {
id 'groovy'
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
mavenCentral()
}
def jmeterVersion = "5.3"
def withoutBom = {
exclude group: 'org.apache.jmeter', module: 'bom'
}
dependencies {
compile group: 'org.apache.jmeter', name: 'ApacheJMeter_core', version: "${jmeterVersion}", withoutBom
compile group: 'org.apache.jmeter', name: 'ApacheJMeter_components', version: "${jmeterVersion}", withoutBom
compile group: 'org.apache.jmeter', name: 'ApacheJMeter_java', version: "${jmeterVersion}", withoutBom
compile group: 'org.apache.jmeter', name: 'ApacheJMeter_jdbc', version: "${jmeterVersion}", withoutBom
compile group: 'org.apache.jmeter', name: 'ApacheJMeter_http', version: "${jmeterVersion}", withoutBom
compile group: 'org.apache.jmeter', name: 'ApacheJMeter_junit', version: "${jmeterVersion}", withoutBom
compile group: 'org.apache.jmeter', name: 'ApacheJMeter_native', version: "${jmeterVersion}", withoutBom
compile 'org.codehaus.groovy:groovy-all:2.3.11'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
-
加载gradle配置
-
在groovy文件中编写脚本
-
附上JSR223脚本变量的来源
import org.apache.jmeter.assertions.AssertionResult
import org.apache.jmeter.protocol.http.control.Cookie
import org.apache.jmeter.protocol.http.sampler.HTTPSampleResult
import org.apache.jmeter.protocol.http.sampler.HTTPSampler
import org.apache.jmeter.threads.JMeterContext
import org.apache.jmeter.threads.JMeterVariables
import org.slf4j.Logger
import org.slf4j.LoggerFactory
//API文档
//https://jmeter.apache.org/api/org/apache/jmeter/samplers/Sampler.html
//https://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterContext.html
//https://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterVariables.html
//https://jmeter.apache.org/api/org/apache/jmeter/samplers/SampleResult.html
//https://jmeter.apache.org/api/org/apache/jmeter/assertions/AssertionResult.html
def sampler = new HTTPSampler() /*Jmeter sampler*/
Logger log = LoggerFactory.getLogger("demo"); /*日志*/
def ctx = new JMeterContext() /*非线程安全,仅适用于单线程*/
def vars = new JMeterVariables() /*vars*/
def props = new Properties() /*props*/
def Label = "当前sampler名字"
def FileName = "当前脚本名字(如有)"
def SampleResult = new HTTPSampleResult() /*SampleResult,出现于sampler*/
def prev = new HTTPSampleResult() /*上一个SampleResult,出现于后置处理器*/
def OUT = System.out
def Parameters,args = "将参数传递给脚本中的参数";
def AssertionResult = new AssertionResult("demo") /*断言*/
def manager = sampler.getCookieManager()
def cookie = new Cookie("CookieName", "value", "demo.com", "/", false, 1617835225)
manager.add(cookie);
def cookieInManager = manager.get(0)
log.info(cookieInManager.getName())
log.info(cookieInManager.getValue())
标签:Groovy,group,JSR223,Idea,version,apache,org,jmeter,def 来源: https://www.cnblogs.com/testopsfeng/p/14630477.html