Skip to content

Commit

Permalink
Merge pull request #66 from alkedr/master
Browse files Browse the repository at this point in the history
Fix for issue #65
  • Loading branch information
artkoshelev committed Aug 10, 2014
2 parents 2228bbb + 7b1ec96 commit 08c2580
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package ru.yandex.qatools.htmlelements.element;

import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;

import java.util.List;
Expand Down Expand Up @@ -68,23 +70,31 @@ protected void fillElement(WebElement element, Object value) {

if (isInput(element)) {
String inputType = element.getAttribute("type");
if (inputType == null || inputType.equals(TEXT_INPUT_TYPE) || inputType.equals(PASSWORD_INPUT_TYPE)) {
element.sendKeys(value.toString());
} else if (inputType.equals(CHECKBOX_TYPE)) {
if (inputType.equals(CHECKBOX_TYPE)) {
CheckBox checkBox = new CheckBox(element);
checkBox.set(Boolean.parseBoolean(value.toString()));
} else if (inputType.equals(RADIO_TYPE)) {
Radio radio = new Radio(element);
radio.selectByValue(value.toString());
} else {
element.sendKeys(getClearTextInputElementCharSequence(element) + value.toString());
}
} else if (isSelect(element)) {
Select select = new Select(element);
select.selectByValue(value.toString());
} else if (isTextArea(element)) {
element.sendKeys(value.toString());
element.sendKeys(getClearTextInputElementCharSequence(element) + value.toString());
}
}

// Returns sequence of backspaces and deletes that will clear element
// element.clear() can't be used because clear() generates separate onchange event
// element must be <textarea> or <input> with type text, password, email etc
// See https://github.com/yandex-qatools/htmlelements/issues/65
private static String getClearTextInputElementCharSequence(WebElement element) {
return StringUtils.repeat(Keys.DELETE.toString() + Keys.BACK_SPACE, element.getText().length());
}

private boolean isInput(WebElement element) {
return "input".equals(element.getTagName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.Keys;
import ru.yandex.qatools.htmlelements.testelements.MockedForm;

import java.util.HashMap;
Expand All @@ -15,6 +16,7 @@
*/
public class FormFillingTest {
private static final String INPUT_TEXT_TO_SEND = "text";
private static final String INPUT_WITH_TEXT_KEYS_TO_SEND = Keys.DELETE.toString() + Keys.BACK_SPACE.toString() + "text";
private static final boolean CHECKBOX_VALUE_TO_SET = true;

private final MockedForm form = new MockedForm();
Expand All @@ -24,10 +26,12 @@ public void fillForm() {
// Prepare data to fill form with
Map<String, Object> data = new HashMap<String, Object>();
data.put(MockedForm.TEXT_INPUT_NAME, INPUT_TEXT_TO_SEND);
data.put(MockedForm.TEXT_INPUT_WITH_TEXT_NAME, INPUT_TEXT_TO_SEND);
data.put(MockedForm.CHECKBOX_NAME, CHECKBOX_VALUE_TO_SET);
data.put(MockedForm.RADIO_NAME, MockedForm.RADIO_BUTTON_VALUE);
data.put(MockedForm.SELECT_NAME, MockedForm.SELECT_OPTION_VALUE);
data.put(MockedForm.TEXT_AREA_NAME, INPUT_TEXT_TO_SEND);
data.put(MockedForm.TEXT_AREA_WITH_TEXT_NAME, INPUT_TEXT_TO_SEND);

// Fill form
form.fill(data);
Expand All @@ -36,9 +40,11 @@ public void fillForm() {
@Test
public void formFieldsShouldBeFilledCorrectly() {
verify(form.getTextInput()).sendKeys(INPUT_TEXT_TO_SEND);
verify(form.getTextInputWithText()).sendKeys(INPUT_WITH_TEXT_KEYS_TO_SEND);
verify(form.getCheckBox()).click();
verify(form.getRadioButton()).click();
verify(form.getSelectOption()).click();
verify(form.getTextArea()).sendKeys(INPUT_TEXT_TO_SEND);
verify(form.getTextAreaWithText()).sendKeys(INPUT_WITH_TEXT_KEYS_TO_SEND);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,37 @@
*/
public class MockedForm extends Form {
public static final String TEXT_INPUT_NAME = "textinput";
public static final String TEXT_INPUT_WITH_TEXT_NAME = "textinput_with_text";
public static final String CHECKBOX_NAME = "checkbox";
public static final String SELECT_NAME = "select";
public static final String RADIO_NAME = "radio";
public static final String TEXT_AREA_NAME = "textarea";
public static final String TEXT_AREA_WITH_TEXT_NAME = "textarea_with_text";

public static final String RADIO_BUTTON_VALUE = "value";
public static final String SELECT_OPTION_VALUE = "value";

private final WebElement textInput;
private final WebElement textInputWithText;
private final WebElement checkBox;
private final WebElement selectOption;
private final WebElement radioButton;
private final WebElement textArea;
private final WebElement textAreaWithText;

public MockedForm() {
this(mock(WebElement.class));
}

private MockedForm(WebElement wrappedElement) {
super(wrappedElement);
textInput = mockTextInput();
textInput = mockTextInput(TEXT_INPUT_NAME, "");
textInputWithText = mockTextInput(TEXT_INPUT_WITH_TEXT_NAME, "a");
checkBox = mockCheckBox();
selectOption = mockSelectOption();
radioButton = mockRadioButton();
textArea = mockTextArea();
textArea = mockTextArea(TEXT_AREA_NAME, "");
textAreaWithText = mockTextArea(TEXT_AREA_WITH_TEXT_NAME, "a");
}

private WebElement mockInputWithNameAndType(String name, String type) {
Expand All @@ -50,9 +56,10 @@ private WebElement mockInputWithNameAndType(String name, String type) {
return element;
}

private WebElement mockTextInput() {
WebElement textInput = mockInputWithNameAndType(TEXT_INPUT_NAME, "text");
when(getWrappedElement().findElements(By.name(TEXT_INPUT_NAME))).thenReturn(Arrays.asList(textInput));
private WebElement mockTextInput(String name, String text) {
WebElement textInput = mockInputWithNameAndType(name, "text");
when(getWrappedElement().findElements(By.name(name))).thenReturn(Arrays.asList(textInput));
when(textInput.getText()).thenReturn(text);
return textInput;
}

Expand Down Expand Up @@ -84,18 +91,23 @@ private WebElement mockSelectOption() {
return selectOption;
}

private WebElement mockTextArea() {
private WebElement mockTextArea(String name, String text) {
WebElement textArea = mock(WebElement.class);
when(getWrappedElement().findElements(By.name(TEXT_AREA_NAME))).thenReturn(Arrays.asList(textArea));
when(getWrappedElement().findElements(By.name(name))).thenReturn(Arrays.asList(textArea));
when(textArea.getTagName()).thenReturn("textarea");
when(textArea.getAttribute("name")).thenReturn(TEXT_AREA_NAME);
when(textArea.getAttribute("name")).thenReturn(name);
when(textArea.getText()).thenReturn(text);
return textArea;
}

public WebElement getTextInput() {
return textInput;
}

public WebElement getTextInputWithText() {
return textInputWithText;
}

public WebElement getCheckBox() {
return checkBox;
}
Expand All @@ -111,4 +123,8 @@ public WebElement getRadioButton() {
public WebElement getTextArea() {
return textArea;
}

public WebElement getTextAreaWithText() {
return textAreaWithText;
}
}

0 comments on commit 08c2580

Please sign in to comment.