简体   繁体   English

检查 Selenium WebDriver 中的响应代码 Jmeter

[英]Check Response code in Selenium WebDriver in Jmeter

I am logging into a webPage using Selenium WebDriver in Jmeter, and want to check that all the links are working fine.我正在使用 Jmeter 中的 Selenium WebDriver 登录网页,并想检查所有链接是否正常工作。 For that, i wanted to check the response code returned when clicked on the link.为此,我想检查单击链接时返回的响应代码。

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);
    }
}

But the above code doesn't seem to be working.但是上面的代码似乎不起作用。 I would be glad if anyone could help me with this.如果有人能帮我解决这个问题,我会很高兴。 Also, is there any alternate way to check if all the links are working fine, in Jmeter Selenium Webdriver using javascript?此外,在 Jmeter Selenium Webdriver 中使用 javascript 是否有任何替代方法可以检查所有链接是否正常工作?

we're not able to help you unless you show us the code of the HttpResponseCode().httpResponseCodeViaGet beast and the relevant error message from the jmeter.log file.除非您向我们展示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 (for example there is noXMLHttpRequest there) 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: 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);
    }    
}

More information:更多信息:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM