Upload PDFs via Java SDK
Prerequisites for Using SmartUI
- Java 8 or higher installed on your system
- Maven or Gradle build tool
- Familiarity with Java development
- Visit the
TestMu AI SmartUIpage and log in with your credentials. - Obtain your
LT_USERNAMEandLT_ACCESS_KEYby clicking on theAccess Keybutton, 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:
- Navigate to the SmartUI Projects Page.
- Tap on the
new projectbutton. - Specify your platform type as
PDF. - Provide your
projectname, designateapprovers, and addtags(optional). - 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
- MacOS/Linux
- Windows - CMD
- PowerShell
export LT_USERNAME="${YOUR_LAMBDATEST_USERNAME}"
export LT_ACCESS_KEY="${YOUR_LAMBDATEST_ACCESS_KEY}"
export PROJECT_TOKEN="123456#1234abcd-****-****-****-************"
set LT_USERNAME="${YOUR_LAMBDATEST_USERNAME}"
set LT_ACCESS_KEY="${YOUR_LAMBDATEST_ACCESS_KEY}"
set PROJECT_TOKEN="123456#1234abcd-****-****-****-************"
$env:LT_USERNAME="${YOUR_LAMBDATEST_USERNAME}"
$env:LT_ACCESS_KEY="${YOUR_LAMBDATEST_ACCESS_KEY}"
$env:PROJECT_TOKEN="123456#1234abcd-****-****-****-************"
Step 4: Upload PDFs using Java SDK
You can upload PDFs in two modes:
- Local Mode
- Cloud Mode
Upload pre-existing PDFs from your local machine:
"> 📁 Sample File: SmartuiPdfLocalTest.java
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);
}
}
Upload PDFs downloaded during TestMu AI cloud test execution:
"> 📁 Sample File: SmartuiPdfCloudTest.java
public class SmartuiPdfCloudTest {
public void uploadCloudPdf(WebDriver driver) throws Exception {
String projectToken = System.getenv(PROJECT_TOKEN");
// Download PDF from cloud session
String base64Content = (String) ((JavaScriptExecutor) driver)
.executeAsyncScript("lambda-file-content=LambdaTest.pdf");
// Convert base64 to PDF file
byte[] pdfBytes = Base64.getDecoder().decode(base64Content);
File pdfFile = new File("downloaded.pdf");
try (FileOutputStream fos = new FileOutputStream(pdfFile)) {
fos.write(pdfBytes);
}
// Upload to SmartUI
SmartUIConfig config = new SmartUIConfig()
.withProjectToken(projectToken)
.withFetchResult(true);
SmartUIPdf pdfUploader = new SmartUIPdf(config);
FormattedResults result = pdfUploader.uploadPDF(pdfFile.getAbsolutePath());
System.out.println("Upload result: " + result);
}
}
Step 5: Configuration Options
| Method | Description |
|---|---|
.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
Best Practices
- PDF File Management
- Project Token Management
- Build Naming
- Error Handling
- Batch Processing
- Batch Processing
PDF File Management
- Use consistent naming conventions for PDF files
- Organize PDFs in logical directory structures
- Keep PDF files in version control when appropriate
- Document PDF sources and purposes
Example:
String[] pdfPaths = {
documents/reports/report-v1.0.pdf",
"documents/specs/spec-v2.1.pdf"
};
Project Token Management
- Store project token as environment variable
- Never commit tokens to version control
- Use different tokens for different environments
- Rotate tokens regularly
Build Naming
- Use meaningful build names that include version info
- Include date or version in build names
- Use consistent naming conventions
Example:
config.withBuildName(PDF-Comparison-v1.0-" + LocalDate.now());
Error Handling
- Always wrap upload calls in try-catch blocks
- Log errors for debugging
- Handle network failures gracefully
- Implement retry logic for transient failures
Batch Processing
- Process PDFs in batches for efficiency
- Monitor upload progress
- Handle partial failures in batch operations
- Use appropriate batch sizes
Batch Processing
- Process PDFs in batches for efficiency
- Monitor upload progress
- Handle partial failures in batch operations
- Use appropriate batch sizes