SUR*_*A N 5 android android-permissions android-espresso
我正在使用 Espresso 进行 UI 测试 android。我想在设置中关闭位置的情况下运行测试,但由于其他测试未通过,我被定位启用对话框卡住了。我已经提到了我的观察以及到目前为止我所尝试的
用过Roboelectric,对问题没有影响。
使用Shadow操作,对问题没有影响。
谢谢你。
目前我正在使用应该添加到所有测试类(all = test-classes 需要访问 android 权限)的 grantPerms 方法,所以我只是在正确的时间调用它(如果应用程序需要 perms)。
这是方案:
public class MyClass {
// Rules etc.
@Test
public void myTestWithPerms() {
// click view, if it's need permissions to camera etc., just call
grantPerms(); //calling inner grantPerms method
// Another code, if you need access to gallery now
grantPerms(); //calling inner grantPerms method
// Some test code
}
private void grantPerms(){
// grantPermsCode
}
}
Run Code Online (Sandbox Code Playgroud)
或者你的意思是具体的?
更新 我明白了,所以我将展示一个例子,我是如何解决它的,对我来说这是一个很好的解决方案。
所以你只需要在你的测试类(目前需要访问 android 权限)中创建一个内部私有方法,上面添加了方案
UPDATE2:通常你不能以编程方式打开 GPS 服务,这是不允许的,因为 android v.4.2,所以通常最好在测试开始之前手动打开 GPS 服务,但看看这个
解决方案,可能这就是你想要的:
public class MyTestClass {
// Rules etc.
@Test
public void myTestWithTurnOnGPS() {
// once access the map check alert presence
tapTurnOnGpsBtn(); //call inner **tapTurnOnGpsBtn** method
}
private void tapTurnOnGpsBtn() throws UiObjectNotFoundException {
UiObject allowGpsBtn = device.findObject(new UiSelector()
.className("android.widget.Button").packageName("com.google.android.gms")
.resourceId("android:id/button1")
.clickable(true).checkable(false));
device.pressDelete(); // just in case to turn ON blur screen (not a wake up) for some devices like HTC and some other
if (allowGpsBtn.exists() && allowGpsBtn.isEnabled()) {
do {
allowGpsBtn.click();
} while (allowGpsBtn.exists());
}
}
Run Code Online (Sandbox Code Playgroud)
}
所以这个方法应该在你的应用程序中的所有地方调用,这些地方假设会引发 GPS 警报 ()
| 归档时间: |
|
| 查看次数: |
1561 次 |
| 最近记录: |