Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added WebDriverAware functionality #109

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,27 @@ public class SampleTest {
}
```

Need access to the WebDriver in your HtmlElement?
-------------------------------------------------
Use WebDriverAwareDecorator in your page objects

```java
PageFactory.initElements(new WebDriverAwareDecorator(new HtmlElementLocatorFactory(driver), driver), this);
```

Implement WebDriverAware on your HtmlElement

```
public class MyComponent extends HtmlElement implements WebDriverAware {
WebDriver driver = null;

@Override
public void setWebDriver(WebDriver driver) {
this.driver = driver;
}
}
```

Questions?
----------
In case you can't find an answer in documentation and examples provided above, you can ask it on StackOverflow with [![htmlelements](https://img.shields.io/badge/stackoverflow-htmlelements-orange.svg?style=flat)](http://stackoverflow.com/questions/tagged/htmlelements) tag.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ru.yandex.qatools.htmlelements.loader.decorator;

import org.openqa.selenium.WebDriver;

public interface WebDriverAware {
public void setWebDriver(WebDriver driver);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package ru.yandex.qatools.htmlelements.loader.decorator;

import java.lang.reflect.Field;
import java.util.List;

import org.openqa.selenium.WebDriver;

import ru.yandex.qatools.htmlelements.element.HtmlElement;
import ru.yandex.qatools.htmlelements.loader.decorator.HtmlElementDecorator;
import ru.yandex.qatools.htmlelements.pagefactory.CustomElementLocatorFactory;

public class WebDriverAwareDecorator extends HtmlElementDecorator {
private WebDriver driver;

public WebDriverAwareDecorator(CustomElementLocatorFactory factory, WebDriver driver) {
super(factory);

this.driver = driver;
}

@Override
protected <T extends HtmlElement> T decorateHtmlElement(ClassLoader loader, Field field) {
T element = super.decorateHtmlElement(loader, field);

if (element instanceof WebDriverAware) {
((WebDriverAware)element).setWebDriver(driver);
}

return element;
}

@Override
protected <T extends HtmlElement> List<T> decorateHtmlElementList(ClassLoader loader, Field field) {
List<T> elements = super.decorateHtmlElementList(loader, field);

if (!elements.isEmpty() && elements.get(0) instanceof WebDriverAware) {
for (T element : elements) {
((WebDriverAware)element).setWebDriver(driver);
}
}

return elements;
}
}