com.redhat.darcy.webdriver.internal.DefaultWebDriverElementContext Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of darcy-webdriver Show documentation
Show all versions of darcy-webdriver Show documentation
An implementation of darcy and darcy-web that uses Selenium WebDriver as the automation library backend.
The newest version!
/*
Copyright 2014 Red Hat, Inc. and/or its affiliates.
This file is part of darcy-webdriver.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
*/
package com.redhat.darcy.webdriver.internal;
import com.redhat.darcy.ui.DarcyException;
import com.redhat.darcy.ui.api.Locator;
import com.redhat.darcy.ui.api.elements.Element;
import com.redhat.darcy.util.LazyList;
import com.redhat.darcy.webdriver.ElementConstructorMap;
import com.redhat.darcy.webdriver.WebDriverElementContext;
import com.redhat.darcy.webdriver.elements.WebDriverElement;
import com.redhat.darcy.webdriver.locators.ByAttribute;
import com.redhat.darcy.webdriver.locators.ByChained;
import com.redhat.darcy.webdriver.locators.ByPartialVisibleText;
import com.redhat.darcy.webdriver.locators.ByVisibleText;
import org.openqa.selenium.By;
import org.openqa.selenium.SearchContext;
import java.util.List;
import java.util.stream.Collectors;
public class DefaultWebDriverElementContext implements WebDriverElementContext {
private final ElementConstructorMap elementMap;
private final SearchContext sc;
public DefaultWebDriverElementContext(SearchContext context, ElementConstructorMap elementMap) {
this.elementMap = elementMap;
this.sc = context;
}
@Override
public List findAllById(Class type, String id) {
return newElementList(type, new WebElementListLookup(By.id(id), sc));
}
@Override
public T findById(Class type, String id) {
return newElement(type, new WebElementLookup(By.id(id), sc));
}
@Override
public List findAllByName(Class type, String name) {
return newElementList(type, new WebElementListLookup(By.name(name), sc));
}
@Override
public T findByName(Class type, String name) {
return newElement(type, new WebElementLookup(By.name(name), sc));
}
@Override
public List findAllByLinkText(Class type, String linkText) {
return newElementList(type, new WebElementListLookup(By.linkText(linkText), sc));
}
@Override
public T findByLinkText(Class type, String linkText) {
return newElement(type, new WebElementLookup(By.linkText(linkText), sc));
}
@Override
public List findAllByTextContent(Class type, String textContent) {
return newElementList(type, new WebElementListLookup(new ByVisibleText(textContent), sc));
}
@Override
public T findByTextContent(Class type, String textContent) {
return newElement(type, new WebElementLookup(new ByVisibleText(textContent), sc));
}
@Override
public List findAllByPartialTextContent(Class type, String partialTextContent) {
By by = new ByPartialVisibleText(partialTextContent);
ElementListLookup listLookup = new WebElementListLookup(by, sc);
return newElementList(type, listLookup);
}
@Override
public T findByPartialTextContent(Class type, String partialTextContent) {
By by = new ByPartialVisibleText(partialTextContent);
return newElement(type, new WebElementLookup(by, sc));
}
@Override
public List findAllByXPath(Class type, String xpath) {
return newElementList(type, new WebElementListLookup(By.xpath(xpath), sc));
}
@Override
public T findByXPath(Class type, String xpath) {
return newElement(type, new WebElementLookup(By.xpath(xpath), sc));
}
@Override
public List findAllByCss(Class type, String css) {
return newElementList(type, new WebElementListLookup(By.cssSelector(css), sc));
}
@Override
public T findByCss(Class type, String css) {
return newElement(type, new WebElementLookup(By.cssSelector(css), sc));
}
@Override
public List findAllByHtmlTag(Class type, String tag) {
return newElementList(type, new WebElementListLookup(By.tagName(tag), sc));
}
@Override
public T findByHtmlTag(Class type, String tag) {
return newElement(type, new WebElementLookup(By.tagName(tag), sc));
}
@Override
public List findAllByClassName(Class type, String className) {
return newElementList(type, new WebElementListLookup(By.className(className), sc));
}
@Override
public T findByClassName(Class type, String className) {
return newElement(type, new WebElementLookup(By.className(className), sc));
}
@Override
public List findAllByChained(Class type, Locator... locators) {
return newElementList(type, new WebElementListLookup(new ByChained(locators), sc));
}
@Override
public T findByChained(Class type, Locator... locators) {
return newElement(type, new WebElementLookup(new ByChained(locators), sc));
}
@Override
public List findAllByNested(Class type, Element parent, Locator child) {
if (!(parent instanceof WebDriverElement)) {
throw new DarcyException("Parent element is not a WebDriverElement. Can only find " +
"by nested for fundamental UI element types found by darcy-webdriver.");
}
return newElementList(type, new NestedElementListLookup((WebDriverElement) parent, child));
}
@Override
public T findByNested(Class type, Element parent, Locator child) {
if (!(parent instanceof WebDriverElement)) {
throw new DarcyException("Parent element is not a WebDriverElement. Can only find " +
"by nested for fundamental UI element types found by darcy-webdriver.");
}
return newElement(type, new NestedElementLookup((WebDriverElement) parent, child));
}
@Override
public List findAllByAttribute(Class type, String attribute, String value) {
return newElementList(type, new WebElementListLookup(new ByAttribute(attribute, value), sc));
}
@Override
public T findByAttribute(Class type, String attribute, String value) {
return newElement(type, new WebElementLookup(new ByAttribute(attribute, value), sc));
}
@SuppressWarnings("unchecked")
private T newElement(Class type, ElementLookup source) {
if (!Element.class.isAssignableFrom(type)) {
throw new DarcyException("An ElementContext can only locate Element types: "
+ type.toString());
}
return (T) elementMap.get((Class) type).newElement(source, this);
}
@SuppressWarnings("unchecked")
private List newElementList(Class type, ElementListLookup source) {
if (!Element.class.isAssignableFrom(type)) {
throw new DarcyException("An ElementContext can only locate Element types: "
+ type.toString());
}
return new LazyList<>(() -> source.lookup()
.stream()
.map(e -> newElement(type, new ListElementLookup(source, e)))
.collect(Collectors.toList()));
}
}