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

update default method to check if element displayed to always return … #83

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
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,12 @@ public WebElement findElement(By by) {
*/
@Override
public boolean isDisplayed() {
return wrappedElement.isDisplayed();
try{
return getWrappedElement().isDisplayed();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CRITICAL Either log or rethrow this exception. rule

} catch (Exception e) {
//ignore and return false to indicate element is not visible.
}
return false;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CRITICAL Either log or rethrow this exception. rule

//ignore and return false to indicate element is not visible.
}
return false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you're presuming that if element isn't found (this is when exception is thrown), then it's invisible. But this is hiding that fact (that element wasn't found) from the developer who runs the tests and therefore I think it's not the right way to handle this.

Instead you should be catching that exception in place where you call isDisplayed method.

}


/**
* Is the element currently enabled or not? This will generally return true for everything but
* disabled input elements.
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}

}