Webapp testing…

December 13, 2007

…is a spectacularly boring pursuit. Extraordinarily, magnificently, dull.

I hate it.

If you’ve done much, I wouldn’t be surprised to hear that you hate it too.

Which is why I don’t do very much of it any more. What’s more, I suggest that you join me in my noble quest.

Recently, I have been letting Watij do it for me. Before I go any further, I’m not normally what one would call a fanboi. I’ve sometimes heard my own brand of rampant unchecked cynicism referred to as “cautious optimism”, but that’s about as close as it gets.

In short, Watij is absolutely great. It’s an open source lump of Java that will test your webapp—albeit currently only in IE—straight from a JUnit instance. Nothing special there, then, you might think. The fab thing, however, is because it’s just a bit of bog-standard Java code, you can start doing really cool things with it.

It’s probably useful at this point to run through what I’ve been occupying the time between train journeys with recently, and how much Watij has helped me in this task. In order to get adequate functionality to become compliant with a big European directive, a sizable chunk of the intranet workflow application on which I work has recently been changed. The terribly fin-du-siecle static JSPs have been ripped out, and replaced with lovely bright, shiny, new AJAX counterparts.

The first thing I have to do on Workflow 1 is to ensure that for a given set of input criteria, the necessary pages are displayed in the necessary order, contain the necessary questions, and provide the correct result given the answers to the set of questions asked. The spec for this workflow is a series of Excel spreadsheets. With a bit of vlookup wizardry, it’s possible to generate all necessary test cases directly within Excel [I know. Sorry.] from the spec. Given a suitably-cunning test constructor, all possible circumstances in this instance can be catered for in one class, resulting in Watij testing every conceivable route through the workflow, at each stage asserting that the right pages are shown in the right order, and contain the right questions and notifications. Lovely stuff. One click, and that’s one morning saved per release cycle. And the really groovy thing is that because the test cases were generated directly from the spec, I know the tests are right.

Workflow 2 is a complete hog. With nearly 450 separate routes through it with any one of six distinct results, editable in two unique scenarios, there’s just shy of 900 test cases to run; each of which has to be perfect. If you think for one second that I’m going through that manually, you’re having a laugh. Luckily, Watij can ride into town, jump off its horse, and—at the last minute, of course—untie me from the railway line of tedium. With details of each test case handily knocking around in Excel, it’s a breeze to generate the code necessary for the test cases from a spreadsheet. [yes—I know. I’ve already apologised] With the 900 test cases falling into 7 different categories, all I need to do is to write 7 subtly different test classes, show each of them the necessary set of cases, and voila! For the price of my computer whirring away for a few hours, I’ve just saved a few days of mind-numbing testing.

The really handy stuff comes when you use Watij in conjunction with a bit of JDBC. The application on which I work accesses its own database via Hibernate, also setting up data in someone else’s database using a bunch of RMI calls. Without wanting to sound overly technical, as far as I’m concerned, both Hibernate and RMI are just black boxes. I know my application tosses objects at them, they do something to it, and some datum gets written somewhere. As a consequence of the directive for which we’re releasing the new version of the code, the data setup scenarios are somewhat arcane. If Result X is reached, data is to be written to Tables 1, 2, and 3, except in the case where Result Y is also true, in which case some data gets written to Table 1, another bit to Table 2, and under no circumstances to Table 3. Except if it’s a tuesday. There are 31 separate routes through the data setup workflow, which needs to be tested—again—in two separate scenarios. Testing all 62 routes through this setup manually would again take at least a day, and be prone to the errors I start introducing by testing whilst unconscious.

With the setup table defined in an Excel spreadsheet, I can use that to generate the test cases [right—I’m not apologising for this any more], and use them all in the same test class. Because the test is written in bog-standard Java, once a test has been run through the GUI, I can let the JDBC do its work, going off to each table and asserting that data which should exist does exist, that when data exists it is correct, and that data which should by no means exist does not exist. Again, one mouse click, a bit of a wait, and IntelliJ shows me the green bar of success or the red bar of failure. Because the code was generated directly from the spec, I know that the tests are correct, and therefore if the red bar of failure appears, we’ve got a functionality bug somewhere, either at our side in how we construct the objects to throw at the other system’s RMI server, or they’ve got an error in how they interpret the method we’ve called. It’s brilliant!

My only slight gripe is that out-of-the-box, Watij doesn’t play terribly well with AJAX, but with a few judiciously-used wait functions, there’s nothing in there that would stop me from using it.

I know I’ve been prattling on now for a while, but I hope that I’m starting to convey how much time and precious, scarce sanity the use of Watij has saved me. It’s fab, and if anyone wants the 5-minute intro course, you know how to get in touch.

Advertisements

12 Responses to “Webapp testing…”

  1. Mushymajor Says:

    You appear to have made a mistake and filed this with a ‘work-friendly’ tag. I’m at work, and it isn’t, but I’m glad it floats your boat


  2. How do you test the Excel code that generates the Java code? The infallibility of your cunning plan depends on this.

  3. mushyp Says:

    Good point Dom. In the case of Workflow 1, the spreadsheet is the actual spec on which the business has signed. In the other cases, these tables are derived from the spec given to the developers. Typically, I’ll prepare the table, then go through it a couple of times with a colleague and check for errors.

  4. Mushymajor Says:

    I’ll say it again – you’ve mistakenly filed this under ‘work-friendly’. Perhaps a ‘geek’ section is required.

  5. Chris Says:

    Thank you for the article/review. I think I face a similar set of technologies and challenges to those covered in this article. I’ve been recently exploring how to use Watij to test customizations with the new AJAX UI that my content (and biz process) management tool has upgraded to. If you have been writing tests for Documentum 6.0 I would love to correspond with you more about how you are writing watij scripts to open inbox tasks, submit combo keys (to the webapp not just the browser) and more. Thanks again for writing this timely review!

    Cheers,
    Chris

  6. mushyp Says:

    Hi Chris,

    I’ve no experience with Documentum, so would you mind letting me know what you mean by “open inbox tasks” and “submit combo keys (to the webapp not just the browser)”? Specifically, are you on about AJAX apps within an IE window, or external applications launched from a browser, e.g., through WebStart?

    Once I know that, I’ll be happy to try and lend a hand.

    Mushy

  7. Chris Says:

    Mushy,

    I am looking into AJAX Apps with an IE window as my primary focus. After I can find these hidden controls using watij (which is where I have been stumped) I will probably attempt to tackle some MS Word and Excel customization testing if possible.

    So, when I did my watij proof of concept I was merely opening a browser to a URL, logging in, and sending some combo keys to create a new object in Documentum. The sendkeys was sort of a desperate attempt on my part to demonstrate watij’s usability, but I’d rather (an now realize i need to) actually get a handle on the target control and send it a click event or populate a text field like I did with the log in screen:

    String aUrl = “http://somelocation.com”;
    String aUserName = “me”;
    String aPassword = “mypassword”;
    IE ie = new IE();
    User aUser = new User(ie);
    aUser.setMyUrl(aUrl);
    aUser.login(aUserName, aPassword);
    === user.login() ====================
    myBrowser.start(myUrl);
    myBrowser.textField(name,”Login_username_0″).set(aUserName);
    myBrowser.textField(name, “Login_password_0”).set(aPassword);
    myBrowser.waitUntilReady();
    System.out.println(“Password Entered?”);
    myBrowser.button(name, “Login_loginButton_0”).click();
    myBrowser.waitUntilReady();
    ===============================================

    This is a nice bit of code if your webpage actually has html elements like or or the like, but with this new interface the only tags/elements I get are and some embedded functions for event registration and stuff.

    I’ve been reading the Watij group up on google and there are others who appear to have gotten past the point that I’m on, but gleaming my solution from theirs is not as straight forward as I had hoped and my java is pretty rusty adding to my challenge.

    What I gather so far is that I may need to write my own finder and that I need to get a handle on the document object which may contain these hidden controls. So, I’ve been exploring ie.iWebBrowser2() and the watij API for this and that’s about where I am at.

    If you have any examples of where you have written code that can grab a non-html ajax like element and are able to populate it with text or if it’s a button send it a click, even better a pull down menu item, selection list item etc., I would be greatly appreciating it 🙂

    Thank you!
    Chris

  8. Chris Says:

    Mushy,

    My previous post(s) have missing text because I contained some of my speak on tags with and I think the forum is trying to interpret them. So. . . here is a re-post that I hope will work since I’ve put a space after the < .

    Please feel free to delete the previous two posts and forgive the spam.

    ========= Start ============
    Mushy,

    I am looking into AJAX Apps with an IE window as my primary focus. After I can find these hidden controls using watij (which is where I have been stumped) I will probably attempt to tackle some MS Word and Excel customization testing if possible.

    So, when I did my watij proof of concept I was merely opening a browser to a URL, logging in, and sending some combo keys to create a new object in Documentum. The sendkeys was sort of a desperate attempt on my part to demonstrate watij’s usability, but I’d rather (and now realize I need to) actually get a handle on the target control and send it a click event or populate a text field like I did with the log in screen:

    String aUrl = “http://somelocation.com”;
    String aUserName = “me”;
    String aPassword = “mypassword”;
    IE ie = new IE();
    User aUser = new User(ie);
    aUser.setMyUrl(aUrl);
    aUser.login(aUserName, aPassword);
    === user.login() ====================
    myBrowser.start(myUrl);
    myBrowser.textField(name,”Login_username_0″).set(aUserName);
    myBrowser.textField(name, “Login_password_0”).set(aPassword);
    myBrowser.waitUntilReady();
    myBrowser.button(name, “Login_loginButton_0”).click();
    myBrowser.waitUntilReady();
    ===============================================

    This is a nice bit of code if your webpage actually has html elements like or or the like, but with this new interface the only tags/elements I get are and some embedded functions for event registration and stuff.

    I’ve been reading the Watij group up on google and there are others who appear to have gotten past the point that I’m on, but gleaming my solution from theirs is not as straight forward as I had hoped and my java is pretty rusty adding to my challenge.

    What I gather so far is that I may need to write my own finder and that I need to get a handle on the document object which may contain these hidden controls. So, I’ve been exploring ie.iWebBrowser2() and the watij API for this and that’s about where I am at.

    If you have any examples of where you have written code that can grab a non-html ajax like element and are able to populate it with text or if it’s a button send it a click, even better a pull down menu item, selection list item etc., I would be greatly appreciating it 🙂

    Thank you!
    Chris
    ========= End ============

  9. Chris Says:

    Gaah that didn’t work either. . . so i cannot use tags in this forum . . . see this link on the google watij gropu for my complete message:

    http://groups.google.com/group/watij/browse_thread/thread/d3472972a9930072

    please feel free to clean up the mess I made here if you so desire!

    Cheers and thanks again,
    Chris

  10. mushyp Says:

    Aha – I think I see what you’re after. When using the “view source” function on IE, all it will show is the source of the page at the time at which the page loaded. Any html which has subsequently been AJAXed into the page won’t be displayed. In order to show the generated source (along with all of the hidden inputs), you’ll need to download and install this. It’s a piece of 3rd party kit which will show you the actual source used to generate part of a page. Just select the item you wish to see (from a lone textbox to an entire page), right-click, and then click on “View partial source”. Rather than just showing you the html which existed at page load, it will show you everything used to display the range you’ve selected; text box names, hidden fields, along with their names, ids, etc.

    Once you know that, you’re 90% of the way there. You know the name / id of the element you want to play with , so you can then start throwing data
    ie.textField(id, “myId”).set(“myValue”);

    combo box selections,
    ie.selectList(name, “myCombo”).option(id,”myOption”).select();

    or clicks
    ie.button(value, “myValue”).click();
    at it. Once you can see the actual generated source of your page, this will [hopefully :-)] make a lot more sense

    Sometimes, another stumbling block is how to fire specific javascript events triggered—for example—as an onkeyup of a text input. I’ve found no good way of doing this in one step in Watij, so tend to set the text value I need, then fire the appropriate javascript off, i.e.,

    ie.textfield(id, “myField”).set(“myValue”);
    ie.textField(id, “myField”).fireEvent(“myEvent”);

    That’s my [admittedly simple] Watij / AJAX box of tricks. Does this get you closer to where you want to be?

    Cheers,

    Mushy

  11. Chris Says:

    Mushy!

    This does help me in what looks to be the right direction! Thank you again. Because I’m using IE7 (and IE6 to some extent), the exact download you linked didn’t quite work for me. However I was then inspired to search for addons to IE7 and sure enough I found one called IE Developer Toolbar. It does reveal the html of every object on the screen so I think I am in pretty good shape to build the finders I need to support my tests. Many thanks for your advise and assistance!

    Cheers,
    Chris

  12. mushyp Says:

    Groovy.

    Happy testing, mate!

    Mushy


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: