package io.appium.android.bootstrap.handler;
import android.graphics.Rect;
import com.android.uiautomator.core.UiObjectNotFoundException;
import io.appium.android.bootstrap.*;
import org.json.JSONException;
import org.json.JSONObject;
/**
* This handler is used to get the size of elements that support it.
*
*/
public class GetSize extends CommandHandler {
/*
* @param command The {@link AndroidCommand} used for this handler.
*
* @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()) {
// Only makes sense on an element
final JSONObject res = new JSONObject();
try {
final AndroidElement el = command.getElement();
final Rect rect = el.getBounds();
res.put("width", rect.width());
res.put("height", rect.height());
} catch (final UiObjectNotFoundException e) {
return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT,
e.getMessage());
} catch (final Exception e) { // handle NullPointerException
return getErrorResult("Unknown error");
}
return getSuccessResult(res);
} else {
return getErrorResult("Unable to get text without an element.");
}
}
}
获取控件的宽和高,调用的是UiObject的getBounds()。得到一个矩形,然后获得其宽和高。
package io.appium.android.bootstrap.handler;
import android.graphics.Rect;
import io.appium.android.bootstrap.*;
import org.json.JSONException;
import org.json.JSONObject;
/**
* This handler is used to get the text of elements that support it.
*
*/
public class GetLocation extends CommandHandler {
/*
* @param command The {@link AndroidCommand} used for this handler.
*
* @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()) {
return getErrorResult("Unable to get location without an element.");
}
try {
final JSONObject res = new JSONObject();
final AndroidElement el = command.getElement();
final Rect bounds = el.getBounds();
res.put("x", bounds.left);
res.put("y", bounds.top);
return getSuccessResult(res);
} catch (final Exception e) {
return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT, e.getMessage());
}
}
}
获取控件的起始点坐标。调用的也是getBounds,然后得到其起始点的x,y 坐标