Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

Commit

Permalink
Added GenericTextFieldMatcher and MatcherTest
Browse files Browse the repository at this point in the history
  • Loading branch information
nicopaul committed Dec 7, 2016
1 parent 16699b6 commit 402ffb9
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
package info.novatec.testit.webtester.support.assertj;

import info.novatec.testit.webtester.pageobjects.*;
import info.novatec.testit.webtester.pageobjects.Button;
import info.novatec.testit.webtester.pageobjects.Checkbox;
import info.novatec.testit.webtester.pageobjects.GenericTextField;
import info.novatec.testit.webtester.pageobjects.IFrame;
import info.novatec.testit.webtester.pageobjects.Image;
import info.novatec.testit.webtester.pageobjects.List;
import info.novatec.testit.webtester.pageobjects.NumberField;
import info.novatec.testit.webtester.pageobjects.PageObject;
import info.novatec.testit.webtester.pageobjects.RadioButton;
import info.novatec.testit.webtester.pageobjects.Table;
import info.novatec.testit.webtester.pageobjects.TableField;
import info.novatec.testit.webtester.pageobjects.TableRow;
import info.novatec.testit.webtester.pageobjects.TextArea;
import info.novatec.testit.webtester.pageobjects.TextField;

import org.assertj.core.api.Assertions;


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package info.novatec.testit.webtester.support.hamcrest;

import info.novatec.testit.webtester.pageobjects.GenericTextField;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Matcher;

/**
* Contains {@link Matcher matcher} for {@link GenericTextField generic text fields}.
*
* @since 1.2
*/
public class GenericTextFieldMatcher extends PageObjectMatcher {


/**
* returns whether the {@link GenericTextField generic text field} contains a certain text.
*
* @param text contains expected text
* @return true if text on generic text field contains expected text
* @since 1.2.
*/
public static Matcher<GenericTextField> hasText(final String text) {
return new BaseMatcher<GenericTextField>() {

@Override
public void describeTo(Description description) {
description.appendText("text to be '" + text + "'");
}

@Override
public void describeMismatch(Object genericTextField, Description mismatchDescription){
mismatchDescription.appendText("was '" + (( GenericTextField ) genericTextField).getText() + "'");
}

@Override
public boolean matches(Object genericTextField){
return ((GenericTextField) genericTextField).getText().equals(text);
}

};

}

protected GenericTextFieldMatcher() {
/* utility constructor */
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package info.novatec.testit.webtester.support.hamcrest;

import info.novatec.testit.webtester.pageobjects.GenericTextField;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import static info.novatec.testit.webtester.support.hamcrest.GenericTextFieldMatcher.hasText;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.not;
import static org.mockito.Mockito.doReturn;

@RunWith(MockitoJUnitRunner.class)
public class GenericTextFieldMatcherTest {

static final String TEXT = "foo";

static final String ANOTHER_TEXT = "bar";

@Mock
GenericTextField genericTextField;

@Before
public void setUp(){
doReturn(TEXT).when(genericTextField).getText();
}

@Test
public void hasTextTrueTest(){
assertThat(genericTextField, hasText(TEXT));
}

@Test (expected = AssertionError.class)
public void hasTextFalseTest(){
assertThat(genericTextField, hasText(ANOTHER_TEXT));
}

@Test
public void notHasTextTrueTest(){
assertThat(genericTextField, not(hasText(ANOTHER_TEXT)));
}

@Test (expected = AssertionError.class)
public void notHasTextFalseTest(){
assertThat(genericTextField, not(hasText(TEXT)));
}
}

0 comments on commit 402ffb9

Please sign in to comment.