从 JUnit 测试用例访问 JMeter 变量

Accessing JMeter Variables from the JUnit Test Case

JUnitSampler junitSampler = new JUnitSampler(); String UserId=junitSampler.getThreadContext().getVariables().get("foo"); driver.findElement(By.name("username")).sendKeys(UserId);

我看到一个错误(红色波浪线):方法 getThreadContext().getVariables() 未定义类型 JUnitSampler 怎么解决??enter image description here

你有ApacheJMeter_junit library in your project classpath? If not - you will need to add it in order to be able to use JMeter API方法吗。

如果不能 - 使用 Java Reflection 仍然可行,但会更加棘手。

另一种选择是考虑切换到 JSR223 Sampler, in the majority of cases valid Java code will be valid Groovy code and you will have vars shorthand for JMeterVariables class,您可以在其中轻松读取和写入 JMeter 变量。

有关在 JMeter 测试中使用 Groovy 脚本的更多信息,请参阅 Apache Groovy - Why and How You Should Use It 文章。