/** * Class constructor specifying the evaluator to use and the id of the textField we want to test. * The constructor checks if the given id does correspond to a textField. * * @param evaluator a technology specific evaluator * @param id the id (unique) of the textField */ public TextField(Evaluator evaluator, String id) { super(evaluator, id); checkIsExpectedComponent(TextField); } }
/** * Uses the matcher ContainsText to compare the content of a given textfield to the expected values of the matcher, * using the syntax "containsText("xxx")" or "containsExactlyText("xxx")" * * @param textField the textField * @return True if the textfield contains (contains exactly for second syntax) the values of the matcher ContainsText */ public boolean matchesSafely(TextField textField) { if (exactly) { return text.equals(textField.value()); } String patternStr = text; Pattern pattern = Pattern.compile(patternStr); java.util.regex.Matcher matcher = pattern.matcher(textField.value()); return matcher.find(); }
public static TextField textFieldWithMaxLength(final int maxLength) { Evaluator evaluator = mock(Evaluator.class); when(evaluator.existComponent(id)).thenReturn(true); when(evaluator.componentType(id)).thenReturn(TextField); when(evaluator.isVisible(any(Component.class))).thenReturn(true); when(evaluator.isEnabled(any(Component.class))).thenReturn(true); when(evaluator.label(any(TextField.class))).thenReturn("label"); when(evaluator.value(any(TextField.class))).thenReturn("textFieldValue"); when(evaluator.maxLength(any(TextField.class))).thenReturn(maxLength); return new TextField(evaluator, id); }
@Test public void test_toString() { when(evaluator.value(any(TextField.class))).thenReturn("myValue"); when(evaluator.label(any(TextField.class))).thenReturn("label"); when(evaluator.maxLength(any(TextField.class))).thenReturn(10); TextField textField = new TextField(evaluator, id); assertThat(textField.toString(), is("class org.testatoo.core.component.TextField with state : enabled:true, visible:true, value:myValue, label:label, maxLength:10")); } }
@Test public void can_check_max_length_accepted_by_textfield() { when(evaluator.value(any(TextField.class))).thenReturn(""); when(evaluator.label(any(TextField.class))).thenReturn(""); when(evaluator.maxLength(any(TextField.class))).thenReturn(10); TextField textField = new TextField(evaluator, id); assertThat(textField.maxLength(), is(10)); }
public static TextField simpleTextFieldWithText(final String text) { Evaluator evaluator = mock(Evaluator.class); when(evaluator.existComponent(id)).thenReturn(true); when(evaluator.componentType(id)).thenReturn(TextField); when(evaluator.isVisible(any(Component.class))).thenReturn(true); when(evaluator.isEnabled(any(Component.class))).thenReturn(true); when(evaluator.label(any(TextField.class))).thenReturn("label"); when(evaluator.value(any(TextField.class))).thenReturn(text); when(evaluator.maxLength(any(TextField.class))).thenReturn(50); return new TextField(evaluator, id); }
@Test public void test_component_type() { evaluator = mock(Evaluator.class); when(evaluator.existComponent(id)).thenReturn(true); when(evaluator.componentType(id)).thenReturn(Radio); try { new TextField(evaluator, id); fail(); } catch (ComponentException e) { assertThat(e.getMessage(), is("The component with id=" + id + " is not a TextField but a Radio")); } }
@Test public void textfield_usage_through_language_part3() { when(evaluator.componentType(id)).thenReturn(TextField); TextField textField = new TextField(evaluator, id); when(evaluator.maxLength(textField)).thenReturn(255); when(evaluator.label(textField)).thenReturn("myLabel"); when(evaluator.value(textField)).thenReturn(""); enter("SomeData", on(textField)); verify(evaluator, atLeastOnce()).focusOn(textField); verify(evaluator, times(1)).reset(textField); verify(evaluator, times(1)).type("SomeData"); }
@Test public void textfield_usage_through_language_part1() { when(evaluator.componentType(id)).thenReturn(TextField); TextField textField = new TextField(evaluator, id); when(evaluator.label(textField)).thenReturn("myLabel"); when(evaluator.value(textField)).thenReturn("myValue"); assertThat(textField, has(label("myLabel"))); assertThat(textField, has(value("myValue"))); clickOn(textField); Keyboard.type("SomeData"); verify(evaluator, times(1)).click(textField, Click.left); verify(evaluator, times(1)).type("SomeData"); }
@Test public void test_keyboard_usage() { when(evaluator.componentType(id)).thenReturn(TextField); TextField textField = new TextField(evaluator, id); when(evaluator.maxLength(textField)).thenReturn(255); when(evaluator.label(textField)).thenReturn("label"); when(evaluator.value(textField)).thenReturn("value"); type("Some Data", on(textField)); Keyboard.type("Other Data"); Keyboard.keyDown(KeyModifier.SHIFT); Keyboard.release(KeyModifier.CONTROL); Keyboard.press(Key.F6); Keyboard.release(); verify(evaluator, atLeastOnce()).focusOn(textField); verify(evaluator, times(1)).type("Some Data"); verify(evaluator, times(1)).type("Other Data"); verify(evaluator, times(1)).keyDown(KeyModifier.SHIFT); verify(evaluator, times(1)).release(KeyModifier.CONTROL); verify(evaluator, times(1)).press(Key.F6); verify(evaluator, times(1)).release(); }
@Test public void textfield_usage_through_language_part2() { when(evaluator.componentType(id)).thenReturn(TextField); TextField textField = new TextField(evaluator, id); when(evaluator.maxLength(textField)).thenReturn(255); when(evaluator.label(textField)).thenReturn("myLabel"); when(evaluator.value(textField)).thenReturn("myValue"); assertThat(textField, is(enabled())); and(not(disabled())); and(it(), visible()); clickOn(textField); Mouse.clickOn(textField); type("data_1", into(textField)); verify(evaluator, times(2)).click(textField, Click.left); verify(evaluator, times(2)).focusOn(any(Component.class)); verify(evaluator, times(1)).type("data_1"); }