Saturday, September 6, 2014

MailCatcher REST client for emails testing

Sometimes it's important to verify emails content while web applications testing, or retrieve it to achieve some high level goals, e.g. password reset scenario. Besides that, we also need to keep in mind that customers shouldn't be spammed with test emails.

In this article we'll look at MailCatcher (MC) tool  - very simple SMTP server that is quite good for emails testing. Its key advantages are:
  • not spamming real users (it catches emails, preventing further sending to recipients list);
  • provides REST APIs for retrieving / deleting emails;
  • allows to read emails in json, html, plain text + full source;
  • has web UI;
  • cross-platform.
Note: you must install Ruby and SDK before setting up MC.
After MC installation, you'll be able to call mailcatcher from command line. It also has additional arguments, e.g. if you need to set custom ip or port:


Now we can open its web UI for accessing caught emails info:


So what's next? As I've mentioned above, MC provides some useful REST APIs for retrieving and deleting caught messages. It means that we can easily implement a simple REST client (e.g. via Jersey) for further content testing. Let's look at emails getting / deleting APIs:


You can find a full source code with some basic examples on GitHub.

Tuesday, September 2, 2014

Monitoring network activity via BrowserMob Proxy + HarStorage

Sometimes it's hard to understand a root cause of an issue while testing web applications, because bugs may not be as obvious to appear on UI. In such case it's important to pay attention to browser's network tab that can give us a lot of useful information about captured requests, e.g. internal server / JS errors.

We can capture network traffic automatically via BrowserMob Proxy that provides us useful REST API. After downloading and unzipping, you can start browsermob-proxy.bat to raise proxy server. Default port is 8080. You can change it by executing batch with -port argument.


Now we can use this proxy for listening traffic on a Selenium port. Generally you should only add an appropriate driver capabilities:


Where seleniumProxy configured to use BrowserMob proxy ip and allocated port:


To access BrowserMob proxy REST API, we can implement a simple REST client using Jersey. For example, to get a HAR file as String, we can send the following request:


You can find a link on a full source code below. And now let's focus on a second part of this article.

BrowserMob proxy allows us to retrieve HAR file and put it into stream. So we can easily save output json into file. It makes perfect sense when we need to run or debug just several tests. But what if we want to capture traffic for hundreds or thousands of tests? It would be much harder to find needed file. Besides that, we would also need to use some external tools to analyze generated HARs.

To make HAR files analysis more flexible, we can use HAR Storage. Just follow installation guide to raise the server for storing generated HAR statistics. By default it uses 5000 port.


To save generated by BrowserMob proxy HAR file into storage, we can use appropriate REST API:


If you run some tests and open HAR storage UI via browser, you can see the following user-friendly statistics:


You can find full source code with BrowserMob proxy and HAR Storage REST clients + simple Selenium test example on GitHub. Note that provided pom.xml already contains plugin for building a jar, if you want to add it into your project. Just exclude selenium / testng libraries and run clean install goals.

Sunday, July 6, 2014

Custom locators

In this article we'll talk about custom locators implementation. Well, I guess some of you would probably ask - why do we need to reinvent the wheel, if it already exists in a form of FindBy annotation + WebElement bunch or just By class?

Let's start with some basics. I prefer using By class, as it's more flexible. But readability and bulkiness of the following construction:


- cries 'refactor me', unlike FindBy + WebElement bunch, which is pretty laconic and straightforward. So the first reason of such customization was aspiration of decoration to increase readability and code understanding.

Let's assume we've decorated our locator. What other advantages could this technique give us? Well, now we can solve a problem of creating locators on fly (dynamic locators). Actually, there's no problem, but for me it's quite strange to create locators dynamically within PageObjects' methods. Such 'no name' elements are dropped from a real page model. Yes, someone can say that in such case we need to use PageElement pattern, but it doesn't make sense, when we use only common page controls.

Saving space. Let's assume that we have lots of identical elements without unique attributes, which require quick access for any kind of interaction, e.g. getText or click. We can create dozen of locators, which differ only by index or text or just use 'no name' dynamic locator within PageObject's method. In first case our PageObject will swell and become hard to read. And of course it's a moveton to use indexes or text inside locators, as it wouldn't be possible to support them. Second case's disadvantage was described earlier.

Passing custom parameters. Custom locators gives us an opportunity to pass custom parameters to any decorated element. As you may know, we can't do this via pure Selenium annotations or By class.

Now let's look at implementation. We would annotate all the elements with HTML annotation:


Let's decorate By class:


This wrapper implements updateElement method, which allows us to create locators on fly. We'll see how it looks like in further PageObject's implementation.

Now we need to provide some way of custom locators' initialization. We can achieve this via java reflection within abstract page class:


And we shouldn't care which page has triggered elements' initialization. This mechanism supports chain inheritance, when we need to move through all child pages to their parent one.

Sample PageObject may look like the following:


As you can see, this example contains both static and dynamic locators, which are marked with special ? symbol within path. This symbol will be replaced with missing member (which is needed for elements' unique identification on a page) in runtime.

You can pull a full example from GitHub.

Tuesday, July 1, 2014

FirePath upgrade: saving locators in a java properties format

Nowadays there are lots of useful tools, that could help QA automation engineers in their routing. When we talk about web automation, one of the most important part is related to elements' locators preparation to create reliable page objects. I guess the most popular tool for elements' inspecting is FireBug with its FirePath addon. I like FirePath features, but it looks incomplete for me. It's really annoying to copy/paste every single element and always switch between IDE and browser. So it would be useful to have some feature to save locators with custom names in e.g. java properties format, without leaving browser's focus.

In this article I'll show you, how to upgrade FirePath by adding such feature.

To start developing Firefox extensions, first read XUL guide. It will help you to understand common structure of Firefox addons.

Let's checkout FirePath project from SVN repository. You can do it via any SVN client or directly from IDE. Next we need to create a new project from existing sources. Actually, Mozilla developers recommend to use Kommodo Edit tool, that supports XUL syntax. But for such small feature, it would be enough to use any IDE, that supports XML / JS syntax.

Mostly we would only need content/bindings.xml file. It contains FirePath UI components implementation. You know that generally we can find any locator by activating special inspecting button and clicking needed element on UI. Depending on selected menu item (XPath / CSS), FirePath will automatically populate text field with full / optimized locator. Let's assume that we want to set some name to found locator, and save it to file. So besides existing components we would also need 2 inputs (locator name / file path) and 1 button (save). Let's add labels first for our new components. All labels' entities are located in locale/en-US/FirePath.dtd.


Now let's create appropriate inputs / button in content/bindings.xml after menu (XPath / CSS) toolbaritem section.



As you see, syntax is pretty straightforward. I'll just pay attention to newly created Save button. It contains a special oncommand handler. It means that when we click this button, saveLocator method will be executed. Let's look at its implementation.


It's a simple JS code that reads a file by a given path and adds new / replaces existing locator by name. By default it uses java properties (key/value) format, but you can easily add your own one. Ideally, this part should be splitted into read / write methods, but as it's only example, it's up to you how to decorate it. locatorName, selector and filePath are simple properties to provide an easy access to textbox values.


To avoid custom fields validation, I've also added a new CSS class into skin/classic/FirePath.css to ignore default one for textbox items.



To build modified FirePath extension, just run ant command inside root folder. After that, you can install it from Firefox addons section. You can look at how it works by downloading and installing already modified extension. Or alternatively you can pull sources from GitHub.

Final FirePath panel view would be the following:


Wednesday, June 25, 2014

Test execution steps logging with AspectJ

When we talk about custom reports, the most common question is what should be inside? What information could be useful for team or management? I saw different reports' implementations with a good level of details, but often people forget to put one important thing - execution steps.

Let's imaging some hand-written test case. It contains id / name, description, steps and some expected result. If we open any good bug report, we would see the same detailed description with steps, actual / expected results, etc. Such kind of details level has a positive impact on issue's root cause analysis, as well as on its fixing time.

When we develop tests, such frameworks as TestNG or JUnit put test name and assertion results into final report. But what's about test execution steps? When we look at report's body, who can say what was exactly tested in particular test? We can only assume real scenario. That's why I've started looking at a way of implementing such functionality. Of course someone can point that Thucydides already has such functionality, but as I've mentioned in my previous article, sometimes it's very expensive to switch to another framework only because it has a good reporting engine.

So how we can log our test execution steps? It could be done via proxy or AOP. In this article I'll talk about test execution steps logging with AspectJ.

To use AspectJ functionality, you need to include the following dependency and plugin:



Let's assume we're using PageObject pattern. In such case test steps would be PageObject's methods. So the main goal is to intercept PageObject's methods calls and save their names in some container to get further access, when we need to print steps into report. That can be easily done with aspects. But as you know, PageObjects may contain some useless for test results report methods (getters, helpers, etc.). So let's assume we want to filter methods to print only real test logic without any redundant calls. This can be achieved via custom annotation.


We can use this annotation for marking methods we want to publish into report.


Finally, we're ready to create a simple aspect that will intercept all the methods calls annotated with Publish annotation inside particular package:


After finishing test execution, you can easily print intercepted steps, that were saved inside ThreadLocal container. Let's see how could it look like in test results report:


You can find a source code example on GitHub.

Sunday, June 22, 2014

Custom reporting engine with ReportNG

I've seen a lot of threads with the same question: how to create a good, reliable test results report, which would be useful for automation team, as well as for developers, manual QAs, management? This question is very popular nowadays, as common JUnit / TestNG reports are pretty ugly, and it doesn't make sense to extend them, as it would be definitely easier to create a new one from scratch. But what if you don't have appropriate skills to do that? In such case people start looking at existing solutions. There's a good one, which is a part of Thusydides framework. It's pretty nice and straightforward, but what if you already have your own framework? It's nonsense moving to another framework only because it contains a good report. Well, it would be also very expensive. In such case we need some external module, which is also compatible with TestNG or JUnit. In this article I'll talk about ReportNG, which is a simple HTML reporting plugin for TestNG framework.

Some of you may ask: why do we need to talk about custom reporting in a ReportNG context? Just add it into project and use! Well, it's a good point, but what if we want to add some specific info into our report? What if we want to change its structure? In such case it wouldn't be enough to add it into project. We would also need to understand its source code to push necessary changes. In this article I'll help you to understand its common components.

As I've mentioned above, ReportNG is a TestNG plugin, so to understand its structure, first you need to figure out with TestNG. To add some visual customization or inject your own custom data, you also have to read about Velocity templates syntax. And of course by default you should be familiar with Java, as it's a ReportNG host language.

To get started, first you need to pull ReportNG sources. Just create your fork and copy appropriate URL. I prefer using IntelliJ IDEA, sorry to Eclipse fans. IntelliJ allows you to clone sources directly from GitHub by provided URL:




After finishing cloning, IDE prompts you to create a new project. We will skip this step. Let's look at project's structure:


Generally, you would only need HTMLReporter / AbstractReporter and ReportNGUtils classes from main package. First one is intended for report's context preparation / generation. It sets different custom properties into Velocity context. It also extends AbstractReporter class, where you have to pay attention to createContext() method. It's very important for further customization.


All the properties we set via VelocityContext are acceptable within velocity templates, that represent report's body. It means that if we override this method, we can pass any custom properties / data into velocity templates. Next question is: how to override it? It can be easily done by creating our own TestNG listener, that extends HTMLReporter class. Such kind of extension allows us to move though inheritance chain and reach AbstractReporter class, which contains needed method with protected modifier.


As you see, I set custom properties and data into VelocityContext from overrriden createContext() method inside BaseHTMLReporter listener.

To force TestNG using our custom listener, just add it via maven-surefire-plugin.


Let's move on. ReportNGUtils provides us a set of useful helpers, that can be also accessed within velocity templates. As you saw from original and overridden createContext() methods, both use UTILS_KEY to set ReportNGUtils object into context. BaseHTMLReporter implementation uses extended version of ReportNGUtils class with overridden getTestOutput method to inject screenshot link into test results output.


Note that this is only a part of code. It's up to you to take care about creating screenshot and setting appropriate link into results context, as it's out of this guide's scope.

Now let's take a look at ReportNG resources. First, you should pay attention to reportng.properties. It contains all the messages displayed in test results report. You'll find a way of how to access them within velocity templates: $messages.getString(key); - where messages is a ResourceBundle object, that is set into VelocityContext inside AbstractReporter class.

Our last part is related to velocity templates. I won't pay attention to details, as it's up to you how you're going to customize your report's body. But let's take a quick look at their structure.

All the listed in html folder vm templates (their html representations) will appear in your results folder after tests execution is finished. So you can easily compare final result's body with its template, if you're familiar with HTML / JS / CSS. So to apply your custom styles or structure, you'll need to modify templates, rebuild plugin and update dependency in pom.xml.

As I've mentioned before, you can access all your custom properties or objects within velocity templates, if you previously set them via overridden createContext() method. For example, if I set testCasesInfo object, I can use it the following way:


By default IntelliJ doesn't highlight vm templates' syntax. But it can be easily configured. Just follow this guide.

Cloned project already contains Ant build.xml. So you can easily create your custom ReportNG version and install it into local maven repository. Note that ReportNG doesn't use latest TestNG and Velocity versions. So you should take care about updating them manually. Besides adding your updated ReportNG dependency, you should also add Velocity and Guice dependencies into pom.xml.


And here are some examples of how could ReportNG be customized:



You can find some sample sources on GitHub.