gpt4 book ai didi

java - 自动授予 Espresso UI 测试权限

转载 作者:搜寻专家 更新时间:2023-11-01 09:25:42 24 4
gpt4 key购买 nike

我试图避免在使用 Espresso 构建的每个 UI 测试上手动授予权限。

我试过:

 @Rule
public GrantPermissionRule permissionRule = GrantPermissionRule.grant(
android.Manifest.permission.INTERNET,
android.Manifest.permission.READ_PHONE_STATE,
android.Manifest.permission.ACCESS_NETWORK_STATE,
android.Manifest.permission.ACCESS_FINE_LOCATION.....)

 @Before
public void grantPhonePermission()
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
getInstrumentation().getUiAutomation().executeShellCommand(
"pm grant " + getTargetContext().getPackageName()
+ " android.permission.ACCESS_FINE_LOCATION");
}
}

这是我的大部分 gradle 文件:

apply plugin: 'com.android.application'

android {
compileSdkVersion 24
buildToolsVersion '26.0.2'
defaultConfig {
minSdkVersion 14
targetSdkVersion 23
testInstrumentationRunner
'android.support.test.runner.AndroidJUnitRunner'
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.txt'
debuggable false
jniDebuggable false
buildConfigField "boolean", "UseProduction", "true"
}
qa {
initWith debug
debuggable true
applicationIdSuffix ".qa"
buildConfigField "boolean", "UseProduction", "false"
}
debug {
debuggable true
testCoverageEnabled = false
buildConfigField "boolean", "UseProduction", "true"
}

}

flavorDimensions "standard"

productFlavors {
standard
{
buildConfigField "boolean", "ChinaBuild", "false"
buildConfigField "boolean", "YTOBuild", "false"
}
....
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
sourceSets {
main {
java.srcDirs = ['src/main/java']
}
}


dependencies {
implementation 'com.android.support:support-v4:24.2.1'
implementation 'com.android.support:appcompat-v7:24.2.1'
implementation 'com.android.support:design:24.2.1'
implementation 'com.android.support:recyclerview-v7:24.2.1'
implementation 'com.android.support:percent:24.2.1'
implementation 'com.google.firebase:firebase-messaging:10.0.1'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-
core:3.0.2'
androidTestImplementation 'junit:junit:4.12'
androidTestImplementation 'org.hamcrest:hamcrest-library:1.3'
androidTestImplementation 'org.mockito:mockito-core:2.+'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test:rules:1.0.2'
}

这些都不起作用。如您所见,我的目标是 API 23,因此 GrantPermissionRule 应该可以工作。有任何想法吗?我已经坚持了3天了。 :(

看起来我的帖子主要是代码,我应该添加更多细节......但不确定还需要说什么。

最佳答案

你所做的几乎是正确的,只是你应该在执行 shell 命令后“hibernate ”一下。这就是您的方法主体的样子:

    ArrayList<String> permissions = new ArrayList<>();
permissions.add(Manifest.permission.ACCESS_FINE_LOCATION);
//add here your other permissions

for (int i = 0; i < permissions.size(); i++) {
String command = String.format("pm grant %s %s", getTargetContext().getPackageName(), permissions.get(i));
getInstrumentation().getUiAutomation().executeShellCommand(command);
// wait a bit until the command is finished
SystemClock.sleep(1000);
}

关于java - 自动授予 Espresso UI 测试权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50856546/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com