Skip to main content
Version: Latest

Fail-Safe Image Loading Strategy

Overviewโ€‹

Brobot implements a fail-safe approach to image loading that ensures automation continues even when images are missing or cannot be loaded. This strategy prioritizes robustness and continuous execution over strict validation.

Core Philosophyโ€‹

Missing images should never break automation execution. Instead, they should:

  • Log clear error messages for debugging
  • Result in failed pattern matches (not exceptions)
  • Allow the automation to continue and potentially recover

This approach is essential for GUI automation where:

  • Images might be temporarily unavailable during development
  • Network issues might prevent loading remote images
  • File system permissions might block access
  • Images might be intentionally removed or renamed

Implementation Detailsโ€‹

StateImage Pattern Loading Behaviorโ€‹

When creating a StateImage with pattern images:

import io.github.jspinak.brobot.model.state.StateImage;

StateImage button = new StateImage.Builder()
.addPatterns("button.png")
.setName("Button")
.build();

The pattern loading follows these rules:

  1. In Mock Mode: Image loading is skipped entirely

    • Internal Pattern objects have null images
    • Pattern names are extracted from filenames
    • No file system access occurs
    • StateImage is created successfully
  2. In Live Mode - Image Found:

    • Images are loaded into memory
    • Patterns are fully functional
    • Debug log confirms successful loading
    • StateImage contains valid Pattern objects
  3. In Live Mode - Image Missing:

    • No exception is thrown
    • Error is logged: "Failed to load image: button.png. Pattern will have null image and find operations will fail."
    • Internal Pattern objects have null images
    • StateImage is still created successfully
    • Find operations using this StateImage will fail gracefully

Spring Initialization Handlingโ€‹

During Spring context initialization, image loading is automatically deferred to avoid unnecessary failures. Internally, Pattern objects detect when Spring is initializing and defer image loading:

// Internal Pattern implementation (for reference)
if (isSpringContextInitializing()) {
// Defer loading until Spring context is ready
this.needsDelayedLoading = true;
this.image = null;
return;
}

Images are loaded lazily on first use after Spring initialization completes. This is handled automatically by Brobot - no user action required.

Error Handling Strategyโ€‹

What Happens When Images Are Missingโ€‹

  1. Pattern Creation: Succeeds with null image
  2. Find Operations: Return empty/failed results
  3. Click/Type Actions: Fail gracefully with appropriate ActionResult
  4. State Transitions: May fail if dependent on missing images
  5. Automation Flow: Continues, allowing for recovery logic

Example Recovery Patternโ€‹

import io.github.jspinak.brobot.action.Action;
import io.github.jspinak.brobot.action.ActionResult;
import io.github.jspinak.brobot.model.state.StateImage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

// StateImage with potentially missing image
StateImage submitButton = new StateImage.Builder()
.addPatterns("submit.png")
.setName("Submit Button")
.build();

// Find operation handles missing images gracefully
ActionResult result = action.find(submitButton);

if (!result.isSuccess()) {
// Recovery logic
Logger logger = LoggerFactory.getLogger(getClass());
logger.warn("Submit button not found, trying alternative approach");

// Try alternative pattern
StateImage alternativeSubmit = new StateImage.Builder()
.addPatterns("submit-alt.png")
.setName("Alternative Submit")
.build();
result = action.find(alternativeSubmit);
}

Benefits of This Approachโ€‹

1. Development Flexibilityโ€‹

  • Start writing automation before all images are captured
  • Test automation logic without complete image sets
  • Iterate quickly without image dependency blocks

2. Production Resilienceโ€‹

  • Temporary file system issues don't crash automation
  • Network failures for remote images are handled gracefully
  • Partial image sets still allow partial functionality

3. Testing Advantagesโ€‹

  • Mock mode tests run without any image files
  • Unit tests don't require image resources
  • CI/CD pipelines work without image assets

4. Clear Debuggingโ€‹

  • Error logs pinpoint exactly which images failed to load
  • Automation continues, showing full execution path
  • Easy to identify and fix missing image issues

Best Practicesโ€‹

1. Always Check ActionResultsโ€‹

import io.github.jspinak.brobot.action.ActionResult;
import io.github.jspinak.brobot.model.state.StateImage;

StateImage button = new StateImage.Builder()
.addPatterns("button.png")
.build();

ActionResult result = action.click(button);
if (!result.isSuccess()) {
// Handle the failure appropriately
}

2. Provide Fallback Patternsโ€‹

StateImage supports multiple patterns natively - Brobot automatically tries each pattern until one succeeds:

import io.github.jspinak.brobot.action.ActionResult;
import io.github.jspinak.brobot.model.state.StateImage;

// StateImage with multiple pattern variations
StateImage submitButton = new StateImage.Builder()
.addPatterns("submit-button.png", "submit-text.png", "ok-button.png")
.setName("Submit Button")
.build();

// Brobot automatically tries all patterns
ActionResult result = action.find(submitButton);
if (result.isSuccess()) {
action.click(submitButton);
}

3. Use Descriptive Image Namesโ€‹

Image names should be self-documenting since they appear in error logs:

  • โœ… login-submit-button.png
  • โœ… main-menu-file-option.png
  • โŒ img1.png
  • โŒ button.png

4. Monitor Logs in Productionโ€‹

Set up log monitoring to catch image loading failures:

# application.properties
logging.level.io.github.jspinak.brobot.model.element.Pattern=ERROR

Configuration Optionsโ€‹

Logging Levelsโ€‹

Control how image loading failures are reported:

# Show all image loading attempts
logging.level.io.github.jspinak.brobot.model.element.Pattern=DEBUG

# Show only failures (recommended for production)
logging.level.io.github.jspinak.brobot.model.element.Pattern=ERROR

# Suppress image loading logs entirely
logging.level.io.github.jspinak.brobot.model.element.Pattern=OFF

Mock Mode Configurationโ€‹

# Enable mock mode - no images are loaded
brobot.mock=true

# Disable mock mode - images are loaded from disk
brobot.mock=false

Testing Considerationsโ€‹

Unit Testsโ€‹

  • Mock mode automatically skips image loading
  • Tests run without any image files present
  • Focus on logic, not image availability

Integration Testsโ€‹

  • Can verify image loading behavior
  • Test both success and failure paths
  • Ensure proper error logging occurs

Example Testโ€‹

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import io.github.jspinak.brobot.action.Action;
import io.github.jspinak.brobot.action.ActionResult;
import io.github.jspinak.brobot.model.state.StateImage;
import io.github.jspinak.brobot.test.BrobotTestBase;

@SpringBootTest
public class FailSafeImageLoadingTest extends BrobotTestBase {

@Autowired
private Action action;

@Test
void shouldHandleMissingImageGracefully() {
// Create StateImage with non-existent image
StateImage stateImage = new StateImage.Builder()
.addPatterns("non-existent.png")
.setName("Non-existent Image")
.build();

// StateImage creation succeeds
assertNotNull(stateImage);

// StateImage contains patterns (even with null images internally)
assertFalse(stateImage.getPatterns().isEmpty());

// Find operations fail gracefully (no exceptions thrown)
ActionResult result = action.find(stateImage);
assertFalse(result.isSuccess());
}
}

Summaryโ€‹

The fail-safe image loading strategy ensures that Brobot automations are robust, maintainable, and production-ready. By handling missing images gracefully rather than failing fast, automations can:

  • Continue execution despite missing resources
  • Provide clear diagnostic information
  • Support gradual development and deployment
  • Maintain stability in production environments

This approach aligns with the reality of GUI automation where image availability cannot always be guaranteed, and partial functionality is often better than complete failure.

Testing & Mock Modeโ€‹

Pattern Managementโ€‹

Error Handling & Debuggingโ€‹

Configurationโ€‹

Core Conceptsโ€‹

Capture Systemโ€‹