ru.stqa.selenium.wait.ExpectedConditions Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2013 Alexei Barantsev
*
* 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.
*
*/
package ru.stqa.selenium.wait;
import com.google.common.base.Function;
import org.openqa.selenium.By;
import org.openqa.selenium.SearchContext;
import org.openqa.selenium.WebElement;
import java.util.List;
public class ExpectedConditions {
private ExpectedConditions() {
// Utility class
}
public static ExpectedCondition element(
final WebElement element, final Function condition) {
return new ExpectedCondition() {
@Override
public WebElement apply(SearchContext context) {
if (element != null && condition.apply(element)) {
return element;
} else {
return null;
}
}
};
}
public static ExpectedCondition firstElementLocated(
final By locator, final Function condition) {
return new ExpectedCondition() {
@Override
public WebElement apply(SearchContext context) {
WebElement element = findElement(context, locator);
if (element != null) {
return condition.apply(element) ? element : null;
} else {
return null;
}
}
};
}
public static ExpectedCondition someElementLocated(
final By locator, final Function condition) {
return new ExpectedCondition() {
@Override
public WebElement apply(SearchContext context) {
List elements = context.findElements(locator);
for (WebElement element : elements) {
if (element != null && condition.apply(element)) {
return element;
}
}
return null;
}
};
}
public static ExpectedCondition> eachElementLocated(
final By locator, final Function condition) {
return new ExpectedCondition>() {
@Override
public List apply(SearchContext context) {
List elements = context.findElements(locator);
for (WebElement element : elements) {
if (! condition.apply(element)) {
return null;
}
}
return elements;
}
};
}
public static ExpectedCondition> listOfElementsLocated(
final By locator, final Function, Boolean> condition) {
return new ExpectedCondition>() {
@Override
public List apply(SearchContext context) {
List elements = context.findElements(locator);
return condition.apply(elements) ? elements : null;
}
};
}
private static WebElement findElement(SearchContext context, By locator) {
List elements = context.findElements(locator);
return elements.size() > 0 ? elements.get(0) : null;
}
}