From bc5816c204803e061d5599ff7adb678c7f11d014 Mon Sep 17 00:00:00 2001 From: Michael Suzuki Date: Sat, 23 May 2015 22:18:24 +0100 Subject: [PATCH 1/2] update default method to check if element displayed to always return a boolean and handle exceptions --- .../qatools/htmlelements/element/TypifiedElement.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/htmlelements-java/src/main/java/ru/yandex/qatools/htmlelements/element/TypifiedElement.java b/htmlelements-java/src/main/java/ru/yandex/qatools/htmlelements/element/TypifiedElement.java index 855ee281..eb9d1ec7 100644 --- a/htmlelements-java/src/main/java/ru/yandex/qatools/htmlelements/element/TypifiedElement.java +++ b/htmlelements-java/src/main/java/ru/yandex/qatools/htmlelements/element/TypifiedElement.java @@ -91,9 +91,15 @@ public String toString() { * @return Whether or not the element is displayed */ public boolean isDisplayed() { - return getWrappedElement().isDisplayed(); + try{ + return getWrappedElement().isDisplayed(); + } catch (Exception e) { + //ignore and return false to indicate element is not visible. + } + return false; } + /** * Is the element currently enabled or not? This will generally return true for everything but * disabled input elements. From d485782b063ac1f77499b2583c0f82e9bcfcf518 Mon Sep 17 00:00:00 2001 From: Michael Suzuki Date: Tue, 26 May 2015 11:59:05 +0100 Subject: [PATCH 2/2] #83,Updated default method to check if element displayed to always return and added unit test --- .../htmlelements/element/HtmlElement.java | 7 ++- .../htmlelements/ElementDisplayedTest.java | 58 +++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 htmlelements-java/src/test/java/ru/yandex/qatools/htmlelements/ElementDisplayedTest.java diff --git a/htmlelements-java/src/main/java/ru/yandex/qatools/htmlelements/element/HtmlElement.java b/htmlelements-java/src/main/java/ru/yandex/qatools/htmlelements/element/HtmlElement.java index 4cedfbaa..d0fa216d 100644 --- a/htmlelements-java/src/main/java/ru/yandex/qatools/htmlelements/element/HtmlElement.java +++ b/htmlelements-java/src/main/java/ru/yandex/qatools/htmlelements/element/HtmlElement.java @@ -208,7 +208,12 @@ public WebElement findElement(By by) { */ @Override public boolean isDisplayed() { - return wrappedElement.isDisplayed(); + try{ + return getWrappedElement().isDisplayed(); + } catch (Exception e) { + //ignore and return false to indicate element is not visible. + } + return false; } /** diff --git a/htmlelements-java/src/test/java/ru/yandex/qatools/htmlelements/ElementDisplayedTest.java b/htmlelements-java/src/test/java/ru/yandex/qatools/htmlelements/ElementDisplayedTest.java new file mode 100644 index 00000000..9d413efe --- /dev/null +++ b/htmlelements-java/src/test/java/ru/yandex/qatools/htmlelements/ElementDisplayedTest.java @@ -0,0 +1,58 @@ +package ru.yandex.qatools.htmlelements; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExternalResource; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.phantomjs.PhantomJSDriver; + +import ru.yandex.qatools.htmlelements.element.HtmlElement; +import ru.yandex.qatools.htmlelements.element.TypifiedElement; +import ru.yandex.qatools.htmlelements.loader.HtmlElementLoader; +import ru.yandex.qatools.htmlelements.testelements.DummyBlock; + +/** + * Test that validates the {@link TypifiedElement} isDisplayed method always + * returns a boolean. + * @author Michael Suzuki + */ +public class ElementDisplayedTest { + + private WebDriver driver; + + @Rule + public ExternalResource manage = new ExternalResource() { + @Override + protected void before() throws Throwable { + driver = new PhantomJSDriver(); + } + + @Override + protected void after() { + driver.quit(); + } + }; + + @Test + public void shouldReturnFalseAndNotAnException() throws Exception { + Assert.assertFalse(HtmlElementLoader.create(DummyBlock.class, driver).isDisplayed()); + } + + @Test + public void shouldReturnTrueForVisibleElements() throws Exception { + WebElement element = mock(WebElement.class); + when(element.isDisplayed()).thenReturn(true); + HtmlElement elementVisble = new HtmlElement(); + elementVisble.setWrappedElement(element); + Assert.assertTrue(elementVisble.isDisplayed()); + + when(element.isDisplayed()).thenReturn(false); + Assert.assertFalse(elementVisble.isDisplayed()); + } + +}