How to Check if an Element Is Present Using Ocaramba’s Extensions
Use the IsElementPresent extension method on IWebDriver from Ocaramba.Extensions.WebDriverExtensions to safely verify element visibility without throwing exceptions.
The accenture/ocaramba framework provides robust Selenium wrappers that simplify common automation tasks. When you need to check if an element is present using Ocaramba, the IsElementPresent method offers a reliable way to poll the DOM and return a boolean result rather than handling raw Selenium exceptions in your test code.
Understanding the IsElementPresent Extension Method
The IsElementPresent method is defined in OcarambaLite/Extensions/WebDriverExtensions.cs at lines 111-126. It extends IWebDriver to provide a fluent API for presence checks.
The method signature follows this pattern:
public static bool IsElementPresent(
this IWebDriver driver,
ElementLocator locator,
double timeout)
Key characteristics:
- Returns
trueonly if the element is found and itsDisplayedproperty equalstrue - Returns
falseif the element does not exist or is not visible within the timeout period - Internally delegates to
GetElementfromSearchContextExtensions
Prerequisites and Configuration
Before implementing presence checks, you need two core components from the Ocaramba framework.
ElementLocator
The ElementLocator class in OcarambaLite/Types/ElementLocator.cs encapsulates Selenium locator strategies. It pairs a Locator enum value (Id, Xpath, CssSelector, etc.) with the search string.
using Ocaramba.Types;
// Define a locator for a login button
private readonly ElementLocator loginButtonLocator =
new ElementLocator(Locator.Id, "loginBtn");
BaseConfiguration Timeouts
The BaseConfiguration class in OcarambaLite/BaseConfiguration.cs provides centralized timeout values. IsElementPresent accepts a double representing seconds, typically sourced from:
BaseConfiguration.ShortTimeout(for quick checks)BaseConfiguration.MediumTimeout(standard waits)BaseConfiguration.LongTimeout(for slow-loading elements)
How to Check if an Element Is Present in Ocaramba
Define an Element Locator
First, declare your locators as readonly fields or properties to promote reusability and maintainability.
using Ocaramba.Types;
public class LoginPage
{
private readonly ElementLocator usernameField =
new ElementLocator(Locator.Id, "username");
private readonly ElementLocator submitButton =
new ElementLocator(Locator.CssSelector, "button[type='submit']");
}
Perform a Simple Presence Check
Use the extension method directly on your IWebDriver instance. Import the Ocaramba.Extensions namespace to access the method.
using Ocaramba;
using Ocaramba.Extensions;
using NUnit.Framework;
[Test]
public void VerifyLoginButtonIsPresent()
{
// Check presence using short timeout
bool isPresent = Driver.IsElementPresent(
loginButtonLocator,
BaseConfiguration.ShortTimeout);
Assert.IsTrue(isPresent, "Login button should be visible on the page.");
}
Check Dynamic Elements with Formatted Locators
For elements with dynamic attributes, use the Format method on ElementLocator to inject runtime values.
// Locator with placeholders
private readonly ElementLocator dynamicMenuItem =
new ElementLocator(
Locator.XPath,
"//a[@class='menu-item' and text()='{0}']");
[Test]
public void VerifyDynamicMenuItem()
{
// Format with specific text
ElementLocator newsLink = dynamicMenuItem.Format("News");
bool isVisible = Driver.IsElementPresent(
newsLink,
BaseConfiguration.MediumTimeout);
Assert.IsTrue(isVisible);
}
Integrate Presence Checks in Page Objects
Encapsulate presence logic within page object methods to keep tests readable and maintainable.
public class DashboardPage
{
private readonly IWebDriver driver;
private readonly ElementLocator welcomeMessage =
new ElementLocator(Locator.ClassName, "welcome-banner");
private readonly ElementLocator logoutButton =
new ElementLocator(Locator.Id, "logout");
public DashboardPage(IWebDriver driver) => this.driver = driver;
public bool IsWelcomeMessageDisplayed() =>
driver.IsElementPresent(
welcomeMessage,
BaseConfiguration.ShortTimeout);
public bool IsLogoutAvailable() =>
driver.IsElementPresent(
logoutButton,
BaseConfiguration.MediumTimeout);
}
How IsElementPresent Works Under the Hood
The implementation in WebDriverExtensions.cs relies on SearchContextExtensions.GetElement to handle the heavy lifting. The method executes the following logic:
- Invokes
GetElementwith the conditione => e.Displayedand the supplied timeout - Returns
trueimmediately whenGetElementsuccessfully locates a visible element - Catches exceptions — specifically
NoSuchElementExceptionandWebDriverTimeoutException— and returnsfalseinstead of propagating the error
This design pattern prevents test failures due to timing issues while providing a clean boolean interface for conditional test logic.
Summary
- Use
IsElementPresentfromOcaramba.Extensions.WebDriverExtensionsto verify element visibility without exception handling boilerplate. - Define locators using the
ElementLocatorclass with strategies from theLocatorenum (Id, Xpath, CssSelector). - Configure timeouts via
BaseConfiguration(ShortTimeout, MediumTimeout, LongTimeout) to control wait duration. - Handle dynamic values by formatting
ElementLocatorinstances at runtime with theFormatmethod. - Encapsulate checks in page object methods to maintain clean, readable test code.
Frequently Asked Questions
What exceptions does IsElementPresent handle internally?
The method catches NoSuchElementException and WebDriverTimeoutException thrown by the underlying GetElement call. When either exception occurs, the method returns false rather than propagating the error, allowing your test to continue execution and make conditional assertions based on element presence.
Can I use custom timeouts with IsElementPresent?
Yes. While the method accepts BaseConfiguration timeout values (ShortTimeout, MediumTimeout, LongTimeout), you can pass any double value representing seconds. For example, driver.IsElementPresent(locator, 5.0) waits exactly five seconds for the element to become present and visible.
How does IsElementPresent differ from standard Selenium FindElement?
Standard FindElement throws a NoSuchElementException immediately if the element doesn't exist, requiring explicit try-catch blocks or explicit waits. IsElementPresent encapsulates the wait logic and exception handling, returning a boolean result and polling the DOM until the element is both found and displayed (or the timeout expires).
Is IsElementPresent available for all driver types in Ocaramba?
Yes. Because it extends IWebDriver, the method works with any Selenium WebDriver implementation supported by Ocaramba, including ChromeDriver, FirefoxDriver, EdgeDriver, and remote WebDriver instances. The extension method is defined in the Ocaramba.Extensions namespace and requires the OcarambaLite or full Ocaramba package.
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 →