How to automate image upload into your TestLink report using Selenium

Wavda Aniva
3 min readJul 31, 2021

--

In this article I’d like to share how we can integrate Selenium with TestLink to keep track each test execution result along with the test snapshots.

“TestLink is most widely used web based open source test management tool. It synchronizes both requirements specification and test specification together. User can create test project and document test cases using this tool.”

Pre-requisites:

  1. Selenium & JUnit5
  2. TestLink Installed

Dependencies:

<dependency>
<groupId>com.github.kalokanand</groupId>
<artifactId>testlink-api-client</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>br.eti.kinoshita</groupId>
<artifactId>testlink-java-api</artifactId>
<version>1.9.20-1</version>
</dependency>

Here’s how you can automate uploading Selenium test result and execution attachment into TestLink. In your TestSetup.java file you can add a method which will take a snapshot at the end of every execution e.g.

public static String testCaseId;
public static String fileLocation;
public static String imageFileName;
private static File scrFile;
@BeforeAll
public void beforeAll(){
driver = new ChromeDriver();
}
@AfterEach
public void after(){
fileLocation = "target/report/"+classname+"/";
imageFileName = System.currentTimeMillis() + ".png";
scrFile=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File(filePath));
}
@AfterAll
public static void afterAll(){
driver.quit();
}

Each Test Method in Selenium might represent one Test Case as well in TestLink, therefore you need to specify in which TestLink TestCaseId you’d like to upload this test’s result. Here’s a sample test file, TestLogin.java:

@Test
void testLogin(){
testCaseId = "TC-01-01-01";
driver.get("https://www.medium.com");
assertTrue(driver.getTitle().contains("Medium"));

driver.findElement(login-popup-button).click();
driver.findElement(login-email-button).click();

driver.findElement(email-textbox).sendKeys(myEmail);
driver.findElement(continue-button).click();
driver.findElement(password-textbox).sendKeys(myPassword);
driver.findElement(login-button).click();
assertTrue(driver.findElement(profile-menu).isDisplayed());
}

A TestWatcher in JUnit5 will allow you to execute certain steps based on test result whether it passed or failed:

public class CustomTestWatcher implements TestWatcher {  @Override
public void testFailed(){
testResult = TestLinkAPIResults.TEST_FAILED;

try {
ReportResult.reportResult();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void testSuccessful(){
testResult = TestLinkAPIResults.TEST_PASSED;

try {
ReportResult.reportResult();
} catch (Exception e) {
e.printStackTrace();
}
}
}

As TestLink stores execution history of each Test Case ID, uploading test execution status will require the following steps in ReportResult.java:

public static void reportResult() throws Exception {
updateExecutionStatus();
getExecutionId();
uploadScreenshot();
}
  1. Update Execution Status for a specific Test Case ID
private static void updateExecutionStatus() throws Exception {
DEVKEY = YOUR-TESTLINK-USER-API-KEY;
URL="https://testlink.exmple.com/lib/api/xmlrpc/v1/xmlrpc.php");
TestLinkAPIClient api = new TestLinkAPIClient(DEVKEY, url);
String testPlan = "Automation Plan";
String testProject = "My Project"
String build = "Build1";
reportOutput = api.reportTestCaseResult(testProject, testPlan,
testCaseId, build, testNote, testResult); }

2. Get Execution ID from the latest Test Case ID History

private static Integer getExecutionId(){
String executionResult = reportOutput.toString();
String executionIdValue = executionResult.split("[=\\,]")[2];
return Integer.parseInt(executionIdValue); }

3. Upload the Base64 encoded image file into the acquired Execution ID

private static void uploadScreenshot(String imagePath){
File attachmentFile = new File(imagePath);
String fileContent = null;
try {
byte[] byteArray = FileUtils.readFileToByteArray(attachmentFile);
fileContent = new String(Base64.encodeBase64(byteArray));
}

catch (IOException e) {
e.printStackTrace( System.err );
System.exit(-1);
}
Integer executionId = getExecutionId();
System.out.println("Uploading "+imagePath+" into "+executionId);
TestLinkAPI api = new TestLinkAPI(testLinkUrl,DEVKEY);
api.uploadExecutionAttachment(
executionId, //execution Id
"Execution for "+testCaseId, //attachment title
"Automation execution for"+testCaseId, //attachment description
fileLocation+imageFileName, //fileName
"image/png", //fileType
fileContent); //content
System.out.println("Attachment uploaded"); }

This concludes how we can automate our Selenium tests reporting into TestLink. There are so many references out there which shares how to automate reporting test status on TestLink from Selenium but it was quite confusing how to automate the overall flow until upload image process so this process has taught me a lot. Here’s some of the references that I’ve used:

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Wavda Aniva
Wavda Aniva

Written by Wavda Aniva

A curious potato exploring new things on software quality

No responses yet

Write a response