简体   繁体   中英

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. 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?

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.

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)

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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