How to Use WebDriverExtensions in Ocaramba: A Complete Guide with Code Examples
WebDriverExtensions in the accenture/ocaramba framework provide fluent extension methods for IWebDriver that simplify navigation, synchronization, and JavaScript execution in Selenium tests.
The accenture/ocaramba testing framework extends Selenium WebDriver with a comprehensive set of helper methods that eliminate repetitive boilerplate code. By leveraging WebDriverExtensions, you can perform complex browser operations—such as waiting for Angular applications to stabilize or handling Internet Explorer certificate warnings—with single, readable method calls defined in the Ocaramba.Extensions namespace.
What Are WebDriverExtensions?
All extension methods reside in the Ocaramba.Extensions namespace and are implemented as static classes that operate on driver instances via the this keyword. The core implementation lives in WebDriverExtensions.cs, which groups together helpers for navigation, waiting, window handling, scrolling, and Angular synchronization.
These extensions are designed to be fluent: you call them directly on the driver instance (e.g., this.Driver.NavigateTo(uri)) and chain them with other Selenium calls. Because they are ordinary static methods, they can be unit-tested independently and mocked when needed.
Navigation and Page Handling
NavigateTo with IE Certificate Support
The NavigateTo extension method wraps the standard Selenium navigation and automatically handles Internet Explorer certificate warnings. According to the source code in WebDriverExtensions.cs (lines 64-70), the method calls ApproveCertificateForInternetExplorer after navigation to suppress IE security dialogs without polluting your test code.
var url = new Uri("https://the-internet.herokuapp.com/");
this.Driver.NavigateTo(url);
Verifying Page State
Use IsPageTitle to wait for an exact title match within a specified timeout, or PageSourceContainsCase to search for text in the page source with optional case sensitivity.
bool titleOk = this.Driver.IsPageTitle("Secure Area", BaseConfiguration.ShortTimeout);
bool textFound = this.Driver.PageSourceContainsCase(
"Welcome",
BaseConfiguration.MediumTimeout,
ignoreCase: true);
Synchronization and Waiting Strategies
Waiting for AJAX Completion
The WaitForAjax method blocks execution until jQuery reports no active XMLHttpRequest objects (jQuery.active == 0). This is essential for testing applications that load data asynchronously.
// Use default timeout from BaseConfiguration
this.Driver.WaitForAjax();
// Specify custom timeout in seconds
this.Driver.WaitForAjax(30);
Angular Application Synchronization
For Angular applications, use WaitForAngular to pause execution until $http.pendingRequests.length == 0. You can also toggle Angular synchronization globally using SynchronizeWithAngular.
// Wait for Angular HTTP requests to complete
this.Driver.WaitForAngular(BaseConfiguration.MediumTimeout);
// Disable Angular synchronization if needed
this.Driver.SynchronizeWithAngular(false);
Element Presence Checks
Instead of wrapping element lookups in try-catch blocks, use IsElementPresent to safely check visibility within a timeout period.
var loginBtn = new ElementLocator(By.Id("login"));
bool isVisible = this.Driver.IsElementPresent(
loginBtn,
BaseConfiguration.MediumTimeout);
Window Management and Interactions
Switching Windows by URL
When tests open pop-ups or new tabs, use SwitchToWindowUsingUrl to iterate through driver.WindowHandles and switch to the handle matching the requested URL.
var popupUrl = new Uri("https://example.com/popup");
this.Driver.SwitchToWindowUsingUrl(popupUrl, BaseConfiguration.ShortTimeout);
Scrolling and Actions
The ScrollIntoMiddle method calculates the element's Y-offset and scrolls the page so the element ends up vertically centered. For complex interactions, the Actions method returns a fresh OpenQA.Selenium.Interactions.Actions builder.
// Scroll element to center of viewport
var target = new ElementLocator(By.CssSelector("#large-list .item:nth-child(50)"));
this.Driver.ScrollIntoMiddle(target);
// Perform complex interactions
this.Driver.Actions()
.MoveToElement(this.Driver.GetElement(target))
.Click()
.SendKeys("Hello")
.Perform();
JavaScript Execution and Alert Handling
Executing JavaScript
The JavaScripts method exposes the underlying IJavaScriptExecutor, allowing you to run arbitrary scripts.
var js = this.Driver.JavaScripts();
string title = (string)js.ExecuteScript("return document.title;");
HTML5 Drag and Drop
When standard Selenium actions fail with HTML5 drag-and-drop interfaces, use DragAndDropJs to inject a JavaScript snippet that simulates the interaction.
IWebElement source = this.Driver.GetElement(new ElementLocator(By.Id("source")));
IWebElement destination = this.Driver.GetElement(new ElementLocator(By.Id("target")));
this.Driver.DragAndDropJs(source, destination);
Handling JavaScript Alerts
The JavaScriptAlert extension method returns a strongly-typed wrapper defined in JavaScriptAlert.cs. This object provides methods to read alert text, accept, dismiss, or send keys without directly accessing Selenium's IAlert interface.
// Accept an alert
this.Driver.JavaScriptAlert().ConfirmJavaScriptAlert();
// Read and dismiss alert
var alert = this.Driver.JavaScriptAlert();
string message = alert.JavaScriptText;
alert.DismissJavaScriptAlert();
Related Extension Points
The WebDriverExtensions architecture connects to several other components:
- WebElementExtensions.cs - Provides element-level helpers such as
JavaScriptClick,SetAttribute, andGetTextContentthat work on anyIWebElement. - BaseConfiguration.cs - Holds timeout constants (
ShortTimeout,MediumTimeout, etc.) used by all waiting helpers to ensure consistent timing across the test suite.
Summary
- WebDriverExtensions in
OcarambaLite/Extensions/WebDriverExtensions.csprovide fluent, static extension methods forIWebDriver. - Key capabilities include navigation with IE certificate handling, AJAX and Angular waiting, and window switching.
- Use
WaitForAjaxfor jQuery applications andWaitForAngularfor Angular apps to ensure elements are ready before interaction. - The
JavaScriptAlertwrapper andDragAndDropJsmethod handle complex browser scenarios that standard Selenium cannot. - All timing operations rely on
BaseConfigurationconstants to maintain consistent timeouts across your test suite.
Frequently Asked Questions
How do I enable WebDriverExtensions in my Ocaramba project?
Simply ensure you have imported the Ocaramba.Extensions namespace at the top of your test files. The extension methods become available automatically on any IWebDriver instance, such as the Driver property in your page objects.
What is the difference between WaitForAjax and WaitForAngular?
WaitForAjax checks for jQuery-specific activity by evaluating jQuery.active == 0, making it suitable for applications using jQuery for asynchronous requests. WaitForAngular waits for Angular's $http.pendingRequests array to empty, which is necessary for AngularJS or Angular applications. Use the method that matches your application's JavaScript framework.
Can I use WebDriverExtensions with browsers other than Internet Explorer?
Yes, all extensions work across Chrome, Firefox, Edge, and other Selenium-supported browsers. The NavigateTo method specifically includes logic for IE certificate warnings, but this does not interfere with other browsers—it safely executes only when IE is detected.
Where are the timeout values configured for WebDriverExtensions?
All waiting methods use timeout constants defined in BaseConfiguration.cs, such as ShortTimeout, MediumTimeout, and LongTimeout. You can override these values in your application configuration file or customize them per call by passing a specific timeout value (in seconds) as the method parameter.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →