Skip to main content

Upload PDFs via Java SDK

Prerequisites for Using Smart UI

  • Java 8 or higher installed on your system
  • Maven or Gradle build tool
  • Familiarity with Java development
  • Visit the LambdaTest SmartUI page and log in with your credentials.
  • Obtain your LT_USERNAME and LT_ACCESS_KEY by clicking on the Access Key button, located at the top right corner of your dashboard.

Step 1: Establishing a SmartUI Project

To initiate a SmartUI PDF Comparison Project, adhere to the following instructions:

  1. Navigate to the SmartUI Projects Page.
  2. Tap on the new project button.
  3. Specify your platform type as PDF.
  4. Provide your project name, designate approvers, and add tags (optional).
  5. Confirm your entry by clicking on Submit.

Once your project is active, retrieve your Project Token from the application. Here's an example of a project token:

projectToken = "123456#1234abcd-****-****-****-************"

Step 1: Clone the Sample Project

First, clone the sample project to get started:

git clone https://github.com/LambdaTest/junit-selenium-sample.git
cd junit-selenium-sample

Step 2: Install the SmartUI Java SDK

Add the SmartUI Java SDK to your pom.xml:

<dependency>
<groupId>io.github.lambdatest</groupId>
<artifactId>lambdatest-java-sdk</artifactId>
<version>1.0.18</version>
</dependency>

Then compile your project:

mvn clean compile

Step 3: Set up your credentials

export LT_USERNAME="${ YOUR_LAMBDATEST_USERNAME()}"
export LT_ACCESS_KEY="${ YOUR_LAMBDATEST_ACCESS_KEY()}"
export PROJECT_TOKEN="123456#1234abcd-****-****-****-************"

Step 4: Upload PDFs using Java SDK

You can upload PDFs in two modes:

Upload pre-existing PDFs from your local machine:

📁 Sample File: SmartuiPdfLocalTest.java

import io.github.lambdatest.SmartUIConfig;
import io.github.lambdatest.SmartUIPdf;
import io.github.lambdatest.models.FormattedResults;

public class SmartuiPdfLocalTest {
public void uploadLocalPdf() throws Exception {
String projectToken = System.getenv("PROJECT_TOKEN");

SmartUIConfig config = new SmartUIConfig()
.withProjectToken(projectToken)
.withFetchResult(true);

SmartUIPdf pdfUploader = new SmartUIPdf(config);

// Upload PDF file
String pdfPath = "path/to/your/document.pdf";
FormattedResults result = pdfUploader.uploadPDF(pdfPath);

System.out.println("Upload result: " + result);
}
}

Step 5: Configuration Options

MethodDescription
.withProjectToken(token)Required. Your SmartUI project token.
.withFetchResult(true)Optional. Returns structured test results.
.withBuildName("v2.1")Optional. Assign a custom build name.

Step 6: Run your tests

mvn test

Advanced Java SDK Usage

Batch Upload Example

public class SmartuiPdfBatchTest {
public void uploadMultiplePdfs() throws Exception {
String projectToken = System.getenv("PROJECT_TOKEN");

SmartUIConfig config = new SmartUIConfig()
.withProjectToken(projectToken)
.withFetchResult(true)
.withBuildName("Batch-Upload-v1.0");

SmartUIPdf pdfUploader = new SmartUIPdf(config);

String[] pdfPaths = {
"documents/report1.pdf",
"documents/report2.pdf",
"documents/specification.pdf"
};

for (String pdfPath : pdfPaths) {
FormattedResults result = pdfUploader.uploadPDF(pdfPath);
System.out.println("Uploaded " + pdfPath + ": " + result);
}
}
}

Error Handling

public class SmartuiPdfErrorHandling {
public void uploadWithErrorHandling() {
try {
String projectToken = System.getenv("PROJECT_TOKEN");

SmartUIConfig config = new SmartUIConfig()
.withProjectToken(projectToken)
.withFetchResult(true);

SmartUIPdf pdfUploader = new SmartUIPdf(config);
FormattedResults result = pdfUploader.uploadPDF("document.pdf");

System.out.println("Upload successful: " + result);

} catch (Exception e) {
System.err.println("Upload failed: " + e.getMessage());
e.printStackTrace();
}
}
}

Use Cases

  • Enterprise Applications: Integrate PDF testing into large-scale Java applications
  • Test Automation Frameworks: Build comprehensive test suites with PDF validation
  • CI/CD Integration: Automate PDF testing in Java-based deployment pipelines
  • Custom Tools: Develop specialized tools for PDF comparison and validation

Test across 3000+ combinations of browsers, real devices & OS.

Book Demo

Help and Support

Related Articles