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

Form.fill() doesn't clear text inputs before calling sendKeys() #84

Merged
merged 1 commit into from
Jun 19, 2015
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
@@ -1,12 +1,11 @@
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;
import java.util.Map;
import java.util.Objects;

/**
* Represents web page form tag. Provides handy way of filling form with data and submitting it.
Expand All @@ -17,10 +16,11 @@
* Date: 26.03.13
*/
public class Form extends TypifiedElement {
private static final String TEXT_INPUT_TYPE = "text";
private static final String PASSWORD_INPUT_TYPE = "password";
private static final String CHECKBOX_TYPE = "checkbox";
private static final String RADIO_TYPE = "radio";
private static final String CHECKBOX_FIELD = "checkbox";
private static final String RADIO_FIELD = "radio";
private static final String SELECT_FIELD = "select";
private static final String INPUT_FIELD = "input";
private static final String FILE_FIELD = "file";

/**
* Specifies {@link org.openqa.selenium.WebElement} representing form tag.
Expand All @@ -43,7 +43,7 @@ public void fill(Map<String, Object> data) {
for (String key : data.keySet()) {
WebElement elementToFill = findElementByKey(key);
if (elementToFill != null) {
fillElement(elementToFill, data.get(key));
fillElement(elementToFill, Objects.toString(data.get(key), ""));
}
}
}
Expand All @@ -63,47 +63,68 @@ protected WebElement findElementByKey(String key) {
return elements.get(0);
}

protected void fillElement(WebElement element, Object value) {
if (value == null) {
return;
protected void fillElement(WebElement element, String value) {
String elementType = getElementType(element);

if (CHECKBOX_FIELD.equals(elementType)) {
fillCheckBox(element, value);
} else if (RADIO_FIELD.equals(elementType)) {
fillRadio(element, value);
} else if (INPUT_FIELD.equals(elementType)) {
fillInput(element, value);
} else if (SELECT_FIELD.equals(elementType)) {
fillSelect(element, value);
} else if (FILE_FIELD.equals(elementType)) {
fillFile(element, value);
}
}

if (isInput(element)) {
String inputType = element.getAttribute("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());
protected String getElementType(WebElement element) {
String tagName = element.getTagName();
if ("input".equals(tagName)) {
String type = element.getAttribute("type");
if ("checkbox".equals(type)) {
return CHECKBOX_FIELD;
}
if ("radio".equals(type)) {
return RADIO_FIELD;
}
if ("file".equals(type)) {
return FILE_FIELD;
}
} else if (isSelect(element)) {
Select select = new Select(element);
select.selectByValue(value.toString());
} else if (isTextArea(element)) {
element.sendKeys(getClearTextInputElementCharSequence(element) + value.toString());
return INPUT_FIELD;
}
if ("select".equals(tagName)) {
return SELECT_FIELD;
}
if ("textarea".equals(tagName)) {
return INPUT_FIELD;
}
return null;
}

protected void fillCheckBox(WebElement element, String value) {
CheckBox checkBox = new CheckBox(element);
checkBox.set(Boolean.parseBoolean(value));
}

// 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());
protected void fillRadio(WebElement element, String value) {
Radio radio = new Radio(element);
radio.selectByValue(value);
}

private boolean isInput(WebElement element) {
return "input".equals(element.getTagName());
protected void fillInput(WebElement element, String value) {
TextInput input = new TextInput(element);
input.sendKeys(input.getClearCharSequence() + value);
}

private boolean isSelect(WebElement element) {
return "select".equals(element.getTagName());
protected void fillSelect(WebElement element, String value) {
Select select = new Select(element);
select.selectByValue(value);
}

private boolean isTextArea(WebElement element) {
return "textarea".equals(element.getTagName());
protected void fillFile(WebElement element, String value) {
FileInput fileInput = new FileInput(element);
fileInput.setFileToUpload(value);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ru.yandex.qatools.htmlelements.element;

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

/**
Expand Down Expand Up @@ -37,8 +39,8 @@ public void sendKeys(CharSequence... keys) {
/**
* @return Text entered into the text input.
* @deprecated Use getText() instead.
* <p/>
* Retrieves the text entered into this text input.
* <p/>
* Retrieves the text entered into this text input.
*/
@Deprecated
public String getEnteredText() {
Expand All @@ -61,4 +63,13 @@ public String getText() {
}
return enteredText;
}

/**
* Returns sequence of backspaces and deletes that will clear element.
* clear() can't be used because generates separate onchange event
* See https://github.com/yandex-qatools/htmlelements/issues/65
*/
public String getClearCharSequence() {
return StringUtils.repeat(Keys.DELETE.toString() + Keys.BACK_SPACE, getText().length());
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package ru.yandex.qatools.htmlelements;

import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.Keys;
import ru.yandex.qatools.htmlelements.testelements.MockedForm;
Expand All @@ -21,7 +20,6 @@ public class FormFillingTest {

private final MockedForm form = new MockedForm();

@Before
public void fillForm() {
// Prepare data to fill form with
Map<String, Object> data = new HashMap<String, Object>();
Expand All @@ -39,6 +37,7 @@ public void fillForm() {

@Test
public void formFieldsShouldBeFilledCorrectly() {
fillForm();
verify(form.getTextInput()).sendKeys(INPUT_TEXT_TO_SEND);
verify(form.getTextInputWithText()).sendKeys(INPUT_WITH_TEXT_KEYS_TO_SEND);
verify(form.getCheckBox()).click();
Expand All @@ -47,4 +46,12 @@ public void formFieldsShouldBeFilledCorrectly() {
verify(form.getTextArea()).sendKeys(INPUT_TEXT_TO_SEND);
verify(form.getTextAreaWithText()).sendKeys(INPUT_WITH_TEXT_KEYS_TO_SEND);
}

@Test
public void clearingFieldWhenNullIsPassed() {
Map<String, Object> data = new HashMap<String, Object>();
data.put(MockedForm.TEXT_INPUT_WITH_TEXT_NAME, null);
form.fill(data);
verify(form.getTextInputWithText()).sendKeys(Keys.DELETE.toString() + Keys.BACK_SPACE.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private WebElement mockInputWithNameAndType(String name, String type) {
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);
when(textInput.getAttribute("value")).thenReturn(text);
return textInput;
}

Expand Down