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

Commit 181c964

Browse files
committed
Merge pull request #21 from slu-it/SendKeyError
Added method for pressing the ENTER key on text fields and a warning on problematic methods
2 parents bdda111 + 9518b5a commit 181c964

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

webtester-core/src/main/java/info/novatec/testit/webtester/pageobjects/TextField.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.Set;
44

55
import org.apache.commons.lang.StringUtils;
6+
import org.openqa.selenium.Keys;
67
import org.openqa.selenium.WebElement;
78
import org.slf4j.Logger;
89
import org.slf4j.LoggerFactory;
@@ -12,6 +13,7 @@
1213
import info.novatec.testit.webtester.api.callbacks.PageObjectCallback;
1314
import info.novatec.testit.webtester.api.exceptions.PageObjectIsDisabledException;
1415
import info.novatec.testit.webtester.api.exceptions.PageObjectIsInvisibleException;
16+
import info.novatec.testit.webtester.api.exceptions.StaleElementRecoveryException;
1517
import info.novatec.testit.webtester.api.pageobjects.traits.HasText;
1618
import info.novatec.testit.webtester.eventsystem.events.pageobject.TextAppendedEvent;
1719
import info.novatec.testit.webtester.eventsystem.events.pageobject.TextClearedEvent;
@@ -88,6 +90,11 @@ protected void executeAfterAction(TextField textField, String oldText, String ne
8890
/**
8991
* Sets the given text by replacing whatever text is currently set for the
9092
* {@link TextField text field}.
93+
* <p>
94+
* <b>Note:</b> is is not advised to try and send {@link Keys} via this method!
95+
* Doing so may in some cases lead to unintended side effects. I.e. sending ENTER
96+
* to a search field will cause a {@link StaleElementRecoveryException} if this action
97+
* navigates to another page.
9198
*
9299
* @param textToSet the text to set
93100
* @return the same instance for fluent API use
@@ -117,6 +124,11 @@ protected void executeAfterAction(TextField textField, String oldText, String ne
117124
/**
118125
* Appends the given text to whatever text is currently set for the
119126
* {@link TextField text field}.
127+
* <p>
128+
* <b>Note:</b> is is not advised to try and send {@link Keys} via this method!
129+
* Doing so may in some cases lead to unintended side effects. I.e. sending ENTER
130+
* to a search field will cause a {@link StaleElementRecoveryException} if this action
131+
* navigates to another page.
120132
*
121133
* @param textToAppend the text to append
122134
* @return the same instance for fluent API use
@@ -142,6 +154,26 @@ protected void executeAfterAction(TextField textField, String oldText, String ne
142154
return this;
143155
}
144156

157+
/**
158+
* Presses enter on this text field. This can be for example be used to send a form where the text
159+
* field is included.
160+
* <p>
161+
* This method does <u>not</u> return this instance for fluent API because pressing ENTER is usually done in
162+
* order to send a form or otherwise execute a potentially terminal action.
163+
*
164+
* @since 1.1.0
165+
*/
166+
public void pressEnter() {
167+
executeAction(new PageObjectCallback() {
168+
169+
@Override
170+
public void execute(PageObject pageObject) {
171+
pageObject.getWebElement().sendKeys(Keys.ENTER);
172+
}
173+
174+
});
175+
}
176+
145177
@Override
146178
protected boolean isCorrectClassForWebElement(WebElement webElement) {
147179

webtester-core/src/test/java/info/novatec/testit/webtester/pageobjects/TextFieldTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import static org.mockito.Mockito.inOrder;
99
import static org.mockito.Mockito.never;
1010
import static org.mockito.Mockito.verify;
11+
import static org.mockito.Mockito.verifyNoMoreInteractions;
1112
import static org.mockito.Mockito.when;
1213

1314
import org.apache.commons.lang.StringUtils;
@@ -16,6 +17,7 @@
1617
import org.mockito.Captor;
1718
import org.mockito.InOrder;
1819
import org.mockito.InjectMocks;
20+
import org.openqa.selenium.Keys;
1921

2022
import info.novatec.testit.webtester.AbstractPageObjectTest;
2123
import info.novatec.testit.webtester.api.exceptions.PageObjectIsDisabledException;
@@ -203,6 +205,15 @@ public void testThatAppendingTextOnDisabledFieldThrowsException() {
203205
cut.appendText("foo");
204206
}
205207

208+
/* pressing ENTER */
209+
210+
@Test
211+
public void testThatPressingEnterSendsCorrectKey() {
212+
cut.pressEnter();
213+
verify(webElement).sendKeys(Keys.ENTER);
214+
verifyNoMoreInteractions(webElement);
215+
}
216+
206217
/* correctness of class */
207218

208219
@Test

0 commit comments

Comments
 (0)