-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Component Manager and Events annotation support.
- Loading branch information
Showing
67 changed files
with
2,436 additions
and
298 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
fluentlenium-core/src/main/java/org/fluentlenium/core/components/ComponentAccessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,20 @@ | |||
package org.fluentlenium.core.components; | |||
|
|||
import org.openqa.selenium.WebElement; | |||
|
|||
import java.util.ArrayList; | |||
import java.util.Collection; | |||
import java.util.List; | |||
import java.util.Map; | |||
|
|||
public interface ComponentAccessor { | |||
/** | |||
* Get the related component from the given element. | |||
* | |||
* @param element | |||
* | |||
* @return | |||
*/ | |||
Object getComponent(WebElement element); | |||
|
|||
} |
17 changes: 17 additions & 0 deletions
17
fluentlenium-core/src/main/java/org/fluentlenium/core/components/ComponentBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,17 @@ | |||
package org.fluentlenium.core.components; | |||
|
|||
import lombok.AllArgsConstructor; | |||
import lombok.Getter; | |||
import lombok.Setter; | |||
import org.openqa.selenium.WebElement; | |||
|
|||
/** | |||
* Encapsulate a component and it's WebElement. | |||
*/ | |||
@Getter | |||
@Setter | |||
@AllArgsConstructor | |||
public class ComponentBean { | |||
public Object component; | |||
public WebElement element; | |||
} |
20 changes: 20 additions & 0 deletions
20
fluentlenium-core/src/main/java/org/fluentlenium/core/components/ComponentException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,20 @@ | |||
package org.fluentlenium.core.components; | |||
|
|||
public class ComponentException extends RuntimeException | |||
{ | |||
public ComponentException() { | |||
super(); | |||
} | |||
|
|||
public ComponentException(String message) { | |||
super(message); | |||
} | |||
|
|||
public ComponentException(String message, Throwable cause) { | |||
super(message, cause); | |||
} | |||
|
|||
public ComponentException(Throwable cause) { | |||
super(cause); | |||
} | |||
} |
23 changes: 23 additions & 0 deletions
23
fluentlenium-core/src/main/java/org/fluentlenium/core/components/ComponentInstantiator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,23 @@ | |||
package org.fluentlenium.core.components; | |||
|
|||
import org.openqa.selenium.WebElement; | |||
|
|||
public interface ComponentInstantiator { | |||
/** | |||
* Create and register a new component from the given element. | |||
* | |||
* @param componentClass type of the component | |||
* @param element wrapped element | |||
* @param <T> type of the component | |||
* | |||
* @return new instance of the component. | |||
*/ | |||
<T> T newComponent(Class<T> componentClass, WebElement element); | |||
|
|||
/** | |||
* Check if this class is a component class. | |||
* | |||
* @param componentClass | |||
*/ | |||
public boolean isComponentClass(Class<?> componentClass); | |||
} |
69 changes: 69 additions & 0 deletions
69
fluentlenium-core/src/main/java/org/fluentlenium/core/components/Components.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,69 @@ | |||
package org.fluentlenium.core.components; | |||
|
|||
import lombok.experimental.Delegate; | |||
import org.openqa.selenium.WebDriver; | |||
|
|||
import java.util.IdentityHashMap; | |||
import java.util.Map; | |||
|
|||
/** | |||
* Manage instances of {@link ComponentsManager}. | |||
*/ | |||
public enum Components { | |||
INSTANCE; | |||
|
|||
@Delegate | |||
private final Impl impl = new Impl(); | |||
|
|||
static class Impl { | |||
private Map<WebDriver, ComponentsManager> driverManagers = new IdentityHashMap<>(); | |||
|
|||
/** | |||
* Create a {@link ComponentsManager} for given {@link WebDriver} | |||
* @param driver | |||
* @return a new registered instance of {@link ComponentsManager} | |||
* @throws Components if a {@link ComponentsManager} is already registered. | |||
*/ | |||
public synchronized ComponentsManager create(WebDriver driver) { | |||
if (driverManagers.containsKey(driver)) throw new ComponentException("This WebDriver already have a Component Manager registered."); | |||
return createImpl(driver); | |||
} | |||
|
|||
private ComponentsManager createImpl(WebDriver driver) { | |||
ComponentsManager driverManager = new ComponentsManager(driver); | |||
driverManagers.put(driver, driverManager); | |||
return driverManager; | |||
} | |||
|
|||
public synchronized ComponentsManager getOrCreate(WebDriver driver) { | |||
ComponentsManager componentsManager = get(driver); | |||
if (componentsManager == null) { | |||
componentsManager = createImpl(driver); | |||
} | |||
return componentsManager; | |||
} | |||
|
|||
/** | |||
* Get the existing {@link ComponentsManager} for given {@link WebDriver} | |||
* @param driver | |||
* @return existing registered instance of {@link ComponentsManager}, or null if not found. | |||
*/ | |||
public synchronized ComponentsManager get(WebDriver driver) { | |||
return driverManagers.get(driver); | |||
} | |||
|
|||
/** | |||
* Release the existing {@link ComponentsManager} for given {@link WebDriver} | |||
* @param driver | |||
*/ | |||
public synchronized void release(WebDriver driver) { | |||
ComponentsManager remove = driverManagers.remove(driver); | |||
if (remove != null) { | |||
remove.release(); | |||
} | |||
} | |||
} | |||
|
|||
|
|||
|
|||
} |
97 changes: 97 additions & 0 deletions
97
fluentlenium-core/src/main/java/org/fluentlenium/core/components/ComponentsManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,97 @@ | |||
package org.fluentlenium.core.components; | |||
|
|||
import com.sun.jna.WeakIdentityHashMap; | |||
import org.fluentlenium.utils.ReflectionUtils; | |||
import org.openqa.selenium.WebDriver; | |||
import org.openqa.selenium.WebElement; | |||
import org.openqa.selenium.internal.WrapsElement; | |||
|
|||
import java.util.ArrayList; | |||
import java.util.Collection; | |||
import java.util.List; | |||
import java.util.Map; | |||
|
|||
/** | |||
* Manage living components for a WebDriver instance. | |||
* | |||
* A component is an Object implementing no particular interface, but capable a wrapping | |||
* a {@link org.openqa.selenium.WebElement}. | |||
* | |||
* {@link org.fluentlenium.core.domain.FluentWebElement} is the most common component. | |||
*/ | |||
public class ComponentsManager implements ComponentInstantiator, ComponentAccessor { | |||
|
|||
private final WebDriver driver; | |||
private final ComponentInstantiator instantiator; | |||
|
|||
//TODO: IdentityHashMap or WeakIdentityHashMap ? | |||
private Map<WebElement, Object> components = new WeakIdentityHashMap(); | |||
|
|||
public ComponentsManager(WebDriver driver) { | |||
this.driver = driver; | |||
this.instantiator = new DefaultComponentInstantiator(this.driver, this); | |||
} | |||
|
|||
public ComponentsManager(WebDriver driver, ComponentInstantiator instantiator) { | |||
this.driver = driver; | |||
this.instantiator = instantiator; | |||
} | |||
|
|||
public ComponentInstantiator getInstantiator() { | |||
return instantiator; | |||
} | |||
|
|||
/** | |||
* Get the related component from the given element. | |||
* | |||
* @param element | |||
* | |||
* @return | |||
*/ | |||
public Object getComponent(WebElement element) { | |||
return components.get(unwrapElement(element)); | |||
} | |||
|
|||
/** | |||
* Get all the component related to this webDriver. | |||
* @return | |||
*/ | |||
public Collection<ComponentBean> getAllComponents() { | |||
List<ComponentBean> allComponents = new ArrayList<>(); | |||
for (Map.Entry<WebElement, Object> entry : components.entrySet()) { | |||
allComponents.add(new ComponentBean(entry.getValue(), entry.getKey())); | |||
} | |||
return allComponents; | |||
} | |||
|
|||
@Override | |||
public boolean isComponentClass(Class<?> componentClass) { | |||
return instantiator.isComponentClass(componentClass); | |||
} | |||
|
|||
@Override | |||
public <T> T newComponent(Class<T> componentClass, WebElement element) { | |||
try { | |||
T component = instantiator.newComponent(componentClass, element); | |||
components.put(unwrapElement(element), component); | |||
return component; | |||
} catch (Exception e) { | |||
throw new ComponentException(componentClass.getName() | |||
+ " is not a valid component class. No valid constructor found (WebElement) or (WebElement, WebDriver)", e); | |||
} | |||
} | |||
|
|||
private WebElement unwrapElement(WebElement element) { | |||
if (element instanceof WrapsElement) { | |||
return unwrapElement(((WrapsElement)element).getWrappedElement()); | |||
} | |||
return element; | |||
} | |||
|
|||
/** | |||
* Release this manager. | |||
*/ | |||
public void release() { | |||
components.clear(); | |||
} | |||
} |
Oops, something went wrong.