Skip to content
This repository has been archived by the owner on Sep 3, 2024. It is now read-only.

Commit

Permalink
Merge pull request #391 from sbtqa/custom-decorator
Browse files Browse the repository at this point in the history
add ability to specify decorator in properties
  • Loading branch information
kosteman authored May 5, 2023
2 parents e2d0675 + 159b0c8 commit 671048b
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 13 deletions.
6 changes: 5 additions & 1 deletion page-factory-doc/src/main/asciidoc/web_elements.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,9 @@ public abstract class YourPage extends {pn} {
----
Также можно переопределить декоратор для всего проекта, для этого необходимо добавить следующий параметр в файл конфигурации application.properties
[source,]
----
plugins.html.decorator.fqcn = ru.sbtqa.tag.pagefactory2example.html.loader.MyCustomHtmlElementDecorator
----
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package ru.sbtqa.tag.pagefactory;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.junit.Assert;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebElement;
Expand All @@ -13,13 +11,15 @@
import ru.sbtqa.tag.pagefactory.find.Find;
import ru.sbtqa.tag.pagefactory.find.HtmlFindUtils;
import ru.sbtqa.tag.pagefactory.html.actions.HtmlPageActions;
import ru.sbtqa.tag.pagefactory.html.loader.decorators.CustomHtmlElementDecorator;
import ru.sbtqa.tag.pagefactory.html.properties.HtmlConfiguration;
import ru.sbtqa.tag.pagefactory.reflection.HtmlReflection;
import ru.sbtqa.tag.pagefactory.reflection.Reflection;
import ru.sbtqa.tag.pagefactory.utils.PageUtils;
import ru.yandex.qatools.htmlelements.loader.decorator.HtmlElementLocatorFactory;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Inherit your html page objects from this class
*/
Expand All @@ -33,7 +33,7 @@ public abstract class HTMLPage extends WebPage {
private static final HtmlConfiguration PROPERTIES = HtmlConfiguration.create();

public HTMLPage() {
super(new CustomHtmlElementDecorator(new HtmlElementLocatorFactory(Environment.getDriverService().getDriver())));
super(HtmlReflection.getDecorator(new HtmlElementLocatorFactory(Environment.getDriverService().getDriver())));
applyEnvironment();
if (PROPERTIES.getVerifyPage()) {
PageUtils.verifyPageByDataTestId();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package ru.sbtqa.tag.pagefactory.html.loader;

import java.lang.reflect.InvocationTargetException;
import org.openqa.selenium.SearchContext;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.PageFactory;
import ru.sbtqa.tag.pagefactory.html.loader.decorators.CustomHtmlElementDecorator;
import org.openqa.selenium.support.pagefactory.FieldDecorator;
import ru.sbtqa.tag.pagefactory.reflection.HtmlReflection;
import ru.yandex.qatools.htmlelements.element.HtmlElement;
import ru.yandex.qatools.htmlelements.element.TypifiedElement;
import ru.yandex.qatools.htmlelements.exceptions.HtmlElementsException;
import ru.yandex.qatools.htmlelements.loader.HtmlElementLoader;
import ru.yandex.qatools.htmlelements.loader.decorator.HtmlElementLocatorFactory;
import ru.yandex.qatools.htmlelements.pagefactory.CustomElementLocatorFactory;

import java.lang.reflect.InvocationTargetException;

import static ru.yandex.qatools.htmlelements.utils.HtmlElementUtils.newInstance;

public class CustomHtmlElementLoader extends HtmlElementLoader {
Expand Down Expand Up @@ -45,7 +48,7 @@ public static void populatePageObject(Object page, SearchContext searchContext)


public static void populatePageObject(Object page, CustomElementLocatorFactory locatorFactory) {
PageFactory.initElements(new CustomHtmlElementDecorator(locatorFactory), page);
PageFactory.initElements((FieldDecorator) HtmlReflection.getDecorator(locatorFactory), page);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public interface HtmlConfiguration extends WebConfiguration {
@DefaultValue("false")
boolean getVerifyPage();

@Key("plugins.html.decorator.fqcn")
@DefaultValue("ru.sbtqa.tag.pagefactory.html.loader.decorators.CustomHtmlElementDecorator")
String getDecoratorFullyQualifiedClassName();

static HtmlConfiguration create() {
return Configuration.init(HtmlConfiguration.class);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
package ru.sbtqa.tag.pagefactory.reflection;

import static java.lang.String.format;
import ru.sbtqa.tag.pagefactory.environment.Environment;
import ru.sbtqa.tag.pagefactory.exception.ElementSearchError;
import ru.sbtqa.tag.pagefactory.find.HtmlFindUtils;
import ru.sbtqa.tag.pagefactory.html.loader.decorators.CustomHtmlElementDecorator;
import ru.sbtqa.tag.pagefactory.html.properties.HtmlConfiguration;
import ru.yandex.qatools.htmlelements.element.HtmlElement;
import ru.yandex.qatools.htmlelements.pagefactory.CustomElementLocatorFactory;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import static java.lang.String.format;

public class HtmlReflection extends DefaultReflection {

private static final HtmlConfiguration PROPERTIES = HtmlConfiguration.create();

/**
* Execute method with one or more parameters inside of the given block
* element
Expand All @@ -25,4 +34,23 @@ public void executeMethodByTitleInBlock(String blockPath, String actionTitle, Ob
throw new ElementSearchError(format("Block not found by path '%s'", blockPath), ex);
}
}

/**
* Get custom decorator instance
*
* @param factory custom element locator factory
*/
public static <T extends CustomHtmlElementDecorator> T getDecorator(CustomElementLocatorFactory factory) {
String fullyQualifiedClassName = PROPERTIES.getDecoratorFullyQualifiedClassName();
try {
Class<?> clazz = Class.forName(fullyQualifiedClassName);
Constructor<?> constructor = clazz.getConstructor(CustomElementLocatorFactory.class);
T instance = (T) constructor.newInstance(factory);
LOG.debug("Loaded decorator: {}", fullyQualifiedClassName);
return instance;
} catch (ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException |
InvocationTargetException e) {
throw new ElementSearchError(format("Decorator not found by path '%s'", fullyQualifiedClassName), e);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package ru.sbtqa.tag.pagefactory.utils;

import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.PageFactory;

import ru.sbtqa.tag.pagefactory.context.PageContext;
import ru.sbtqa.tag.pagefactory.environment.Environment;
import ru.sbtqa.tag.pagefactory.html.loader.CustomHtmlElementLoader;
import ru.sbtqa.tag.pagefactory.html.loader.decorators.CustomHtmlElementDecorator;
import ru.sbtqa.tag.pagefactory.reflection.HtmlReflection;
import ru.yandex.qatools.htmlelements.element.TypifiedElement;
import ru.yandex.qatools.htmlelements.loader.decorator.HtmlElementDecorator;
import ru.yandex.qatools.htmlelements.loader.decorator.HtmlElementLocatorFactory;

import java.util.List;

import static ru.yandex.qatools.htmlelements.utils.HtmlElementUtils.isTypifiedElement;

public class HtmlElementUtils {
Expand Down Expand Up @@ -50,7 +52,7 @@ public static <T extends WebElement> WebElement getWebElement(T element) {
*/
public static <T extends TypifiedElement> T createElementWithCustomType(Class<T> clazz, WebElement element) {
T customElement = CustomHtmlElementLoader.createTypifiedElement(clazz, element, "Custom");
CustomHtmlElementDecorator decorator = new CustomHtmlElementDecorator(new HtmlElementLocatorFactory(Environment.getDriverService().getDriver()));
HtmlElementDecorator decorator = HtmlReflection.getDecorator(new HtmlElementLocatorFactory(Environment.getDriverService().getDriver()));
PageFactory.initElements(decorator, PageContext.getCurrentPage());
return customElement;
}
Expand Down

0 comments on commit 671048b

Please sign in to comment.