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

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

解决方法:

  1. 确保测试类继承自ActivityInstrumentationTestCase2或ActivityTestRule类。

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

相关内容

热门资讯

实测交流开挂!微扑克数据分析,... 『标题:当前标题』主打赛后复盘的 AI 游戏攻略黑科技走红,对局总结能力开挂。上传对局回放或者战绩截...
一分钟了解开挂!wepoker... 一款 AI 游戏攻略黑科技工具上线,查攻略效率直接开挂。它整合版本更新、装备搭配、BOSS 打法、牌...
玩家必看分享透视挂!德普之星私... 德普之星私人局辅助器AI 游戏攻略黑科技上线本地缓存功能,离线查阅能力开挂。提前下载对应游戏攻略数据...
重大通报!辅助!wepoker... 您好,重大通报!辅助!wepoker有辅助吗,德普之星怎么开挂安装(确实有辅助)这款游戏可以开挂的,...
揭秘攻略辅助!WPK模拟器,德... 一款 AI 游戏攻略黑科技工具上线,查攻略效率直接开挂。它整合版本更新、装备搭配、BOSS 打法、牌...
推荐一款辅助器!wepoker... 您好,推荐一款辅助器!wepoker代打辅助,wepoker开挂脚本教程(一向有辅助器)这款游戏可以...
必看攻略!辅助!wepoker... 『标题:当前标题』主打赛后复盘的 AI 游戏攻略黑科技走红,对局总结能力开挂。上传对局回放或者战绩截...
记者爆料辅助器!wpk有作弊吗... 本地部署 AI 黑科技工具迎来新版本,不用上传文件到云端就能完成wpk有作弊吗解析、 AI 处理、w...
今日科普辅助器!wepoker... 『标题:当前标题』主打赛后复盘的 AI 游戏攻略黑科技走红,对局总结能力开挂。上传对局回放或者战绩截...
一秒答解开挂!wepoker手... 一秒答解开挂!wepoker手机版透视脚本,wepoker辅助工具安装(先前是有开挂)主打本地运行的...