diff --git a/page-factory-doc/src/main/asciidoc/web_elements.asciidoc b/page-factory-doc/src/main/asciidoc/web_elements.asciidoc index ea8d96f43..ff8469c73 100644 --- a/page-factory-doc/src/main/asciidoc/web_elements.asciidoc +++ b/page-factory-doc/src/main/asciidoc/web_elements.asciidoc @@ -30,5 +30,9 @@ public abstract class YourPage extends {pn} { ---- - +Также можно переопределить декоратор для всего проекта, для этого необходимо добавить следующий параметр в файл конфигурации application.properties +[source,] +---- +plugins.html.decorator.fqcn = ru.sbtqa.tag.pagefactory2example.html.loader.MyCustomHtmlElementDecorator +---- diff --git a/plugins/html-plugin/src/main/java/ru/sbtqa/tag/pagefactory/HTMLPage.java b/plugins/html-plugin/src/main/java/ru/sbtqa/tag/pagefactory/HTMLPage.java index eddc43467..d2b6ea39a 100644 --- a/plugins/html-plugin/src/main/java/ru/sbtqa/tag/pagefactory/HTMLPage.java +++ b/plugins/html-plugin/src/main/java/ru/sbtqa/tag/pagefactory/HTMLPage.java @@ -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; @@ -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 */ @@ -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(); diff --git a/plugins/html-plugin/src/main/java/ru/sbtqa/tag/pagefactory/html/loader/CustomHtmlElementLoader.java b/plugins/html-plugin/src/main/java/ru/sbtqa/tag/pagefactory/html/loader/CustomHtmlElementLoader.java index ce370b70d..0658b8712 100644 --- a/plugins/html-plugin/src/main/java/ru/sbtqa/tag/pagefactory/html/loader/CustomHtmlElementLoader.java +++ b/plugins/html-plugin/src/main/java/ru/sbtqa/tag/pagefactory/html/loader/CustomHtmlElementLoader.java @@ -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 { @@ -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); } } diff --git a/plugins/html-plugin/src/main/java/ru/sbtqa/tag/pagefactory/html/properties/HtmlConfiguration.java b/plugins/html-plugin/src/main/java/ru/sbtqa/tag/pagefactory/html/properties/HtmlConfiguration.java index 04486da80..4972f2527 100644 --- a/plugins/html-plugin/src/main/java/ru/sbtqa/tag/pagefactory/html/properties/HtmlConfiguration.java +++ b/plugins/html-plugin/src/main/java/ru/sbtqa/tag/pagefactory/html/properties/HtmlConfiguration.java @@ -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); } diff --git a/plugins/html-plugin/src/main/java/ru/sbtqa/tag/pagefactory/reflection/HtmlReflection.java b/plugins/html-plugin/src/main/java/ru/sbtqa/tag/pagefactory/reflection/HtmlReflection.java index 7dc1d6903..e28f4a4ca 100644 --- a/plugins/html-plugin/src/main/java/ru/sbtqa/tag/pagefactory/reflection/HtmlReflection.java +++ b/plugins/html-plugin/src/main/java/ru/sbtqa/tag/pagefactory/reflection/HtmlReflection.java @@ -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 @@ -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 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); + } + } } diff --git a/plugins/html-plugin/src/main/java/ru/sbtqa/tag/pagefactory/utils/HtmlElementUtils.java b/plugins/html-plugin/src/main/java/ru/sbtqa/tag/pagefactory/utils/HtmlElementUtils.java index 3c49d70f8..49c7d3198 100644 --- a/plugins/html-plugin/src/main/java/ru/sbtqa/tag/pagefactory/utils/HtmlElementUtils.java +++ b/plugins/html-plugin/src/main/java/ru/sbtqa/tag/pagefactory/utils/HtmlElementUtils.java @@ -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 { @@ -50,7 +52,7 @@ public static WebElement getWebElement(T element) { */ public static T createElementWithCustomType(Class 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; }