Added support to clear an editable element (i.e. text field)

Added unit test
This commit is contained in:
Eric Plaster
2013-04-29 19:11:50 -05:00
parent 62d482563e
commit 1e677d8cee
5 changed files with 88 additions and 1 deletions

View File

@@ -370,7 +370,7 @@ Android.prototype.complexTap = function(tapCount, touchCount, duration, x, y, el
};
Android.prototype.clear = function(elementId, cb) {
cb(new NotYetImplementedError(), null);
this.proxy(["element:clear", {elementId: elementId}], cb);
};
Android.prototype.submit = function(elementId, cb) {

View File

@@ -28,4 +28,33 @@ describeWd('text boxes', function(h) {
});
});
});
it('should be able to clear editText', function(done) {
h.driver.elementByTagName('editText', function(err, el) {
should.not.exist(err);
// get the text
el.text(function(err, text) {
should.not.exist(err);
text.should.equal("");
// set the text
el.sendKeys(testText, function(err) {
should.not.exist(err);
// make sure the text is actuall in the edittext
el.text(function(err, text) {
should.not.exist(err);
text.should.equal(testText);
// now clear it
el.clear(function(err) {
should.not.exist(err);
// make sure that it's empty
el.text(function(err, text) {
should.not.exist(err);
text.should.equal("");
done();
});
});
});
});
});
});
});
});

View File

@@ -1,6 +1,7 @@
package io.appium.android.bootstrap;
import io.appium.android.bootstrap.exceptions.AndroidCommandException;
import io.appium.android.bootstrap.handler.Clear;
import io.appium.android.bootstrap.handler.Click;
import io.appium.android.bootstrap.handler.Find;
import io.appium.android.bootstrap.handler.Flick;
@@ -25,6 +26,7 @@ class AndroidCommandExecutor {
private static HashMap<String, CommandHandler> map = new HashMap<String, CommandHandler>();
static {
map.put("clear", new Clear());
map.put("orientation", new Orientation());
map.put("swipe", new Swipe());
map.put("flick", new Flick());

View File

@@ -27,6 +27,10 @@ public class AndroidElement {
el = uiObj;
}
public void clearText() throws UiObjectNotFoundException {
el.clearTextField();
}
public boolean click() throws UiObjectNotFoundException {
return el.click();
}

View File

@@ -0,0 +1,52 @@
package io.appium.android.bootstrap.handler;
import io.appium.android.bootstrap.AndroidCommand;
import io.appium.android.bootstrap.AndroidCommandResult;
import io.appium.android.bootstrap.AndroidElement;
import io.appium.android.bootstrap.CommandHandler;
import io.appium.android.bootstrap.WDStatus;
import io.appium.android.bootstrap.exceptions.ElementNotInHashException;
import org.json.JSONException;
import com.android.uiautomator.core.UiObjectNotFoundException;
/**
* This handler is used to clear elements in the Android UI.
*
* Based on the element Id, clear that element.
*
* @author <a href="https://github.com/xuru">xuru</a>
*
*/
public class Clear extends CommandHandler {
/*
* @param command The {@link AndroidCommand}
*
* @return {@link AndroidCommandResult}
*
* @throws JSONException
*
* @see io.appium.android.bootstrap.CommandHandler#execute(io.appium.android.
* bootstrap.AndroidCommand)
*/
@Override
public AndroidCommandResult execute(final AndroidCommand command)
throws JSONException {
if (command.isElementCommand()) {
try {
final AndroidElement el = command.getElement();
el.clearText();
return getSuccessResult(true);
} catch (final UiObjectNotFoundException e) {
return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT,
e.getMessage());
} catch (final ElementNotInHashException e) {
return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT,
e.getMessage());
}
}
return getErrorResult("Unknown error clearing text");
}
}