July 11, 2014

Automated JMeter Browser Test and Report in New Relic

As part of my work, we have New Relic set up to record and monitor back-end server response time and front-end browser response time. Recently I noticed some of our JMeter Browser test did not record any traffic nor response time in New Relic. This post is to document my findings and solution. Firstly, I need to work out whether it is our New Relic setting error, or my application JavaScript error, or something else. So I start all my browsers and hit the website URL. I then wait a few minutes before looking into New Relic. This manual test has reported correctly in New Relic, where I can see all browsers data: Chrome, Firefox, IE. That means both my New Relic setting and my application New Relic JavaScript setting are correct.

Manual browser test reported correctly in New Relic

So... what's wrong? I look into how my JMeter Browser test is set up. I use JMeter Chrome Driver plugin and JMeter file looks like this:

JMeter browser test set up

The full script is here:

var javaUtilConcurrent = JavaImporter(java.util.concurrent);
var websampler = JavaImporter(org.openqa.selenium, org.openqa.selenium.support.ui, org.openqa.selenium.interactions.touch);

//open the browser before timing starts 
WDS.browser.get("about:blank")

with(websampler) {
    WDS.browser.manage().timeouts().implicitlyWait(10, javaUtilConcurrent.TimeUnit.SECONDS);
    var wait = new WebDriverWait(WDS.browser, 15);

    WDS.sampleResult.sampleStart(); 
    WDS.browser.get("http://"+WDS.args[0]);

    wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".home-page"))); 

    //end timing
    WDS.sampleResult.sampleEnd();
}

// verify on homepage
if (!WDS.browser.getTitle().contains("Our website home page")) {
    WDS.sampleResult.setSuccessful(false);
    WDS.sampleResult.setResponseMessage("Homepage failed: Title does not match the expected value.")
}

I start the JMeter script and watch closely to how it runs. I noticed the browser window might have closed too soon, maybe even before New Relic report script kicked in. To confirm that the New Relic script is loaded correctly in the test browser, I put in a wait for an invalid element, so that when the test runs, and test browser will pop up and I would have enough time to play with / inspect element on the test browser. And YES, I can see the New Relic script at the bottom of the page in Chrome inspect element.

<script type="text/javascript" src="http://js-agent.newrelic.com/nr-100.js"></script>
<script type="text/javascript" src="http://beacon-3.newrelic.com/1/somethingsomething"></script>

That confirms the page can load the script correctly, and also confirms my theory that JMeter closes the Chrome window too early before the New Relic script has enough time to report back to New Relic. I then put some wait for New Relic script element right after waiting for the page to load, and before the "end timing" block.

wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("script[src*='beacon']")));

Voila! It works, and report in New Relic is as expected.

JMeter browser test report correctly

0 comments:

Post a Comment