我目前正在使用grantPerms方法,该方法应添加到所有测试类(所有需要访问Android权限的测试类),因此只需在正确的时间调用它(如果应用程序需要权限)。
以下是方案:
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
}
}
你是指有具体的问题需要解决吗?
更新我明白了,我将展示一个我在自己的项目中如何解决它的例子,对我来说这是一个好的解决方案。
so YOU just need to create a inner private method inside your test class (which currently require access to android permissions) scheme was added above
UPDATE2: generally you can't turn ON GPS services programmatically, this is not allowed since android v.4.2, so generally it's better to turn ON GPS services manually before test was started, but take a look at this
solution, may be this is what you want:
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());
}
}
}
因此,应该在您的应用程序中的所有位置调用这个方法,以便触发 GPS 警报 ()。