Mock Stochasticity in Brobot
โ ๏ธ IMPORTANT NOTE: This document describes the
mockFindStochasticModifierfield that exists in the State model but is not currently used by the mock framework. Mock behavior is currently determined by ActionHistory alone. This field is reserved for future enhancements.
Overviewโ
Brobot's mock mode uses ActionHistory to simulate realistic behavior by replaying historical action outcomes. The mockFindStochasticModifier field exists as a probability modifier but is not currently integrated with the mock framework.
For information on how mock mode actually works, see:
- Mock Mode Guide - Comprehensive mock mode documentation
- ActionHistory Mock Snapshots - How ActionHistory determines mock behavior
- Mock Mode Manager - Mock mode configuration
The mockFindStochasticModifier Fieldโ
Each State in Brobot has a mockFindStochasticModifier field that represents an additional probability factor for whether the state's images will be found during mock runs.
Purposeโ
The mockFindStochasticModifier field simulates environmental uncertainties that can affect GUI automation:
- Network failures: Connection issues that prevent pages from loading
- Unexpected overlays: Update windows, notifications, or popups that cover target elements
- Rendering issues: Elements with transparent backgrounds or poor contrast that make detection unreliable
- Timing variations: Elements that appear/disappear at unpredictable times
- System interruptions: OS-level dialogs, screen savers, or other interruptions
How Mock Mode Actually Worksโ
โ ๏ธ Current Implementation: Mock behavior is determined by ActionHistory alone. The
mockFindStochasticModifierfield exists but is not currently consulted by the mock framework.
Current Mock Behavior (as implemented in MockFind.java):
-
ActionHistory-Based Matching: The mock framework uses
ActionHistorystored in eachPatternto determine find results:- Retrieves historical match snapshots from past runs
- Selects a random snapshot matching the current action type and active states
- Returns the historical match results (matches found, locations, etc.)
- If no history exists, returns a default successful match
-
No Probability Calculations: Currently, there is no random failure injection or probability-based decision making. Mock results are deterministic based on ActionHistory.
For details on how ActionHistory works, see:
Planned Future Enhancement: mockFindStochasticModifierโ
The mockFindStochasticModifier field is reserved for future enhancement to add probabilistic failure injection:
- Would act as an additional probability modifier
- Could simulate random environmental failures
- Would work in conjunction with ActionHistory
Setting the Field (For Future Use)โ
The field can be set via State.Builder, though it currently has no effect:
import io.github.jspinak.brobot.model.state.State;
// Programmatic approach using State.Builder
State loginState = new State.Builder()
.setName("LoginScreen")
.setBaseMockFindStochasticModifier(95) // Field exists but not currently used
.build();
// Note: In Brobot 1.1.0, states are typically defined using @State annotations
// on classes, not programmatically. See the States Guide for modern patterns.
For modern state definition patterns, see States Guide.
Implementation Notesโ
Framework Behaviorโ
The Brobot framework's StateMemory class does NOT modify mockFindStochasticModifier during state transitions:
โ Actual StateMemory Behavior:
// From StateMemory.java:
// "Note: mockFindStochasticModifier is NOT modified here as it's only relevant
// for mock mode and should be controlled by the mock framework, not by state activation."
The field is designed to be:
- Set during state definition
- Managed by mock configuration (when future implementation is complete)
- NOT modified during state transitions
TransitionExecutor Behaviorโ
The TransitionExecutor class resets the field to base values:
import io.github.jspinak.brobot.model.state.State;
// TransitionExecutor calls this method when activating states:
state.setProbabilityToBaseProbability(); // Resets to baseMockFindStochasticModifier
This ensures state probability returns to configured base values after transitions.
Configurationโ
Setting Base Values via State.Builderโ
When using the programmatic approach, set the base value via the builder:
import io.github.jspinak.brobot.model.state.State;
// Using State.Builder (programmatic approach)
State myState = new State.Builder()
.setName("MyState")
.setBaseMockFindStochasticModifier(90) // Currently not used, reserved for future
.build();
Note: The modern Brobot 1.1.0 approach uses
@Stateannotations on classes rather than programmatic builders. The@Stateannotation does not currently have a parameter forbaseMockFindStochasticModifier. See States Guide for current patterns.
Common Values (For Future Use)โ
โ ๏ธ Note: These values are illustrative for when the feature is implemented. Currently, the field has no effect on mock behavior.
- 100: Perfect availability (default value)
- 95-99: Highly stable states (main navigation, home screens)
- 85-94: Normal states (typical application screens)
- 70-84: Less stable states (dynamic content, Ajax-heavy pages)
- 50-69: Unreliable states (states with frequent overlays or interruptions)
How ActionHistory Currently Works in Mock Modeโ
โ Current Implementation: Mock behavior is entirely driven by ActionHistory.
The mock framework uses ActionHistory to create realistic test behavior:
-
Historical Snapshot Selection: When a find operation occurs in mock mode:
- MockFind retrieves ActionHistory from the Pattern
- Selects a random historical snapshot matching:
- Current action type (FIND vs VANISH)
- Active states
- Find strategy (BEST, EACH, ALL)
- Returns the historical match results
-
Realistic Variation: By replaying actual historical outcomes, mocks naturally include:
- Successful finds with actual match locations
- Failed finds when historically no matches were found
- Varying match counts and positions
- Realistic timing (via mock timing configuration)
-
Default Behavior: If no ActionHistory exists, MockFind returns a default successful match, allowing tests to run even without recorded history.
For detailed information:
- ActionHistory Mock Snapshots - Creating and using ActionHistory
- Action Recording - Recording action history for mocks
- ActionHistory Integration Testing - Testing with ActionHistory
Best Practices for Mock Testingโ
Current Best Practices (ActionHistory-Based)โ
-
Record ActionHistory: Build comprehensive ActionHistory data for realistic mocks
- Run automation in real mode to capture actual behavior
- Include both success and failure scenarios
- See Action Recording
-
Use Mock Mode for Fast Testing: Enable mock mode for unit and integration tests
- Tests run in ~10-40ms instead of seconds
- No screen capture or GUI interaction required
- See Mock Mode Guide
-
Test with BrobotTestBase: All tests should extend BrobotTestBase
- Automatic mock mode configuration
- Headless environment support
- See Test Utilities
-
Use Profile-Based Testing: Create different test profiles
- Development: Full ActionHistory for comprehensive testing
- CI/CD: Minimal ActionHistory for fast builds
- See Profile-Based Testing
Future Practices (When mockFindStochasticModifier Is Implemented)โ
- Set realistic probability values: Model actual state reliability
- Document state stability: Explain why certain states have lower availability
- Don't modify at runtime: Keep configuration static for reproducible tests
Testing Strategiesโ
Testing with ActionHistoryโ
Create realistic test scenarios by recording and replaying ActionHistory:
import io.github.jspinak.brobot.test.BrobotTestBase;
import io.github.jspinak.brobot.model.action.Action;
import io.github.jspinak.brobot.model.state.StateImage;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import static org.junit.jupiter.api.Assertions.*;
public class ActionHistoryTest extends BrobotTestBase {
@Autowired
private Action action;
@Test
public void testWithHistoricalBehavior() {
// Mock mode is automatically enabled by BrobotTestBase
assertTrue(isMockMode());
// The mock framework uses ActionHistory to determine find results
// Behavior varies based on recorded history:
// - If history shows 80% success, mock will replay mixed results
// - If history shows 100% success, mock will always succeed
// - If no history exists, mock returns default success
// Your test logic here using the Action API
// Results will be based on ActionHistory, not random probability
}
}
Testing with Custom ActionHistoryโ
import io.github.jspinak.brobot.test.BrobotTestBase;
import io.github.jspinak.brobot.actions.actionHistory.ActionHistory;
import io.github.jspinak.brobot.model.state.StateImage;
import io.github.jspinak.brobot.model.patterns.Pattern;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeEach;
public class CustomHistoryTest extends BrobotTestBase {
private StateImage testImage;
@BeforeEach
@Override
public void setupTest() {
super.setupTest();
// Create StateImage with Pattern
testImage = new StateImage.Builder()
.addPatterns("test-pattern")
.setName("Test Image")
.build();
// ActionHistory can be built programmatically for testing
// See ActionHistory Mock Snapshots guide for details
}
@Test
public void testWithMockMode() {
// Test behavior using ActionHistory-driven mocks
// See ActionHistory Integration Testing guide for patterns
}
}
For complete testing patterns, see:
Conclusionโ
Current Stateโ
Brobot's mock mode currently uses ActionHistory to create realistic test behavior by replaying historical action outcomes. This provides:
- Fast test execution (~10-40ms per action)
- Realistic variation based on actual past behavior
- Headless testing support
- Deterministic but varied test results
The mockFindStochasticModifier field exists in the State model as a reserved field for future probabilistic enhancements.
Using Mock Mode Effectivelyโ
For effective mock-based testing:
- Build ActionHistory: Record real automation runs to capture actual behavior patterns
- Use BrobotTestBase: Extend BrobotTestBase for automatic mock mode configuration
- Test comprehensively: Include both success and failure scenarios in ActionHistory
- Profile-based testing: Use different test profiles for development vs CI/CD
See the comprehensive testing guides:
- Mock Mode Guide - Complete mock mode documentation
- Testing Introduction - Overview of testing approaches
- ActionHistory Mock Snapshots - Building ActionHistory data
Related Documentationโ
Core Mock Mode Documentationโ
- Mock Mode Guide - Comprehensive guide to mock mode
- Mock Mode Manager - Centralized mock mode management
- Mock Mode Migration - Migrating to MockModeManager
ActionHistory Documentationโ
- ActionHistory Mock Snapshots - Creating and using ActionHistory
- Action Recording - Recording action history
- ActionHistory Integration Testing - Testing with ActionHistory
Testing Guidesโ
- Testing Introduction - Overview of Brobot testing approaches
- Unit Testing - Unit testing patterns with BrobotTestBase
- Integration Testing - Integration test patterns with Spring
- Profile-Based Testing - Profile-specific test configurations
- Test Utilities - BrobotTestBase and testing utilities
- Testing Strategy - Overall testing strategy
State Managementโ
- States Guide - Foundational guide to States and @State annotation
- States - Theoretical Foundations - Theoretical model of states
Configurationโ
- BrobotProperties Usage - Configuring via properties
- Properties Reference - Complete property reference
Advanced Topicsโ
- Enhanced Mocking - Advanced mock scenarios
- Debugging Pattern Matching - Troubleshooting pattern matching