Blog

Traceability Matrix

Automation Traceability Matrix

This article details how to use Cucumber to create an Automation Traceability Matrix.

Traceability report maps Requirements to Test Cases.

Automation Traceability report can be generated mapping Cucumber Scenarios to corresponding Test Scenarios.

Cucumber comes with Hooks and Tags that ca be added to test scenarios. These can be read and used to generate a PDF report.

To link a test scenario to a requirement add them as Tags. Then we can use @Before hook to read all the tags and @After hook to generate reports.

Add the following itextpdf generator dependency in pom.xml
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13.1</version>
</dependency>

Add the following code to @Before hook:
Add the following code to @After hook

package Automation.Application.Matrix;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;

import org.junit.runner.RunWith;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import cucumber.api.PendingException;
import cucumber.api.Scenario;
import io.cucumber.java.After;
import io.cucumber.java.Before;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
public class AutomationMatrix {
ArrayList tagScenario = new ArrayList();
static ArrayList tagAll = new ArrayList();
@Before
public void readallTags(Scenario scenario) {
String tag="";
tagScenario= (ArrayList) scenario.getSourceTagNames();
String scenarioName = scenario.getName();
for ( int count=0;count < tagScenario.size() ; count++) {
tag= tag +"_"+ tagScenario.get(count);
}
tagAll.add(scenarioName + "_" + tag);
}
@After
public void GenerateReport() {
int countOfScenarios = tagAll.size();
Document document = new Document();
try
{
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("src//test//java//Automation//Application//Matrix//AutomationTraceabilityMatrix.pdf"));
document.open();
document.add(new Paragraph("Automation Traceability Matrix"));
document.add(new Paragraph("Report Date - " + java.time.LocalDateTime.now()));
document.add(new Paragraph("Product Name - Booking"));
document.add(new Paragraph("Release Version - 3.5"));
PdfPTable table = new PdfPTable(3); // 3 columns.
table.setWidthPercentage(100); //Width 100%
table.setSpacingBefore(10f); //Space before table
table.setSpacingAfter(10f); //Space after table
//Set Column widths
float[] columnWidths = {1f, 1f, 1f};
table.setWidths(columnWidths);
PdfPCell cell1 = new PdfPCell(new Paragraph("Scenario"));
cell1.setBorderColor(BaseColor.BLUE);
cell1.setPaddingLeft(10);
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
cell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell1);
PdfPCell cell2 = new PdfPCell(new Paragraph("JIRA Requirement"));
cell2.setBorderColor(BaseColor.BLUE);
cell2.setPaddingLeft(10);
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell2);
PdfPCell cell3 = new PdfPCell(new Paragraph("Test case number"));
cell3.setBorderColor(BaseColor.BLUE);
cell3.setPaddingLeft(10);
cell3.setHorizontalAlignment(Element.ALIGN_CENTER);
cell3.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell3);
String s1="";
String s2="";
String s3="";
for (int rowCount=0; rowCount < countOfScenarios ;rowCount++) {
String[] scenarioTags= tagAll.get(rowCount).toString().split("_");
for ( String ss : scenarioTags) {
if(ss.contains("Verify")){
s1 = ss;
}
else {
if (ss.contains("JIRA")) {
s2 = ss;
}
else if (ss.contains("TCM")) {
s3=ss;
}
}
}
PdfPCell cell11 = new PdfPCell(new Paragraph(s1));
cell11.setBorderColor(BaseColor.GREEN);
cell11.setPaddingLeft(10);
cell11.setHorizontalAlignment(Element.ALIGN_CENTER);
cell11.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell11);
PdfPCell cell12 = new PdfPCell(new Paragraph(s2));
cell12.setBorderColor(BaseColor.GREEN);
cell12.setPaddingLeft(10);
cell12.setHorizontalAlignment(Element.ALIGN_CENTER);
cell12.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell12);
PdfPCell cell13 = new PdfPCell(new Paragraph(s3));
cell13.setBorderColor(BaseColor.GREEN);
cell13.setPaddingLeft(10);
cell13.setHorizontalAlignment(Element.ALIGN_CENTER);
cell13.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell13);
}
document.add(table);
document.close();
writer.close();
}
catch (DocumentException e)
{
e.printStackTrace();
} catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
}


The generated traceability matrix report:

Recent Blogs