Cha*_*ham 26 android android-testing android-espresso
我知道Espresso可以按照UiAutomator的方式点击边界.(x和y坐标)我已经阅读了文档,但我似乎无法找到它.任何帮助表示赞赏.谢谢
编辑
我找到了这个链接,但没有示例如何使用它,我主要关心的是它UiController是如何使用它.
haf*_*fax 49
咖啡有GeneralClickAction,这是ViewActions的底层实现click(),doubleClick()和longClick().
该GeneralClickAction构造函数采用CoordinatesProvider第二个参数.所以基本的想法是创建一个ViewAction提供自定义的静态getter CoordinatesProvider.像这样的东西:
public static ViewAction clickXY(final int x, final int y){
return new GeneralClickAction(
Tap.SINGLE,
new CoordinatesProvider() {
@Override
public float[] calculateCoordinates(View view) {
final int[] screenPos = new int[2];
view.getLocationOnScreen(screenPos);
final float screenX = screenPos[0] + x;
final float screenY = screenPos[1] + y;
float[] coordinates = {screenX, screenY};
return coordinates;
}
},
Press.FINGER);
}
Run Code Online (Sandbox Code Playgroud)
Espresso的一般建议:查看源代码,而不是查找文档(几乎没有).Espresso是开源的,源代码本身质量非常好.
lil*_*die 11
@ haffax的答案很棒,效果很好.
但是,如果要在视图的某个部分中单击可能在屏幕之间发生变化,则根据百分比(或比率)单击可能很有用,因为即使dp数字在所有屏幕上也可能不稳定.所以,我对它做了一个简单的修改:
public static ViewAction clickPercent(final float pctX, final float pctY){
return new GeneralClickAction(
Tap.SINGLE,
new CoordinatesProvider() {
@Override
public float[] calculateCoordinates(View view) {
final int[] screenPos = new int[2];
view.getLocationOnScreen(screenPos);
int w = view.getWidth();
int h = view.getHeight();
float x = w * pctX;
float y = h * pctY;
final float screenX = screenPos[0] + x;
final float screenY = screenPos[1] + y;
float[] coordinates = {screenX, screenY};
return coordinates;
}
},
Press.FINGER);
}
Run Code Online (Sandbox Code Playgroud)
我想我会在这里分享,以便其他人可以受益.
尽管不赞成使用该方法,但是有效的答案对我有所帮助。现在您必须指定inputDevice(例如InputDevice.SOURCE_MOUSE)和buttonState(例如MotionEvent.BUTTON_PRIMARY
Kotlin中的示例:
companion object {
fun clickIn(x: Int, y: Int): ViewAction {
return GeneralClickAction(
Tap.SINGLE,
CoordinatesProvider { view ->
val screenPos = IntArray(2)
view?.getLocationOnScreen(screenPos)
val screenX = (screenPos[0] + x).toFloat()
val screenY = (screenPos[1] + y).toFloat()
floatArrayOf(screenX, screenY)
},
Press.FINGER,
InputDevice.SOURCE_MOUSE,
MotionEvent.BUTTON_PRIMARY)
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8690 次 |
| 最近记录: |