From bf3f275ab0ce0a677b860535fab06ec08f75d5d3 Mon Sep 17 00:00:00 2001 From: Graham Russell Date: Mon, 13 Jun 2016 22:28:54 +0100 Subject: [PATCH] Fixes #94 (#99) --- .../decorator/HtmlElementDecorator.java | 41 ++++++++++--------- .../htmlelements/NonElementFieldsTest.java | 29 +++++++++++++ .../testpages/NonElementFieldsPage.java | 34 +++++++++++++++ 3 files changed, 85 insertions(+), 19 deletions(-) create mode 100644 htmlelements-java/src/test/java/ru/yandex/qatools/htmlelements/NonElementFieldsTest.java create mode 100644 htmlelements-java/src/test/java/ru/yandex/qatools/htmlelements/testpages/NonElementFieldsPage.java diff --git a/htmlelements-java/src/main/java/ru/yandex/qatools/htmlelements/loader/decorator/HtmlElementDecorator.java b/htmlelements-java/src/main/java/ru/yandex/qatools/htmlelements/loader/decorator/HtmlElementDecorator.java index 3d44bbc5..f51216bf 100644 --- a/htmlelements-java/src/main/java/ru/yandex/qatools/htmlelements/loader/decorator/HtmlElementDecorator.java +++ b/htmlelements-java/src/main/java/ru/yandex/qatools/htmlelements/loader/decorator/HtmlElementDecorator.java @@ -50,26 +50,29 @@ public HtmlElementDecorator(CustomElementLocatorFactory factory) { } public Object decorate(ClassLoader loader, Field field) { - if (isTypifiedElement(field)) { - return decorateTypifiedElement(loader, field); + try { + if (isTypifiedElement(field)) { + return decorateTypifiedElement(loader, field); + } + if (isHtmlElement(field)) { + return decorateHtmlElement(loader, field); + } + if (isWebElement(field) && !field.getName().equals("wrappedElement")) { + return decorateWebElement(loader, field); + } + if (isTypifiedElementList(field)) { + return decorateTypifiedElementList(loader, field); + } + if (isHtmlElementList(field)) { + return decorateHtmlElementList(loader, field); + } + if (isWebElementList(field)) { + return decorateWebElementList(loader, field); + } + return null; + } catch (ClassCastException ignore) { + return null; // See bug #94 and NonElementFieldsTest } - if (isHtmlElement(field)) { - return decorateHtmlElement(loader, field); - } - if (isWebElement(field) && !field.getName().equals("wrappedElement")) { - return decorateWebElement(loader, field); - } - if (isTypifiedElementList(field)) { - return decorateTypifiedElementList(loader, field); - } - if (isHtmlElementList(field)) { - return decorateHtmlElementList(loader, field); - } - if (isWebElementList(field)) { - return decorateWebElementList(loader, field); - } - - return null; } protected T decorateTypifiedElement(ClassLoader loader, Field field) { diff --git a/htmlelements-java/src/test/java/ru/yandex/qatools/htmlelements/NonElementFieldsTest.java b/htmlelements-java/src/test/java/ru/yandex/qatools/htmlelements/NonElementFieldsTest.java new file mode 100644 index 00000000..bec36d0c --- /dev/null +++ b/htmlelements-java/src/test/java/ru/yandex/qatools/htmlelements/NonElementFieldsTest.java @@ -0,0 +1,29 @@ +package ru.yandex.qatools.htmlelements; + +import org.junit.Test; +import ru.yandex.qatools.htmlelements.testpages.NonElementFieldsPage; + +import static org.hamcrest.CoreMatchers.nullValue; +import static org.hamcrest.Matchers.emptyCollectionOf; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +/** + * @author Graham Russell graham@ham1.co.uk + * Date: 24.10.2015 + */ +public class NonElementFieldsTest { + + @Test + public void nonElementPrivateFieldsShouldNotBeDecoratedAsElements() { + + NonElementFieldsPage page = new NonElementFieldsPage(); + + assertThat("Non-WebElement/HtmlElement fields are not touched", + page.rowsCache, is(nullValue())); + assertThat("Non-WebElement/HtmlElement fields are not touched", + page.rowsAsStringCache, is(nullValue())); + assertThat("Non-WebElement/HtmlElement fields are not touched", + page.otherCache, is(emptyCollectionOf(String.class))); + } +} diff --git a/htmlelements-java/src/test/java/ru/yandex/qatools/htmlelements/testpages/NonElementFieldsPage.java b/htmlelements-java/src/test/java/ru/yandex/qatools/htmlelements/testpages/NonElementFieldsPage.java new file mode 100644 index 00000000..ee7e1f24 --- /dev/null +++ b/htmlelements-java/src/test/java/ru/yandex/qatools/htmlelements/testpages/NonElementFieldsPage.java @@ -0,0 +1,34 @@ +package ru.yandex.qatools.htmlelements.testpages; + +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import ru.yandex.qatools.htmlelements.loader.HtmlElementLoader; + +import java.util.ArrayList; +import java.util.List; + +import static org.mockito.Mockito.mock; + +/** + * @author Graham Russell graham@ham1.co.uk + * Date: 24.10.2015 + */ +public class NonElementFieldsPage { + + // public for ease of assertion in test + public List> rowsCache; + public List> rowsAsStringCache; + public List otherCache = new ArrayList<>(); + + public NonElementFieldsPage() { + this(mockDriver()); + } + + public NonElementFieldsPage(WebDriver driver) { + HtmlElementLoader.populatePageObject(this, driver); + } + + public static WebDriver mockDriver() { + return mock(WebDriver.class); + } +}