Mastering Sahi: Advanced Techniques and Best Practices
Overview
Sahi is an automation and testing tool (browser automation and web testing). This guide covers advanced techniques to increase reliability, maintainability, and performance of Sahi test suites.
Test design & architecture
- Modularize tests: Break tests into reusable modules/functions (page objects or sections).
- Layered structure: Separate locators, test logic, and test data.
- Data-driven tests: Store inputs and expected outcomes in external files (CSV/JSON) and iterate tests.
- Idempotent tests: Ensure tests can run independently and repeatedly by resetting state between runs.
Locator strategies
- Prefer stable attributes: Use data-attributes, IDs, or stable class names.
- Relative locators: Locate elements relative to stable anchors (e.g., find(child, near(parent))).
- Fallback chains: Implement a primary locator and one or two fallbacks to handle minor UI changes.
- Avoid brittle selectors: Minimize reliance on XPath with long absolute paths or text that may change.
Advanced scripting techniques
- Custom functions/library: Build a shared utilities library (retry helpers, wait wrappers, logging).
- Smart waits: Replace fixed sleeps with conditional waits (waitFor, waitIf) tied to element state or network idleness.
- Retry patterns: Retry flaky operations with exponential backoff and clear failure thresholds.
- Parallel execution: Run suites in parallel using thread-safe design and isolated test data/environment per thread.
Handling asynchronous behavior
- Network-aware waits: Wait for AJAX calls to complete using request hooks or by checking network activity/response indicators.
- DOM stability checks: Wait for absence of loading spinners and for element dimensions/positions to stabilize before interacting.
- Event synchronization: Use explicit event listeners or confirm expected events (e.g., mutation observers) where appropriate.
Test data & environment management
- Seed & teardown scripts: Programmatically create and clean data fixtures to keep environments consistent.
- Environment configuration: Parameterize base URLs, credentials, feature flags; keep secrets secure.
- Service virtualization: Mock external services to make tests predictable and fast.
Reporting & observability
- Structured logs: Emit consistent logs with timestamps, test IDs, and step names.
- Screenshots & DOM snapshots: Capture on failure and on key checkpoints.
- Metrics: Track pass/fail rates, flakiness, and execution time per test to prioritize maintenance.
CI/CD integration
- Fail-fast vs. gating: Decide which suites block merges (smoke/regression) and which run asynchronously (long-running).
- Parallel runners & resource limits: Tune concurrency based on available browsers and CI quotas.
- Artifact retention: Store logs, screenshots, and videos for failed runs.
Flakiness reduction & maintenance
- Root-cause driven fixes: Prioritize fixing causes over adding retries.
- Test triage process: Tag flaky tests and assign owners with SLAs to fix or quarantine.
- Regular refactoring: Schedule periodic cleanup to remove obsolete tests and update selectors.
Security & compliance
- Handle secrets safely: Use vaults or CI secret stores; never hard-code credentials in scripts.
- Least privilege: Use test accounts with minimal access.
- Data privacy: Mask or avoid using real PII in test data.
Example snippets
- Wait wrapper (pseudo-Sahi):
javascript
function waitForVisible(locator, timeoutMs) { var start = new Date().getTime(); while (new Date().getTime() - start < timeoutMs) { if (_exists(locator) && _isVisible(locator)) return true; sleep(200); } throw “Timeout waiting for “ + locator; }
- Retry with backoff (pseudo-Sahi):
javascript
function retry(action, attempts) { var delay = 200; for (var i=0;i<attempts;i++) { try { return action(); } catch(e) { _sleep(delay); delay*=2; } } throw “All retries failed”; }
Checklist for production readiness
- Stable locators and modular code
- Smart waits and minimal sleeps
- Isolated, repeatable test data
- CI integration with artifacts retained
- Monitoring for flakiness and ownership
Date: February 3, 2026
Leave a Reply