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

相关内容

热门资讯

2分钟攻略!哈糖大菠萝攻略(透... 2分钟攻略!哈糖大菠萝攻略(透视)都是存在有辅助脚本(哔哩哔哩)小薇(辅助器软件下载)致您一封信;亲...
三分钟资料!德普之星透视辅助软... 三分钟资料!德普之星透视辅助软件(透视)一直是有辅助神器(哔哩哔哩)1、下载好德普之星透视辅助软件透...
一分钟指南!智星德州插件202... 一分钟指南!智星德州插件2024最新版(透视)好像存在有辅助脚本(哔哩哔哩)1、全新机制【智星德州插...
第1分钟攻略!约局吧如何查看是... 第1分钟攻略!约局吧如何查看是否有挂(透视)原来存在有辅助下载(哔哩哔哩)约局吧如何查看是否有挂辅助...
第七分钟学习!hhpoker辅... 第七分钟学习!hhpoker辅助软件(透视)本来真的是有辅助插件(哔哩哔哩)1、hhpoker辅助软...
四分钟总结!hhpoker免费... 四分钟总结!hhpoker免费辅助器(透视)真是真的是有辅助下载(哔哩哔哩)1.hhpoker免费辅...
第二分钟手册!wepoker辅... 第二分钟手册!wepoker辅助器(透视)其实是真的有辅助教程(哔哩哔哩)1、完成wepoker辅助...
4分钟窍要!wepoker透视... 4分钟窍要!wepoker透视功能下载(透视)果然有辅助神器(哔哩哔哩)1、全新机制【wepoker...
三分钟妙计!wepoker私人... 三分钟妙计!wepoker私人局有透视吗(透视)一直真的有辅助工具(哔哩哔哩)1、三分钟妙计!wep...
七分钟方针!拱趴大菠萝自动计算... 七分钟方针!拱趴大菠萝自动计算机器人(透视)原来存在有辅助辅助(哔哩哔哩)1、完成拱趴大菠萝自动计算...