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

June 18, 2014

Installing Karma behind proxy


As part of working with AngularJS, I tried to install Karma test runner by using npm on one of our new Build Slaves. I got this error while executing the installation.
$ npm install karma

npm http 200 http://registry.npmjs.org/abbrev
> phantomjs@1.9.7-6 install /usr/lib/node_modules/karma-phantomjs-launcher/node_modules/phantomjs
> node install.js
Downloading http://cdn.bitbucket.org/ariya/phantomjs/downloads/phantomjs-1.9.7-linux-x86_64.tar.bz2
Saving to /usr/lib/node_modules/karma-phantomjs-launcher/node_modules/phantomjs/phantomjs/phantomjs-1.9.7-linux-x86_64.tar.bz2
Using proxy http://company-proxy.com:8080/
Receiving...
Error requesting archive.
Status: 403
Request options: {
  "protocol": "http:",
  "slashes": true,
  "auth": null,
  "host": "company-proxy.com:8080",
  "port": "8080",
  "hostname": "company-proxy.com",
  "hash": null,
  "search": null,
  "query": null,
  "pathname": "/",
  "path": "http://cdn.bitbucket.org/ariya/phantomjs/downloads/phantomjs-1.9.7-linux-x86_64.tar.bz2",
  "href": "http://company-proxy.com:8080/",
  "headers": {
    "Host": "cdn.bitbucket.org",
    "User-Agent": "curl/7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4 OpenSSL/0.9.8r zlib/1.2.5"
  }
}
Response headers: {
  "cache-control": "no-cache",
  "pragma": "no-cache",
  "content-type": "text/html; charset=utf-8",
  "proxy-connection": "Keep-Alive",
  "connection": "Keep-Alive",
  "content-length": "606"
}
Make sure your network and proxy settings are correct.
npm ERR! phantomjs@1.9.7-6 install: `node install.js`
npm ERR! Exit status 1

Solution: Turns out that I need to set the proxy for npm:
$ npm config set proxy "http://company-proxy.com:8080"

Then I hit another problem, where the output has this error return code 407: Unauthorized; which means we need to add credential into the proxy config:
$ npm config set proxy "http://username:password@company-proxy.com:8080"
That's it. Installation is successful!

Side notes:

Sometimes, you might need to set the registry and set https-proxy:
npm config set registry http://registry.npmjs.org/
npm config set https-proxy https://username:password@proxy.company.com:8080

References:

June 8, 2014

Reasons I Start Blogging



So... I started blogging after many years. Well, better late than never. You might wonder what made me changed my mind.

For a start, I always wanted to document what I learned, problems I faced at work and their solutions. Normally I use Evernote, it is a great tool to keep notes; but it would be much better than I could share those notes with a wider developer community which I owe so much too. There are many times I found a solution to my current in someone's blog; so I hope I could return that favour, and someone would find my blog useful.

In this keynote Jackstones: the Journey to Mastery by Dan North, he mentioned a great way to learn is to teach. Blogging is a form of teaching, so by blogging I could reinforce what I learned.

I have been introduced to SEO (Search Engine Optimisation) in my work, and have learned the power of a good domain and content. That is an investment. An investment that grows with time.

Finally, this blog post really summarised it all, 15 reasons I think you should blog,  and made me push the "Start Blogging" button.

I sincerely hope you will find something valuable from my blog.  :-)