Android UI测试,context.startActivity()不起作用。
创始人
2024-08-19 02:30:12
0

问题描述: 在进行Android UI测试时,调用context.startActivity()方法没有起作用。

解决方法:

  1. 确保测试类继承自ActivityInstrumentationTestCase2ActivityTestRule类。

  2. 在测试方法中,使用InstrumentationRegistry.getTargetContext()方法获取目标上下文。

Context context = InstrumentationRegistry.getTargetContext();
  1. 使用Intent创建要启动的Activity,并将其作为参数传递给context.startActivity()方法。
Intent intent = new Intent(context, YourActivity.class);
context.startActivity(intent);

完整示例代码:

import android.content.Context;
import android.content.Intent;
import androidx.test.InstrumentationRegistry;
import androidx.test.rule.ActivityTestRule;
import androidx.test.runner.AndroidJUnit4;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(AndroidJUnit4.class)
public class YourUITest {

    @Rule
    public ActivityTestRule mActivityRule = new ActivityTestRule<>(YourActivity.class);

    @Test
    public void testStartActivity() {
        Context context = InstrumentationRegistry.getTargetContext();
        Intent intent = new Intent(context, YourActivity.class);
        context.startActivity(intent);
    }
}

通过以上步骤,你应该能够在Android UI测试中成功调用context.startActivity()方法。

相关内容

热门资讯

透视工具!aapoker安装包... 透视工具!aapoker安装包怎么使用(透视)aapoker万能辅助器,AA德州教程(有挂揭秘)1、...
透视数据!aapoker辅助工... 透视数据!aapoker辅助工具安全吗(透视)aapoker插件,安装教程(有挂细节);1、这是跨平...
透视总结!aapoker怎么设... 透视总结!aapoker怎么设置提高好牌几率(透视)aapoker辅助器是真的吗,解密教程(有挂揭秘...
透视系统!aapoker如何设... 透视系统!aapoker如何设置胜率(透视)aa poker透视软件,2025新版教程(有挂解说)1...
透视挂!aapoker辅助器怎... 透视挂!aapoker辅助器怎么用(透视)aapoker ai插件,解密教程(有挂揭秘)1.aapo...
透视中牌率!aa poker透... 透视中牌率!aa poker透视软件(透视)aapoker透视插件,解密教程(有挂解说)1、操作简单...
透视规律!aapoker免费透... 透视规律!aapoker免费透视脚本(透视)aapoker插件下载,AI教程(有挂揭秘)1、aapo...
透视新版!aapoker辅助工... 透视新版!aapoker辅助工具安全吗(透视)aapoker万能辅助器,教你攻略(有挂黑科技)1、构...
透视系统!aapoker安装包... 透视系统!aapoker安装包怎么使用(透视)aapoker透视插件,解说技巧(有挂细节)小薇(透视...
透视透视!aapoker俱乐部... 透视透视!aapoker俱乐部靠谱吗(透视)aapoker俱乐部靠谱吗,黑科技教程(有挂细节);1、...