blob: 31ed8e98c3a89fa9f4d26f3a0f63dc316209b34f [file] [log] [blame]
// <copyright file="ExpectedConditions.cs" company="WebDriver Committers">
// Copyright 2007-2011 WebDriver committers
// Copyright 2007-2011 Google Inc.
// Portions copyright 2011 Software Freedom Conservancy
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
using System;
using System.Collections.Generic;
using System.Text;
namespace OpenQA.Selenium.Support.UI
{
/// <summary>
/// Supplies a set of common conditions that can be waited for using <see cref="WebDriverWait"/>.
/// </summary>
/// <example>
/// <code>
/// IWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(3))
/// IWebElement element = wait.Until(ExpectedConditions.ElementExists(By.Id("foo")));
/// </code>
/// </example>
public sealed class ExpectedConditions
{
/// <summary>
/// Prevents a default instance of the <see cref="ExpectedConditions"/> class from being created.
/// </summary>
private ExpectedConditions()
{
}
/// <summary>
/// An expectation for checking the title of a page.
/// </summary>
/// <param name="title">The expected title, which must be an exact match.</param>
/// <returns><see langword="true"/> when the title matches; otherwise, <see langword="false"/>.</returns>
public static Func<IWebDriver, bool> TitleIs(string title)
{
return (driver) => { return title == driver.Title; };
}
/// <summary>
/// An expectation for checking that the title of a page contains a case-sensitive substring.
/// </summary>
/// <param name="title">The fragment of title expected.</param>
/// <returns><see langword="true"/> when the title matches; otherwise, <see langword="false"/>.</returns>
public static Func<IWebDriver, bool> TitleContains(string title)
{
return (driver) => { return driver.Title.Contains(title); };
}
/// <summary>
/// An expectation for checking that an element is present on the DOM of a
/// page. This does not necessarily mean that the element is visible.
/// </summary>
/// <param name="locator">The locator used to find the element.</param>
/// <returns>The <see cref="IWebElement"/> once it is located.</returns>
public static Func<IWebDriver, IWebElement> ElementExists(By locator)
{
return (driver) => { return driver.FindElement(locator); };
}
/// <summary>
/// An expectation for checking that an element is present on the DOM of a page
/// and visible. Visibility means that the element is not only displayed but
/// also has a height and width that is greater than 0.
/// </summary>
/// <param name="locator">The locator used to find the element.</param>
/// <returns>The <see cref="IWebElement"/> once it is located and visible.</returns>
public static Func<IWebDriver, IWebElement> ElementIsVisible(By locator)
{
return (driver) =>
{
try
{
return ElementIfVisible(driver.FindElement(locator));
}
catch (StaleElementReferenceException)
{
return null;
}
};
}
private static IWebElement ElementIfVisible(IWebElement element)
{
if (element.Displayed)
{
return element;
}
else
{
return null;
}
}
}
}