Monday, December 8, 2014

Linux, BlueJeans and SpringSource Tool Suite

Not everything is gold that shines

This has been a long, long day. I learned once again that people don't care about users using their programs but they care very much about those few that give them money. This is also the case with BlueJeans...

The problem

I was a bad smell from the beginning. To use BlueJeans you need to install the nprbjn plugin which is provided as a deb/rpm package. On Linux Mint (which I'm happily using for many months now) I choose the deb package, installed it and voila! Everything was up and running. It was such a relief from using this piece of shit WebEx that it felt almost surreal. I soon realized that my joy was premature. After I restarted STS (while on the meeting..) I was kind of shocked that just a few seconds into using it it just shut down on its own. No warning, no message, no nothing - just bang! and it was gone.

I tried doing pretty much everything: installing and reinstalling Eclipse, reinstalling STS - nothing helped. And so I arrived at removing the BJ plugin after which everything immediately went back to normal. And I said to myself "what a piece of crap!" but hey - you need to work with it and besides the fact that it is screwing with the main application you're using on a daily basis (pretty much 80% of the time) everything else looks and works kind of nice.

I even went through the trouble and created a support ticket explaining the whole situation but there was no concrete solution beyond we'll look into it and 3 months later there's still no fix for that annoying bug.

The solution

Or should I rather say workaround... How can we make it work both ways?

1. Start firefox with -p parameter - this will open a new window for selecting profiles
2. Create a new profile, let's call it Eclipse
3. Check the 'Use the selected profile without asking at startup' and click Start Firefox - the new profile will be initialized
5. Start STS, navigate to Window -> Preferences and select General -> Web Browser
6. Highlight 'Firefox' in the list of browsers, click 'Edit' and in the 'parameters' field type

-p Eclipse

7. Click OK, then Apply and close STS
8. Close Firefox, start it again with -p but this time select the 'default' profile so that everything is back to normal with your browser

This will ensure that Eclipse has its own clean profile and you can use STS without interruption.

Happy coding!

Saturday, November 22, 2014

Opening and closing resources

It's the silliest thing that's been bothering me for agest: why on earth would someone go through all the trouble to write code like that:

InputStream in = null;
try {
    in = openInputStream();
    // do something with in
} finally {
    if (in != null) {
        in.close();
    }
}

First of all, if opening a stream didn't succeed then an exception is thrown and "in" is going to be as null as it gets - that's true, but the actual exception is going to get propagated upwards too - so why the hell do the it in the try block and check for null in finally? It's just so pointless I can't stand it..

InputStream in = openInputStream();
try {
    // do something with in
} finally {
    in.close();
}

Now isn't that a lot more readable, simpler and everything? If the opening of a stream blows up everything blows up too - just like in the code above.

Can anyone please be kind and explain this complete insanity to me?

Sure, with Java 7 we have the try-with-resources feature - but is it doing anything more than the second form?

try (InputStream in = openInputStream()) {
    // do something with in
}

The visibility of the stream is limited to the body of the try statement which seems to be the absolutely only difference to the previous version (the pre-java7 one).

Thursday, November 20, 2014

JSON RPC framework in 12 lines

There are lots of times when I just shake my head in admiration of what Groovy actually is. This has been one of the times today and I'm here to tell you I'm not easily impressed.

We're introducing a communication layer between our microservices based on JSON-RPC (just because it's cool and fast). Here's an initial implementation of a Groovy-based framework for doing JSON-RPC calls:

class JsonRpcClient extends HTTPBuilder {
    JsonRpcClient(String uri) {
        super(uri)
    }

    def methodMissing(String name, args) {
        def result
        request(POST, JSON) { req ->
            body = [ 
                "jsonrpc" : "2.0", 
                "method" : name, 
                "params" : args, "id" : 1 ]
            response.success = { resp, json ->  result = json }
        }
        return result
    }
}

I means sure it's not complete but using it is very much possible!

def http = new JsonRpcClient('http://localhost:8080/example/api/hello')
println http.sayHello("John")

I mean how cool is that, ha? Some meta programming with an existing framework (HTTPBuilder) and you get a micro implementation of JSON-RPC protocol in a few lines of code!

Happy coding! Groovy Rulez!

Monday, October 20, 2014

Data structures are not tabelar

Recently one of my colegues has been designing a piece of structure that was supposed to store some data about something. It isn't really important why or what data. The point is that for the most part there have been 1:1 or 1:n relations. I can't tell why or how it came to me but the itch was just to strong to let it go and I really had to ask what is actual thing he's trying to achieve.

The data model consisted of 8 tables, 2 additional artificial concepts (just for the sake of storing data) and a bunch of names that didn't really make any sense.

It turns out that when we tried to create example records we wrote them down very naturally in JSON because storing data in tables on paper was just to cumbersome.

A rule of thumb: if you can't picture something don't do it!

I started to look for an alternative storage for those documents and found out that there are not too many options available for free. There's obviously the (almost infamous) MongoDB but with its recent bad press and the lack of embedded mode I felt it's not the right way to go. Luckily we've stumbled upon OrientDB - a multi-paradigm database implemented in Java. Since the application was already on the JVM having an option to do an embedded document database seemed like the perfect match.

Now the whole thing is just a document with some embedded documents since all the data comes as one. And using OrientDB's pseudo-SQL dialect is super simple!

Go ahead and try it out yourself! It's super simple!


Happy coding!

Friday, October 10, 2014

Continous deployment - Java style

Recently the topic of continuous deployment has been a hot topic around the office. CD this, CD that, we need to CD because [increase productivity, faster time to market, agile deployment]... You name - we have heard it. I'm sure you don't really need to be told what continuous deployment / delivery is. It's all about making deployments easy, boring and natural part of your work day.

When faced with the task in any of the scripting languages (take PHP for instance) the task is extremely easy: just rsync the files and you're done. This means that you need to take into account that some session data may be null in certain circumstances and you're pretty much done. It's like having a zillion nano applications, each file you hit from the browser being one of them and some services (the includes). Bum! You're done!

In Java the case is a little bit different. First of all one needs to understand that the usual deployment of Java applications involves deploying of 3rd party components in binary (a.k.a. compiled) form. The library takes a form a zip file with a ".jar" extension and contains lots of folders with ".class" files (the product of compilation of ".java" sources). For that very reason doing CD on Java applications isn't all that easy. The second thing that makes it even more attractive is that the actual application is packaged in yet another zip file, this time with a ".war" extension (as in "Web Application aRchive" I presume). The third insanity level is the packaging of multiple web applications alongside some business layer services all packaged into yet another zip archive, this time with the ".ear" extension (no, it's not your hearing organ, it's the "Enterprise Application aRchive"). This has historically had only one reason: to be able to provide a packaging mechanism and to minimize the overhead of data transfer over the wire (I mean the must have been something else but I didn't find anything on that topic so far so I take it I'm right on this one).

To be completely fair there is a way to deploy both unpackaged .war's as well as .ear's (however strange that sounds :D) to an application server, but since it doesn't really matter if in the application a single ".jsp" (as in Java Server Pages, similar to ASP's - Active Server Pages in the Redmond world) file gets updated because it most likely uses some binary ".class" file that will not get updated. There are paid solutions to this problem but I think it's going to be a fairly seldom case where you'd want to pay lots of money to get CD done (unless you can spare then off you go!).

For the purpose of this discussion we're going to focus only on .war deployment descriptors and only on the reference implementation of the servlet container, Apache Tomcat, and only in version 7+.

What do you need to get continuous deployment done? The answer couldn't be simpler: proper naming!

Here's an example: we're working with an application called "example" (for lack of a better name) and we want the following:

1. Users using the system will not experience any undesired results, that includes:
- sudden unavailability of the system
- change in behaviour
2. Users using the system will make a semi-conscious decision to start using the new version
3. The old version will be automatically uninstalled once every user makes the decision from pt. 2

So here we go. The first version can be named anything. So let's go simple and call it example.war. Since it is most likely that the application will utilize some server state in the form of a session the client will get a cookie with unique ID called JSESSIONID. This is what binds the user to a deployed version of the application on the server. Now if a user logs out then a new JSESSIONID is generated. This is very important. Read on.

Tomcat has the capability to run multiple versions of the same application out of the box since version 7. How is it done? By naming the next versions properly:

example##001.war
example##002.war
example##003.war

Please note that the naming of the version is alphanumeric therefore I took the leading zero to pad the version number so that it always increases. The main point here is that the resolution which user will hit which version is done by the JSESSIONID!

- if no JSESSIONID is sent from the client - newest version
- if JSESSIONID is sent from the client but cannot be bound to any existing version - newest version
- otherwise there's a JSESSIONID matching a running version

An automated shell script to get the next version number from a remote server is as follows:

#!/bin/bash

user='your-user-on-remote-machine'
host='name-or-address-of-remote-machine'
location='location-of-webapps-folder'
apppatern='base-name-of-your-application'

number=$(ssh ${user}@${host} "ls ${location}/${apppatern}*war -1 \
        |sed 's,.*${apppatern},,;s,##,,;s,\.war,,'|sort -n|tail -n1")

if [ -z $number ]; then
    numpad=3
else
    numpad=${#number}
fi

number=$(expr $number + 1)
nextnumber=$(printf %0${numpad}d ${number})


echo ${nextnumber}

To make sure the application gets automatically undeployed when everybody's session is either timed out or otherwise invalidated include the

undeployOldVersions='true'

parameter in the "Host" element of your server.xml configuration file. Done.

So to bottom line this for you:

1. Use naming convention in the form of appname##version.war remembering that it is alphanumeric and _not_ numeric so padding is crucial
2. add the undeployOldVersions="true" parameter to Host in server.xml
3. Start rolling updates

Of course the entire process in real life is a lot more complex. It involves automated testing of the application before it gets released, automated copying of files to the server - stuff like that. But the essential piece is there and you get it absolutely for free. Please note that since it is the entire version of the application being updated it is OK to have your dependencies updated as well with such an update. This will just work.

Here's the link to the relevant configuration options in Tomcat:

http://tomcat.apache.org/tomcat-7.0-doc/config/context.html#Parallel_deployment

Happy CDing!

Wednesday, September 17, 2014

Running transmission as a different user

Sometimes things should just be easier. One config file, restart, done. This time I faced quite a different daemon so I though I'll share since it took me a while to figure it out. It's about the transmission-daemon running as a different user.

The why?
I need to download files from the Internet using BitTorrent protocol (like Ubuntu ISO for example) and I'd like to do that using a computer that's serving as my home server.

What's difficult?
First of all there's no place in any of the configuration files to tell you which user is it going to be that's running the daemon. That secret is safely guarded inside /etc/init.d/transmission-daemon. You'll find this kind of line:

USER=debian-transmission

So you'd think that's all then. We change it to something like

USER=nobody:nogroup

and life's easy. Well, not exactly. If you try to do this you'll see that transmission-daemon tries to start but fails rapidly. To diagnose what's wrong you'll want to use this daemon in foreground mode like this

transmission-daemon -f --config-dir /var/lib/transmission-daemon/info --log-debug

But that'll only tell you that there are permission issues and that some files that apparently may even have permissions like 666 on folders that will have 777 permissions will still not be out of reach. The problem lies in the default configuration of the daemon. It keeps it's configuration data in /var/lib/transmission-daemon/info however it's customary to store such information in /etc which Ubuntu and Mint do. And so there's a /etc/transmission-daemon/settings.json with the ownership of debian-transmission and it's then linked to the place where Transmission awaits it (/var/lib/transmission-daemon/info/settings.json)

The solution
So here's what I did. I first stopped the deamon or else my configuration file would get overwritten. Then I changed the ownership of the entire structure of /var/lib/transmission-daemon to nobody:nogroup like so

chown -R nobody:nogroup /var/lib/transmission-daemon

Then I removed the /var/lib/transmission-daemon/info/settings.json link and replaced it with the /etc/transmission-daemon cusine:

sudo mv /etc/tranmission-daemon/settings.json /var/lib/transmission-daemon

and I updated the ownership of that file again:

chown -R nobody:nogroup /var/lib/transmission-daemon/info/settings.json

That's it! Transmission now runs as user nobody:nogroup creating new files and folders as nobody:nogroup and life is easier again

Monday, September 8, 2014

Migrating a project from Google Code to GitHub

Recently I've grown very impatient to the progress being made to psi-probe. I use it at work and wherever else I can because it is a fantastic piece of software but the fact that the last commit was around 6 months ago leads me to understand that the project is simply dead.

The original author assured me that he's got major interest in keeping this project alive but it seems he's got no time to do so. Also keeping the project maintained with Subversion these days seems a bit too vintage for me. And so I decided to migrate the whole thing to GitHub.

Migrating the repository itself is quite simple and there's more than one tutorial on the Internet to help you out with it. The major thing that needs to come out at the end is a repository with git tags (not Subversion ones), git branches (same as Subversion ones) and an .gitignore file containing the set of things you'd like to not care for.

Migrating issues is also not very difficult once you have all the tools in place. https://github.com/arthur-debert/google-code-issues-migrator is your biggest friend. As with any friend there's love and there's hate involved. Basically the tool does everything properly up until some damn comment contains some god forsaken character in which case the whole thing blows up in your face.

Traceback (most recent call last):
  File "./migrateissues.py", line 387, in
    process_gcode_issues(existing_issues)
  File "./migrateissues.py", line 288, in process_gcode_issues
    add_comments_to_issue(github_issue, issue)
  File "./migrateissues.py", line 120, in add_comments_to_issue
    body = u'_From {author} on {date}_\n\n{body}'.format(**comment)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 362: ordinal not in range(128)

There are 2 places where a similar problem causes the tool to stop working. The other one is at the addition of an issue. I've worked around by catching that error and providing some dummy text like "Unable to import comment". I'll later on modify that comment by hand and using the Copy/Paste method I'll bring it on the level.

To keep the numbering of issues the same as on Google Code I needed to create some dummy issue because someone deleted issue nr 1 and the importer doesn't recognize this fact and skips the creation of the first, missing issue. Fortunately enough it's quite easy what the first issue should contain in a migration project like that so I used that to my advantage :)

Anyways.. If you'd like to see the Psi Probe flurish again you can always post me a thank you card for all the hard work I'm doing :) Or better yet post a pull request with fix to one of the 100+ issues imported from the original project - the choice is yours!

Happy coding!

Sunday, August 10, 2014

Selenium tests - a couple of loose thoughts...

Recently I've blogged about how to employ fluent interface when creating a framework for Selenium-based integration tests. After a few talks I gave I think I have a some grasp of what the major concerns for taking this approach are. Let's go quickly through what I observed was the top 5 questions.


Assertions shall be done in tests - not in page objects

This is probably the numero uno questions/concern voiced during the presentations. Let's assume for a second that we're not doing fluent interface in our Selenium tests and force ourselves to write assertions in tests, because the original documentation says so.
It turns out pretty quickly that there's more than one of the same assertion in more than one test. You may ask why that's the case? If you try to keep your scenarios separated and isolated you will end up either navigating to the page in question more than once (and verifying if the navigation succeeded). That's just one example but you get the idea.
For me personally the page object being the right place to define the assertion has more to do with scoping and avoiding unnecessary getter just to be able to assert a state on a given page. I know that for years we've been taught the POJO paradigm and the JavaBean convention, that most of us have that already in our DNA, so to speak. There are even things to spice up your life in this regard and have the compiler generate getters and setters for you. Hell, there are even languages like Groovy (my very favorite one) that will generate those things for you without you even asking for it. I actually like the idea because it takes completely away the POJO bloated boilerplate and gives you a sane way to express your intent. All that does not mean that we are allowed to turn off thinking and follow the convention like rats followed the pied piper. When you need an action on an object you don't create a separate friend/utility class for it but you embed that inside that object. I don't know if that's already domain-driven design or whatever but it is just plane sanity dictating it. The lack thereof can be easily observed in the Java Collection API (Collections.sort(people, comparator) instead of simply calling people.sort()). We've been fed this pulp for years - no wonder we take it for caviar.

I already have an existing suite of tests - what shall I do?

This is probably going to hit everyone who'd like to try the fluent interface in Selenium tests. Let me quote an old saying: "No pain, no gain". Things will not evolve by themselves - you need to take matters into your own hands and make the world a better place. I know that for many of you those tests are more of a pain in the ass that the boss/tech lead/company policy brutally makes you do but if you don't change your mindset how can you even begin to expect to start changing how others operate?
I say start small. Write one or two tests, create a handful of page objects, make a presentation, get others excited. The hard part is already done in all your other (for the sake of it I'm just going to call them legacy) tests and all that's left is just changing the outfit.

How do I allow writing custom assertions?

I brought this up as a separate concern because it's been made very clear for me that sticking with what the regular assertion framework gives you is far from optimal. Although AssertJ or just the assertions from org.junit.Assert class give a certain amount of flexibility it'd be nice to have some common patterns baked in into some custom assertion class.
Let's make one thing crystal clear: there are 2 reasons why you'd want to do it. One, when you want to have for example strongly-typed assertions that compare domain objects, maybe you parse and assert strings in a peculiar way throughout your test suite - that's the generic extension and it is perfectly legit. However there is a whole group of other reasons why people do it: to write custom assertions that access the state of your page objects and assert if a given action gave proper results. Well that's precisely the Collections.sort(people, comparator) vs people.sort(comparator) thing and if you do it that old dumb way you're just going to have to stick with it while the whole galaxy moves forward.
That being said there's absolutely nothing (besides sanity) preventing you from writing a separate custom assertion and calling it from a wrapper inside a page object to continue the fluent interface.

Does it work for you?

It's been almost a year when this pattern has been used in our product. During that time we went from near-no-tests to around 60% coverage of a very legacy system which in my opinion counts as a great success. But the best part of it is that it doesn't stop! Developers simply love the way they create tests. It's easy and now that we have a lot of the page objects already created writing new tests is really simple. Even simple bug fixes begin with a selenium test and it takes mare minutes to write them.
And above all I love the way it translates from steps to reproduce to steps in a test - it is plain awesome! Please bear in mind we're talking about things in one of the most constrained languages out there that a moderately sober student can learn in a week!

Are there any drawbacks?

Yes, there are! Since creating data for tests is so extremely easy developers tend to prepare that data every time they write a test and that tends to make those tests run slowly. So if speed is what you're craving for you need to be smart about it. We tend to create separate database migrations that run before selenium tests and that way if we already have part of the functionality covered in other tests we can speed things up.

How long runs your test suite?

Around 5 hours. That being said doing the same thing manually takes a week so I say we're well on the safe side. Please bear in mind that we're executing those tests sequentially and that doesn't need to stay this way. Since most of our tests have data specific for them created before each test that makes it very much possible to run them in parallel. Initial tests show that running the whole system on developer's notebook and executing 20 tests in parallel cuts the time to 20 minutes which is well within the realm of modern continuous deployment scenarios.

Well, that's it! Happy coding!

Wednesday, April 16, 2014

Short and to the point - BrightTalks meetup

I gave a presentation for the BrightTalks meetup about how I envision writing selenium tests. Read more about it here (http://www.meetup.com/Bright-Talks/events/174660312/)

Saturday, March 29, 2014

Are Spock and others dead?

I've been looking for a good alternative to writing unit tests, maybe in a more BDD-like form, or just to get some fresh perspective on how one could do unit tests. I think it's because me getting old and the middle-life crisis hitting on me :) And so I examined a few of the usual suspects in the Java space...

Spock

Some time ago I was extremely inspired by Spock. It was The Way to do unit tests. The introduction of a table-like construct to do multiple data-driven tests, labels for given/when/then template - that was awesome! I was hoping Peter Niederwieser would keep working on it forever but since he joined Gradelware his talent seems to be occupied elsewhere and Spock seems to be discontinued.

Fest Assert

I remember the time when Szczepan showed us fest-assert framework in action. Szczepan was well known for his passion for fluentization of all possible interfaces - which I strongly admire - but this, this was the real deal, the perfect combination of why and how to do things. I fell in love with this library, even started to create a .NET port of it and I'm still a fan. But it's been discontinued too. Luckily it's been picked up by Joel Costigliola and is being developed further as AssertJ. I sure hope this will keep being maintained!

TestNG

Now that's a fresh start I'm going to try to pursue. New annotations, a bit dangerous in what it tries to accomplish with the dependency on tests (I always hated that) but I'll try to give it a shot. And it's still maintained which is makes it stand out among other frameworks.

Geb

Now that's a good example of something one should never had created - a DSL to tackle a DSL. Wrr... I thought it couldn't get any worse but then I found...

Fluentlenium

Well,... It seems like someone tried to kind of repeat the success of fest-assert and got it wrong. I tried it and it is just... wrong.

Conclusion

From what I've been able to figure out in a few hours it seems like only TestNG is the only actively developed alternative to JUnit and one of the very few frameworks in the testing space that actually makes any advancements. But hey - I might very well be wrong. If you can share a link to something revolutionary in the testing space drop me an email ad padcom (at) gmail (dot) com or leave a comment below.

Happy testing!


Thursday, February 20, 2014

Selenium and reusable scenarios

In my last post I've shown you how to structure your code describing the application under test so that it allows for easier construction of more readable integration tests. Today I'll try to complement that with the concept of reusable blocks of tests - scenarios.

Let me give you an example of a scenario that everyone can relate to: logging in to an application. Let's consider what does it actually take to perform this task:

  • navigation to the login page
  • entering proper credentials
  • clicking the "Login" button

Doing that in every test every time will soon make the tests unmaintainable. For example changing username and/or password or adding language to the login page when you do this 200 times is just going to be a huge pain in the neck.

Scenario concept to the rescue.

Let's imagine our base Page object contains this kind of method:

public <I extends Page, O extends Page> O run(Scenario<I, O> scenario) {
    return scenario.run(this);
}

Having defined this let's see the Scenario interface:

public interface Scenario<I extends Page, O extends Page> {
    O run(I entry);
}

The I generic parameter is the type of the page that you'd like to start the scenario on. The O generic parameter is the resulting page the scenario ends on.

Now the definition of the login scenario from the last post would look something like this (extended with language selector):

public class LoginScenario implements Scenario<LoginPage, HomePage> {
    private final String username;
    private final String password;
    private String language = "english";

    public LoginScenario(String username, String password) {
        this.username = username;
        this.password = password;
    }

    @Override
    public HomePage run(LoginPage entry) {
        return entry
            .assertHasProperUsernameLabel()
            .assertHasProperPasswordLabel()
            .enterCredentials(username, password)

            .selectLanguage(language)
            .clickLogin()
            .assertPageTitleIs("Welcome to this cool application!");
    }


    public LoginScenario language(String language) {
        this.language = language;
        return this;
    }
}

There are 2 types of parameters for this scenario: required (username and password) and optional with default values (language). The usage of fluent interface in setting up the scenario plays very nicely with the overall style of the integration tests. The fancy-looking generic Scenario interface allows to chain the execution of this reusable block of code in a fluent interface of the actual test:

public class LoginTest {
    @Test
    public void will_login_and_logout() {
        new LoginPage("http://localhost/myapp")
            .run(new LoginScenario("johndoe", "secret").language("german"))
            .logout();
    }
}

I hope this will help you keep your integration tests code DRY and maintainable!

Happy coding!

Tuesday, January 28, 2014

Writing selenium tests with style!

I know it's been more that a while sine I last blogged but there's actually a good reason for it: I've been reviving a legacy application sentenced to death by decommission for the better part of the last year. With success I might add :) Now everybody wants a piece of it - but that's not what I wanted to talk about...

During this exercise beside obvious technology updates like switching to Tomcat from a God forsaken version of some other application server and making the project actually compile in a continuous integration environment, yata yata yata,  I've fiddled a little bit with selenium tests to make them at least do a sanity check while doing all those naughty changes. In doing so I've realized that actually Selenium can be a real beauty if used properly.

Without further due let's jump into the meat!

The problem

Selenium tests are integration tests and they are hard to write and maintain

The solution

This is actually not so hard and if you structure your tests properly, apply some rules and enforce a couple of design decisions then writing those tests is actually extremely easy and fun!

Let's see how we can turn the beast into a beauty!

Originally the recipe that you get when you arrive to Selenium's getting started page presents you with the basics (and for a good reason): create the driver, navigate to a page, search for an element, interrogate the element for some information. This works really nicely if all you want to do is automate Google's search engine and nothing else.

If you want to build some structure around the pages you test the Page Object design pattern comes to the rescue. Unfortunately the original documentation fails to mention the @FindBy annotation which is crucial for making the code look good and perform as expected.

Let's take a look at the following: we have an application (a hello-world style one) that presents one heading on the page with the text "Hello, world!". To describe the page using the @FindBy annotation simply declare the field as a WebElement and annotate it with @FindBy(id = "greeting")

public class HomePage {
    @FindBy(id = "greeting")
    public WebElement greeting;
}

Now to initialize such a page object instantiate it as you'd normally do

HomePage page = new HomePage();

and later on use the PageFactory.initElements(driver, page) method to initialize a set of proxies to elements. The "proxies" element is crucial: they don't need to appear on the page up front and you get all the usual stuff like waiting for them to load absolutely for free! It's like magic - only better :)

The Problem

Applications have structure and I need to repeat lots of elements

The Solution

Applications have structure. Their individual pages are not disconnected from each other, each presenting a totally different content. Usually pages are contained in some form of layout that'll act as a common experience for the user. Luckily for us we have decided to describe pages as classes and classes can do ... inheritance!

Let's say the HomePage is of some system that has a layout with logout button for the users to take a leave if they get bored using it.

public abstract class ApplicationPage {
  @FindBy(id = "logout")
}

Next we declare our HomePage as before, only denoting the application structure using inheritance:

public class HomePage extends ApplicationPage {
    @FindBy(id = "greeting")
    public WebElement greeting;
}

And you instantiate it as usual:

HomePage page = new HomePage();
PageFactory.initElements(driver, page);

This way we're expressing an "is-a" relation between the layout page and the actual page: The home page is an application page. In Java 8 we'll be able to put some of the stuff directly into interfaces and have even more capability to construct pages from functional bits and pieces. Until now for most cases this is more than enough.

The Problem

My integration tests are unreadable because of all the fiddling with Selenium.

The Solution

This is actually a huge problem, not only with Selenium or any other integration/unit tests. At least in Unit tests we have the universal layout ("given/when/then" or if you're more a Microsoft type guy then it'd be "arrange/act/assert"). But what about integration tests? They are expected to have assertions in the middle, they are expected to manipulate our application in many ways and verify the state in the middle because that's what the user would do!

No worry! There's this perfect little design pattern called fluent interface that we can easily employ to nicely structure our tests and to have cool, reusable place for everything! Let's start with the description of the test we're about to write:

- the user navigates to the login page
- verify that the login prompt actually says "Username" and "Password"
- upon entering the proper credentials and clicking "Login" the user lands on the home page
- verify that the login succeeded by checking the page's header or some other element

I suggest you take the description and follow it to the letter using fluent interface which might look something like this:

public class LoginTest {
    @Test
    public void will_login_properly() {
        new LoginPage("http://localhost/myapp")
            .assertHasProperUsernameLabel()
            .assertHasProperPasswordLabel()
            .enterCredentials("johndoe", "secret")
            .clickLogin()
            .assertPageTitleIs("Welcome to this cool application!");
    }
}

As you can see I've deliberately shifted all the specifics to the page objects, including navigation which is especially neat if you're working in an IDE and if you already have a set of page objects to work with. That way the IDE will actually tell you what you can do after you have "executed" a particular action (clickLogin for example).

Constructing such page objects isn't anything extremely sophisticated. Let's take a look at the LoginPage:

public class LoginPage {
    private final WebDriver driver;

    @FindBy(id = "username")
    WebElement username;

    @FindBy(id = "username-label")
    WebElement usernameLabel;

    @FindBy(id = "password")

    WebElement password;

    @FindBy(id = "password-label")
    WebElement passwordLabel;

    @FindBy(id = "login-button")
    WebElement loginButton;

    public LoginPage(WebDriver driver, String url) {
        this.driver = driver;
        if (url != null && url.length() > 0) {
            driver.navigate().to(url);
        }
    }

    public LoginPage assertHasProperUsernameLabel() {
        Assert.assertEquals("Username:", usernameLabel.getText());
        return this;
    }

    public LoginPage assertHasProperPasswordLabel() { ... }


    public LoginPage enterCredentials(...) { ... }

    public HomePage clickLogin() {
        loginButton.click();
        return new HomePage(driver);
    }
}

As you can see the assertions are contextual and can be re-used at will in any test scenario. The same goes for any operation you'd normally run on pages like filling in the login form and clicking on the login button and you can reuse them as many times as you want. If you combine that with inheritance you'll get a extremely powerful way to describe your application in an object-oriented way. With the fluent interface you'll get a chance to exercise that model in a way that will not make your eyes bleed when you'll get back to the code to fix that one button's location that has moved and all of the sudden all integration tests are failing.

The Problem

My integration tests run every time I build the project using Maven and it takes to much time

The Solution

The solution lies in proper Maven project configuration. Maven has this nice idea of profiles. I'm using a profile called "integration-test" to actually run everything integration-test-related only if that profile is enabled.

It's quite a lot of XML as you can probably imagine so I've prepared an example project that demonstrates the configuration. The crucial part in all this is the configuration of surefire, failsafe and tomcat7 plugins. You can grab the example here. If you do a mvn clean install then no integration tests will get executed but with mvn clean install -Pintegration-test you'll see Tomcat starting for the duration of tests and all the Selenium tests executed against that it.

Epilogue

We've been using this way of writing unit tests with great success for more than a few months now. It works great if you have regular web applications (request/response) and adopting it to any dynamic pages isn't all that difficult mainly because the @FindBy annotation does such an amazing job of hiding the complexity of Selenium.

Happy integration testing!