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()方法。

相关内容

热门资讯

查到实测辅助!wepoker亲... 查到实测辅助!wepoker亲友圈有用吗(辅助挂)原来真的有挂(有挂讲解辅助插件)1、免费脚本咨询教...
分享一款!拱趴大菠萝开挂方法(... 分享一款!拱趴大菠萝开挂方法(辅助挂)都是真的有挂(存在有挂辅助攻略)1、实时透视辅助更新:用户可以...
科技介绍!黑侠破解wepoke... 科技介绍!黑侠破解wepoker(辅助挂)确实是真的有挂(有挂详细辅助方法)1、模拟器是什么优化,俱...
最新技巧!德普之星app安卓版... 最新技巧!德普之星app安卓版破解版(辅助挂)竟然存在有挂(真的有挂辅助教程)1、每一步都需要思考,...
玩家交流!wepoker有没有... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家在这款游戏中打...
解密关于!wepoker插件功... 解密关于!wepoker插件功能辅助器(辅助挂)本来有挂(详细教程辅助器)1、任何透视是真的假的的玩...
科技揭秘!wpk俱乐部辅助器(... 科技揭秘!wpk俱乐部辅助器(辅助挂)好像真的是有挂(发现有挂辅助工具)1、全新机制【ai辅助工具激...
热点推荐!fishpoker透... 热点推荐!fishpoker透视(辅助挂)一直是真的有挂(有人有挂辅助app)1、不需要AI权限,帮...
让我来分享经验!xpoker辅... 让我来分享经验!xpoker辅助器(辅助挂)原来是真的有挂(的确有挂辅助攻略)1、每一步都需要思考,...
一分钟揭秘!xpoker辅助神... 一分钟揭秘!xpoker辅助神器(辅助挂)好像真的有挂(有挂辅助辅助器)1、脚本辅助下载、免费透视脚...