2010. június 4., péntek

Selenium Framework Structure - Java

Hi Guys!

Today i want to talk about a topic that is not often talked about. This is the structure of the framework you are building for automated testing and how you could build a framework that is easily maintained and can be expanded any time.

These are general suggestions your implementation may wary based on your environment.

And they will be made using java.

So here goes nothing...

Chapter 1. Technology

What kind of technology do you should use when you want continues integration, nightly builds, any easily executed tests.

First of all... Let go over the basics:

1.1. Continues integration

This means that you always have some functions implemented that you always build integrate and test and that mostly all of these are happening automatically. This means that there is a server or some kind of software that runs builds over night or at weekends if the build is to big.

Example softwares that are free of use are: Hudson, Cruise Control, Apache Maven, Cascade, Buildbot, and so on and so fort. The mainly used are Cruise Control, Hudson, Maven and maybe rationals tool.

These can be configured to build your application every night. These tools work with lots of configuration and lot of ant/nant scripts.

1.2. Automatic Building

So you set up your environment and you have your Maven / Hudson / whatever set and ready to go... What you need now is a building tool. For this there are two great building tools that are used all over the world. These are Ant and Nant. Where nant is a .NET version of ant really.

Ant can be used to build everything really. And it has a lot of other functions too, like. Creating directory, moving files, executing tasks, integrating behavior and lots more.

These you will use to create build.xml-s that build your automation framework and executes the tests you got.

1.3. Test run framework

Next you need a framework that will organize and run your tests. These usually are xUnit where x can be a lot of things like j for java and n for .NET and r for Ruby and p for PHP and so on. And then for java there is testng what i'm currently using. It is a great tool but you have to decide for yourself which one you will use.

1.4. Web Automation Framework

There are a LOT of good automation frameworks out there. And i mean a LOT.

I will just name a few of the big ones.

  • Selenium
  • WebDriver
  • Watir
  • Watin
  • Ranorex
  • Sahi
  • WebAction
So there... These are a LOT of tools. I'm using Selenium currently because i believe that they will rule over all in time. :) For a huge list look at this: http://www.softwareqatest.com/qatweb1.html

Chapter 2. Main structure

So let us talk about how your environment should look like in java...

2.1. Modularity and basic principles

For your environment to work properly you have to consider a few things first...

First, that it will expand beyond any comprehension. This means that whatever you think how big it will get, IT WILL GET BIGGER! You will have over thousands of files that will contain over thousands of code and other stuff.

You will have to write a structure that can handle this kind of load. And you have to write it so that can handle maintenance. That means that if something breaks than you wont lose your head trying to find the problem. And then fixing the problem introduces another couple of problems.

That is something we don't want.

And it can be easily avoided for that matter. You don't need so much fancy Design Patterning or to complicated stuff. There are only a few rules that you have to follow.

Everybody for himself.

Means that everything you write every object every file only needs to be aware of himself. It only needs to take care of stuff regarding to that object only.

For example you have a class that creates ui elements that you work with. That means that everything regarding ui objects is handled in that class. It means that if some kind of problem occurs with an element, like the element is not found, then you know that you have to look in that class and that class only. ( And in the tests because sometimes you just forgot something or misspelled it :P )

KISS - Keep it Simple Stupid.

This is a basic concept and it works. You don't need to fancy tests and scripts that a programmer would create. These are programs of course but they need to be kept as simple as possible. Why? Because not all of your testers have a degree in programing science or even care to have one.

You need to keep it simple for maintainability too because the simpler the easier to edit and poke in it.

Everything in one place.

This is similar to everybody for himself. It means that everything you write or edit needs to be in one place only. This means ui elements, configurations, variables, names, users, passwords, settings, instantiations and so on and so fort.

For example you have selenium and you can start it in more then one way. Than you should have a class dealing with JUST selenium. Or you have a couple of settings like usernames you are using to test with. These should be kept in Java property files.

Chapter 3. Hard facts

So let go forth and present you with an example. Just so you see all these things working together.

For example let's say you have to automate a web page.

Technologies you are using are:

  • Selenium
  • TestNg
  • SimpleXml for Java
  • ReportNG - Reporting for TestNG - Nice piecharts
  • Ant
  • Hudson
Okey. What you want to achieve. You want your tests to run every night and you want your developers to run these tests before they commit. At least a portion of them not all. Just for example the login screen.

First you build your framework.

You have selenium in a separate class taking care of.

You have Base classes for All your tests taking care of functions that all tests should run, like setUp and tearDown.

Your tests should look something like this: doLogin; checkStatus; doLogout;

This is easily achieved too. You just need to hide your logic from your tests. Have test classes and then have Implementation classes. The test would only look like this:

public void loginTest()
LoginPageImplementation loginPageImpl = new LoginPageImplementation(seleniumInstance);


Tada.... You have your test. It is easily readable. You know what it does although your don't have a clue what the implementation looks like. The implementation would be something like this:

public void doLogin()

Tada.. Of course there are a bunch of more stuff to it like waiting for a page, and checking if the login was a success but again. It is really simple and you know what it does.. Why?

Here is a trick hidden again. What does username.xpath mean? Well selenium works with locators. These locators find elements on the web page.

Again like i said every man for himself. You need to encapsulate the ui objects on a webpage. How you do that is completely up to you.

I'm using xml Serialization. That is why i'm using SimpleXML for java. I have a class that holds UI objects and an XML telling how does objects are working.

For example:

The xml would look something like this:

And your object hierarchy would look like UIElement - Elements. And in Elements you would have WebElement value... ( Look under the documentation for SimpleXML it is really simple! )

This way if something on the page changes you again have to look only at ONE PLACE!!

This is really important in ways of maintainability because if something fails and you have to look at at last 5 places to find a bug... well that is a sign you are doing something wrong.

Chapter 4. Last words

So i hope that helped... Any constructive comments are appreciated and tell me if something was too complicated or left out...

Stay Tuned for the next Post on which i explain how to put it together with ANT TestNG and Hudson! :)


Nincsenek megjegyzések:

Megjegyzés küldése