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
LambdaTest 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 LambdaTest 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
Troubleshooting
- PDF Upload Fails
- Project Not Found Error
- Upload Returns Null or Empty Result
- Maven Dependencies Not Resolving
- Batch Upload Partially Fails
- PDFs Not Appearing in Dashboard
Issue: PDF Upload Fails
Symptoms: PDF upload returns error or fails silently
Possible Causes:
- Invalid PDF file
- File path incorrect
- File size too large
- Network connectivity issues
- Project token incorrect
Solutions:
-
Verify PDF file is valid and not corrupted:
file document.pdf -
Check file path is correct:
File pdfFile = new File(path/to/document.pdf");
if (!pdfFile.exists()) {
throw new FileNotFoundException("PDF file not found");
} -
Verify file size is within limits
-
Check network connectivity to LambdaTest servers
-
Verify PROJECT_TOKEN is set correctly:
echo $PROJECT_TOKEN
Issue: Project Not Found" Error
Symptoms: Error message indicating project cannot be found
Possible Causes:
- Incorrect project token
- Project deleted or renamed
- Token from wrong project
Solutions:
- Verify project exists in SmartUI dashboard
- Copy project token directly from Project Settings
- Ensure token includes the project ID prefix (e.g.,
123456#...) - Check for extra spaces or quotes in token
Issue: Upload Returns Null or Empty Result
Symptoms: Upload completes but result is null or empty
Possible Causes:
withFetchResult(false)or not set- Network timeout
- Server-side processing error
Solutions:
-
Enable result fetching:
config.withFetchResult(true); -
Check upload response:
FormattedResults result = pdfUploader.uploadPDF(pdfPath);
if (result == null) {
// Handle null result
} -
Review error logs for server-side issues
-
Retry upload if transient error
Issue: Maven Dependencies Not Resolving
Symptoms: Maven cannot find lambdatest-java-sdk or dependencies fail
Possible Causes:
- Incorrect dependency version
- Maven repository access issues
- Network connectivity problems
Solutions:
- Check latest version on Maven Central
- Clear Maven cache:
mvn clean - Verify internet connectivity for Maven repository access
- Check pom.xml for version conflicts
Issue: Batch Upload Partially Fails
Symptoms: Some PDFs upload successfully, others fail
Possible Causes:
- Individual file issues
- Network interruptions
- Timeout issues
- File size limits
Solutions:
-
Implement individual error handling:
for (String pdfPath : pdfPaths) {
try {
FormattedResults result = pdfUploader.uploadPDF(pdfPath);
System.out.println(Uploaded: " + pdfPath);
} catch (Exception e) {
System.err.println("Failed: " + pdfPath + " - " + e.getMessage());
}
} -
Verify each file individually
-
Check file sizes and formats
-
Implement retry logic for failed uploads
Issue: PDFs Not Appearing in Dashboard
Symptoms: Uploads complete but PDFs don't appear in SmartUI dashboard
Possible Causes:
- Incorrect project token
- Project name mismatch
- Upload not completed
- Dashboard refresh needed
Solutions:
- Verify PROJECT_TOKEN is correct
- Check project name matches exactly (case-sensitive)
- Wait a few moments and refresh dashboard
- Check upload response for errors
- Review test execution logs
Getting Help
If you encounter issues not covered here:
- Review the Comprehensive Troubleshooting Guide for detailed solutions
- Check PDF Comparison Overview for PDF-specific information
- See PDF API Upload for alternative upload methods
- Visit LambdaTest Support for additional resources
- Contact support at support@lambdatest.com or use 24/7 Chat Support
