Tuesday, July 17, 2012

Testing Tridion implementation with Watir

A while ago my friend was telling me how they are testing their web application and he was really enthusiastic  about the Watir-library that they use. He told me that it makes writing the code for the tests a breeze and adding Cucumber into play makes the creating of the test cases possible for any non-techie. Obviously I had to try what all the fuzz was about.


Watiring Tridion

So what I decided to do was to try and automate some of the basic actions in Tridion. I wanted to build some kind of a Tridion-framework on top of Tridion that would provide easy access to the most commonly used actions and then write the test scripts on top of my own framework. Looking at the examples on the Watir-site I thought it was going to be a walk in the park. I should've known better.

Enough said I had some issues, some of them were solved, some of them weren't. The main problem I had was the excessive amount of IFrames and popups used in Tridion 2009 and some weird bugs in Watir/Watir-webdriver when handling both of those. I noticed that Watir-webdriver generally works better except that it is painfully slow to parse the contents of IFrames and in 2009 there were quite a few nested ones. You can see from the code that I tried to solve the problem by using the XPath and Nokogiri to interact with elements but it only solved it partially (it was faster but still slow). Tridion 2011 was a way easier to work with, with the exception of few dynamically generated IFrames due to a bug in ChromeDriver.

The second problem I had was getting past the authentication. Solving that took a lot of effort:

  1. How to get past it without having to handle the pop-up --> Basic HTTP Authentication
  2. Figuring why it's not picking the credentials, seems that browsers have security settings that by default they don't allow it, so how to get past it --> On Chrome an extension, on IE a registry key change
  3. So how to load the extension to Chrome (ChromeDriver always loads a "clean" browser without any extensions or saved logins). I tried several ways to read the extension and this simple one worked in the end.
  4. Tridion started throwing errors if the URL contained the credentials. Solution: reload the CME URL without the credentials as they are saved in the session.
In any case I managed to achieve my goal, I made a framework for the basic actions and some example scripts to show how to use it. It is far from perfect at the moment but I think it is quite cool that you can automate your browser to create some components for you, and with some additional work it can even create the schemas for them as well ;)

So your next question might be something like "Why would I need this? I can use Core Service to create components". Well, the answer is simple, with Watir you're testing the functionality of CME, in other words you're testing that your users see what they should see when they perform the same actions. For example, you can test that if a specific field (that is checked by Event System) has an invalid value the Event System throws a correct error message. Or you can test that when an user clicks on your GUI Extension he will see a correct pop-up and not some error messages. Automating these kind of tests can be a real life-saver when there are multiple people working on an implementation. You can pick up most of the problems as soon as they appear and not when somebody happens to find them.

Interested yet? Let's see how you can try this for yourself...

Installation of necessary tools

I presume that most of the readers are Windows-users, so I will describe the steps for installing the necessary stuff for that (and if you happen to be a *nix-user you probably already have Ruby installed and know how to use it).
  1. Download Ruby 1.9.3 and Ruby DevKit from  http://rubyinstaller.org/downloads/
  2. Run the installer for Ruby and extract the DevKit to some location (doesn't really matter where, I usually put it under Ruby/DevKit)
  3. Follow the installation instructions for DevKit at  https://github.com/oneclick/rubyinstaller/wiki/Development-Kit
  4. Update Rubygems with command "gem update --system"
  5. Install Bundler with command "gem install bundler"
For editing the source-files you can use any text-editor. I highly recommend to try out Sublime Text 2. Since I tried it out I have used it for pretty much everything (HTML, JS, Java, Ruby, C++, Lua...). It's not free but it is well worth the money (the evaluation version doesn't have any limitations, but you should support good software), just keep in mind that it is not an IDE, it's a text editor and heck of a good one. If you don't believe me, just check some articles out there. And when you decide to install it, remember to get some useful plugins and to learn the most useful tricks.

Running the examples

Running the scripts is quite straightforward. Just clone the GitHub-repository and follow the instructions. Obviously you need access to some Tridion-environment to be able to see the scripts in action.

Tip: if you installed ST2 you can just open the whole directory of the repository and run the scripts straight from the editor by pressing CTRL+B (build system needs to be set as Ruby).

Creating your own scripts

The examples are quite simple so you probably want to create your own test cases and you probably want some documentation on how to do it. Well, unfortunately there is no other documentation available than the code itself. That is simply because I kept changing things daily so any documentation would've been outdated instantly. The other reason is that Ruby, Watir and the framework are so verbose that you should easily be able to figure out how to use the framework just by reading the example scripts. Or what do you think these lines do:


Looks simple enough, right? The only thing that you might wonder why it looks like you change the title twice, and the reason for that is simple. The first one changes the actual title of the component and the second one changes a user-defined called "title" (don't ask me why someone created a field named "title" in the schema).

Also the framework in its current state might not be enough for your needs so you might want to extend it. That might be little more difficult especially if you're a newcomer to Ruby and Watir, but I left a lot of commented code and comments to explain certain choices and to explain the more complex solutions so that should help you get started. If you decide to extend it I hope that you fork the repository and share your code as well!

What next?

So the obvious next step would be to extend the framework but what I personally find more interesting at this point is to try and plug in the Cucumber-framework to allow non-techies to write the test cases (or maybe I just want to see those nice green lines when running the tests ::think). I will definitely try and implement it at some point but you're free to contribute and beat me to it. (Hint: There are already good examples how to do it)


Ps. This is actually my first real program written in Ruby, so don't shoot me if I did something wrong :) All the feedback is welcome like always!

No comments:

Post a Comment