Skip to content

How to add the new programming language support

Nozomi Ito edited this page Mar 27, 2015 · 22 revisions

You can add the new programming language support to Sahagin by implementing the Sahagin runtime library for the language you want to support. (See [Sahagin basic architecture](Basic architecture))

The Sahagin runtime library is called during test execution, and generates the [intermediate data](Intermediate data format). You have to implement the following two logic in the Sahagin runtime library.

  • The SrcTree generation logic, which analyzes the test class definition and test method code body statically and generates the SrcTree.
  • The RunResults generation logic, which hooks to the test method call and test code line execution at runtime and generates the RunResults.

Sahagin runtime library implementation example in Java

We explain how the Java Sahagin runtime libary for JUnit4 and WebDriver is implemented. This explanation will help you implement the Sahagin runtime library for the new language.

SrcTreeGenerator

This logic generates SrcTree.

SrcTreeGenerator reads all java text files (not class files) for test code and analyzes them by the ASTParser, which is the core module of the JDT and used to parse Java source code and generate the abstract syntax tree in Eclipse IDE.

SrcTree generation by ASTParser is two step process.

First, visit the all method ASTs (Abstract Syntax Trees) generated from the test source code by the ASTParser, and generate the following tables.

  • rootMethodTable: generated from the methods annotated by @Test annotation.
  • rootClassTable: generated from the classes which have root methods.
  • subMethodTable: generated from the methods annotated by @TestDoc annotation.
  • subClassTable: generated from the classes which have sub methods but do not have root method.

All code bodies of TestMethods are still empty at this step.

Second, visit the all method ASTs again, and generate code bodies of the TestMethods from the method code body ASTs.

We get the SrcTree object on memory after these two step, and then convert it to YAML string and write to srcTree file.

RunResultsGenerateHookSetter

This logic generates RunResults and related screen captures.

RunResultsGenerateHookSetter catches every invocations of the method annotated by @TestDoc, and gets the screen capture for each invocation and the corresponding stack trace lines, and error information on test failure. The invocation catch is implemented with the Java byte code manipulation library, Javassist.

RunResultsGenerateHookSetter checks each line of each method, and if invocation of the method annotated by @TestDoc is found, adds the hook event after the method invocation line by byte code manipulation with Javassist. The event takes screen capture and gets Java stackTrace information and generates stack trace lines data from the already generated SrcTree information.

In some programming languages which do not have the powerful byte code manipulation library like Javassist, it may be difficult to hook each code line of the method. If you implement the Sahagin runtime library for such languages, you can hook each method invocation by some AOP library or any other method invocation listener mechanism.

In this case, you should take screen capture only for the code line top level method invocation. For example, given the following code:

AssertThat(page.getUser(), is("user"));

Three methods (AssertThat, getUser, is) will be hooked, but should take screen capture only for AssertThat method. Taking screen captures too many times makes test slow, and besides, screen captures other than code line top level method are not used and just ignored in Sahagin HTML report.

Clone this wiki locally