Showing posts with label testing. Show all posts
Showing posts with label testing. Show all posts

Tuesday, January 22, 2013

How to invoke JavaScript in JMeter?

A colleague complained that there is little documentation about how to invoke JavaScript in JMeter. Specifically, his problem was about how to use the session ID to compute a value to be passed to the server. After playing with JMeter for an hour, we managed to figure it out. This is how it works:

Since the session ID (JSESSIONID) is set as a cookie, we only need to figure how to retrieve the cookie in JMeter and pass it to the JavaScript. The first step is to enable cookies in JMeter so that they are accessible by using variables. In jmeter.properties, set CookieManager.save.cookie=true.

Next, make sure you add a HTTP Cookie Manager to your Thread Group. Add a BSF PostProcessor and write your JavaScript in the text area provided. You can also externalize your script to a file and specify the file name in the text field above the text area. You can access the JSESSIONID cookie this way:
var sid = "${COOKIE_JSESSIONID}";

To invoke a JavaScript function and set the return value as a variable:
var val = compute(sid);
vars.put("someKey", val);

You can access the value later as ${someKey}.

Saturday, April 04, 2009

Testing Controllers That Are Protected By JSecurity Plugin

It can be a challenge to write a test case to test an action that depends on the current user. For example, the list action only returns the records that is accessible by the current user.

If you use the JSecurity Plugin, chances are you will use SecurityUtils.getSubject() to refer to the current user. If the user logs in through the login form provided by the plugin, the user name is stored as SecurityUtils.getSubject().principal. What we want to do is to find a way to inject the "current" user name during the test.

This is how I did it. In FooController, I define getSubject and list like this:

class FooController {

def getSubject = {
return SecurityUtils.getSubject()
}

def list = {
def subject = getSubject()

// Restrict results in listing
...
[fooInstanceList: results, fooInstanceListTotal: total]
}
}

and the test:

void testList() {
FooController.metaClass.getSubject = { return [principal: 'user1'] }
def ctrl = new FooController()
ctrl.list()
...
}

In line 1, just before the FooController instance is created, the getSubject closure is modified to return a map that contains principal as the key. When the list action is invoked, the getSubject() method will return the map. That way, the listing will be restricted to the records which are accessible by the user user1.

Saturday, March 21, 2009

Integration test of Grails controller

The general pattern to test a controller is:

void testBar() {
def ctrl = new FooController()
def testParams = ...
ctrl.params.putAll(testParams)
ctrl.bar()
...
}


Different outcomes are expected from the call to the action:

Redirection


If redirection is expected, the redirected URL can be obtained from ctrl.response.redirectedUrl. Keep in mind that unless GRAILS-4270 is resolved, you will have to make sure that the controller name is specified in the parameter list of redirect(). For example,

def index = {
redirect(action: 'list', params: params)
}

won't work. You need to change it to:

def index = {
redirect(controller: 'foo', action: 'list', params: params)
}


A View Is Rendered


If a view is expected, the parameters passed to render() is actually stored in Spring's ModelAndView object. E.g. if you have the following statement in your controller:

render(view: 'show', model: [ fooInstance: foo ])

You can access foo via:

def foo = ctrl.modelAndView.model.fooInstance


Text Is Rendered Directly


If the action renders some texts directly, the texts can be accessed by

def text = ctrl.response.contentAsString


A Map Is Returned


If a map is returned, as is the case for the scaffolded list action, you can access the map like this:

def map = ctrl.list()

Thursday, February 19, 2009

Testing Ajax pages with WebTest

I just learned that you need to add some delay to the test script after making an Ajax call. (Isn't it logical? Why didn't I think of that?).

For example, assuming there is an onChange handler in the combo box, you should add a sleep step after setSelectField. Groovy code below:

setSelectField(htmlId: 'foo', text: 'bar')
sleep(seconds: 5)
...

Tuesday, February 10, 2009

Writing Testable Code

If you search the web for advice on how to write testable code, you will end up with a lot of articles describing the strategies and the dos and donts. But, if there's only one thing you need to remember about making your code testable, then take this simple advice: write the tests first, then write your code.

As you start to write your tests, you'll start to think about how to use your code and the APIs that your code provides. You may find that your code has too many dependencies, instead of writing tests, your test case may end up with mostly the codes that build the object graph.

Think of how you will test your code as you design your application, then you'll realize that not all designs can be tested easily. You'll need to decide whether to use the design or to think of another design that is easier to test. All things being equal, choose the design that is easier to test.

I'm not suggesting that you should set strict rules about the writing tests. If you can't test all 10 areas of your code, test 9. If 9 is not possible, test 8. If you at least think about the tests before writing your code, you will have a higher chance of producing better codes.

This post and this elaborate on the issue in more details.