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

add TypifiedElement.exists and HtmlElement.exists #91

Merged
merged 1 commit into from
Jan 20, 2016
Merged
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 @@ -2,6 +2,7 @@

import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.internal.WrapsElement;
Expand Down Expand Up @@ -83,6 +84,21 @@ public void setName(String name) {
this.name = name;
}

/**
* Determines whether or not this element exists on page.
*
* @return True if the element exists on page, false otherwise.
*/
@SuppressWarnings("squid:S1166") // Sonar "Exception handlers should preserve the original exception" rule
public boolean exists() {
try {
getWrappedElement().isDisplayed();
} catch (NoSuchElementException ignored) {
return false;
}
return true;
}

/**
* Clicks this element. See {@link org.openqa.selenium.WebElement#click()} for more details.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ru.yandex.qatools.htmlelements.element;

import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.internal.WrapsElement;

Expand Down Expand Up @@ -84,6 +85,21 @@ public String toString() {
return name;
}

/**
* Determines whether or not this element exists on page.
*
* @return True if the element exists on page, false otherwise.
*/
@SuppressWarnings("squid:S1166") // Sonar "Exception handlers should preserve the original exception" rule
public boolean exists() {
try {
getWrappedElement().isDisplayed();
} catch (NoSuchElementException ignored) {
return false;
}
return true;
}

/**
* Is this element displayed or not? This method avoids the problem of having to parse an
* element's "style" attribute.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package ru.yandex.qatools.htmlelements;

import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebElement;
import ru.yandex.qatools.htmlelements.element.HtmlElement;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
* @author Alexander Kedrik [email protected]
* Date: 07.09.15
*/
public class HtmlElementExistsMethodTest {
private final WebElement webElement = mock(WebElement.class);
private final HtmlElement htmlElement = new HtmlElement();

@Before
public void setUp() {
htmlElement.setWrappedElement(webElement);
}

@Test
public void existsShouldReturnTrueIfIsDisplayedReturnsTrue() {
when(webElement.isDisplayed()).thenReturn(true);
assertThat(htmlElement.exists(), is(true));
}

@Test
public void existsShouldReturnTrueIfIsDisplayedReturnsFalse() {
when(webElement.isDisplayed()).thenReturn(false);
assertThat(htmlElement.exists(), is(true));
}

@Test
public void existsShouldReturnFalseIfIsDisplayedThrowsNoSuchElementException() {
doThrow(new NoSuchElementException("")).when(webElement).isDisplayed();
assertThat(htmlElement.exists(), is(false));
}

@Test(expected = TimeoutException.class)
public void existsShouldCatchOnlyNoSuchElementException() {
doThrow(new TimeoutException("")).when(webElement).isDisplayed();
htmlElement.exists();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package ru.yandex.qatools.htmlelements;

import org.junit.Test;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebElement;
import ru.yandex.qatools.htmlelements.element.TypifiedElement;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
* @author Alexander Kedrik [email protected]
* Date: 07.09.15
*/
public class TypifiedElementExistsMethodTest {
private final WebElement webElement = mock(WebElement.class);
private final TypifiedElement typifiedElement = new TypifiedElement(webElement) {};

@Test
public void existsShouldReturnTrueIfIsDisplayedReturnsTrue() {
when(webElement.isDisplayed()).thenReturn(true);
assertThat(typifiedElement.exists(), is(true));
}

@Test
public void existsShouldReturnTrueIfIsDisplayedReturnsFalse() {
when(webElement.isDisplayed()).thenReturn(false);
assertThat(typifiedElement.exists(), is(true));
}

@Test
public void existsShouldReturnFalseIfIsDisplayedThrowsNoSuchElementException() {
doThrow(new NoSuchElementException("")).when(webElement).isDisplayed();
assertThat(typifiedElement.exists(), is(false));
}

@Test(expected = TimeoutException.class)
public void existsShouldCatchOnlyNoSuchElementException() {
doThrow(new TimeoutException("")).when(webElement).isDisplayed();
typifiedElement.exists();
}
}