[英]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.