任何人都可以展示如何提取jmeter中的本地存储值。
var pkg = JavaImporter(org.openqa.selenium, org.openqa.selenium.support.ui);
var jmeterVariables = org.apache.jmeter.threads.JMeterContextService.getContext().getVariables();
WDS.sampleResult.sampleStart();
WDS.sampleResult.getLatency();
WDS.browser.manage().timeouts().pageLoadTimeout(60, java.util.concurrent.TimeUnit.SECONDS);
WDS.log.info("Sample started");
try {
WDS.browser.get(jmeterVariables.get('url'));
var userName = WDS.browser.findElement(pkg.By.xpath("//input[@id='username']"));
userName.sendKeys(jmeterVariables.get('userName'));
var password = WDS.browser.findElement(pkg.By.xpath("//input[@id='password']"));
password.sendKeys(jmeterVariables.get('Password'));
var loginButton = WDS.browser.findElement(pkg.By.xpath("//button[@type='submit' and contains(@class, 'login-button')]"));
loginButton.click();
WDS.browser.get(jmeterVariables.get('url1'));
WDS.log.info("User logged in successfully");
// Wait for the page to load for 30-40 seconds
var wait = new pkg.WebDriverWait(WDS.browser, 40); // Adjust the timeout as needed
var element = wait.until(pkg.ExpectedConditions.visibilityOfElementLocated(pkg.By.xpath("//some/xpath")));
// Perform actions with the element after it's located
// element.click(); // Example action after element is located
// Extract refresh token from local storage
var tokenStorageValue;
allStorage();
var value = JSON.parse(tokenStorageValue);
jmeterVariables.put('refresh_Token', value.secret);
WDS.sampleResult.sampleEnd();
} catch (e) {
WDS.log.error("Error occurred: " + e);
} finally {
// Ensure to quit the browser after execution
WDS.browser.quit();
}
function allStorage() {
for (var key = 0; key <= WDS.browser.executeScript("return window.localStorage.length"); key++) {
var keyName = WDS.browser.executeScript("return window.localStorage.key(" + key + ")");
if (keyName.indexOf('refreshtoken') > 0) {
tokenStorageValue = WDS.browser.executeScript("return window.localStorage.getItem('" + keyName + "')");
break;
}
}
}
我在 jmeter 日志中遇到的问题是
2024-07-13 01:40:03,098错误c.g.j.p.w.s.WebDriverSampler:发生错误:TypeError:无法使用传递的参数使用构造函数org.openqa.selenium.support.ui.WebDriverWait创建新对象;它们与其任何方法签名都不匹配。
现在,当我尝试要求浏览器等待 40 秒以从本地存储中提取用户 ID 时。我在日志中看到上述错误。
从WebDriverWait JavaDoc开始,第二个参数是Duration,并且您在那里传递一个整数,这就是您的代码失败的原因。
您需要更改此行:
var wait = new pkg.WebDriverWait(WDS.browser, 40);
对于这个:
var wait = new pkg.WebDriverWait(WDS.browser, java.time.Duration.ofSeconds(40));
至少这个错误会消失。
另外关于使用 JavaScript - 这不是最好的主意,因为: