[英]Convert Selenium WebDriver code to JMeter WebDriver Sampler code
[英]Check Response code in Selenium WebDriver in Jmeter
我正在使用 Jmeter 中的 Selenium WebDriver 登錄網頁,並想檢查所有鏈接是否正常工作。 為此,我想檢查單擊鏈接時返回的響應代碼。
var links = WDS.browser.findElements(pkg.By.cssSelector("a"));
var href;
links.forEach(myFunction);
function myFunction(item) {
WDS.log.info("link value" + item);
href = item.getAttribute("href");
statusCode = new HttpResponseCode().httpResponseCodeViaGet(href);
if(200 != statusCode) {
System.out.println(href + " gave a response code of " + statusCode);
}
}
但是上面的代碼似乎不起作用。 如果有人能幫我解決這個問題,我會很高興。 此外,在 Jmeter Selenium Webdriver 中使用 javascript 是否有任何替代方法可以檢查所有鏈接是否正常工作?
除非您向我們展示HttpResponseCode().httpResponseCodeViaGet
野獸的代碼以及jmeter.log文件中的相關錯誤消息,否則我們無法為您提供幫助。
If the above function is something you copied and pasted from StackOverflow, I strongly doubt that it will ever work because the language of the WebDriver Sampler is not that JavaScript which is being executed by your browser, it's a limited subset of the browser version of JavaScript (例如那里沒有XMLHttpRequest )
Instead you have full access to underlying Java SDK and JMeter API so I would recommend amending your function as follows:
var links = WDS.browser.findElements(org.openqa.selenium.By.cssSelector("a"));
var href;
links.forEach(myFunction);
function myFunction(item) {
WDS.log.info("link value" + item);
href = item.getAttribute("href");
var client = org.apache.http.impl.client.HttpClientBuilder.create().build()
var request = new org.apache.http.client.methods.HttpGet(href)
var response = client.execute(request)
var statusCode = response.getStatusLine().getStatusCode()
if(200 != statusCode) {
WDS.log.error(href + " gave a response code of " + statusCode);
}
}
更多信息:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.