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、点击下载安装,竞技...
查到实测!云扑克有透视吗,po... 查到实测!云扑克有透视吗,pokemmo脚本,原来真的有挂(证实有挂)1)辅助插件:进一步探索辅助大...
实测发现!wepoker破解器... 实测发现!wepoker破解器激活码,hhpoker是真的吗,确实真的是有挂(有挂细节)破解侠是真的...
技术分享!wepoker有透视... 技术分享!wepoker有透视吗,wepoker透视脚本视频,竟然是真的有挂(有挂实锤)1、玩家可以...
今日公布!hhpoker怎么开... 今日公布!hhpoker怎么开透视,wejoker免费脚本,好像真的是有挂(有挂规律)1、今日公布!...
推荐十款!wepoker有辅助... 推荐十款!wepoker有辅助功能吗,hhpoker透视脚本下载,其实是真的有挂(有挂透视)1、上手...
今日科普!德普之星私人局辅助免... 今日科普!德普之星私人局辅助免费,aapoker能控制牌吗,竟然存在有挂(有挂辅助)1、上手简单,内...
查到实测!wepoker透视版... 查到实测!wepoker透视版下载,werplan外卦神器,一直是真的有挂(有挂总结)运辅助工具,进...
记者揭秘!来玩app 德州 辅... 记者揭秘!来玩app 德州 辅助,wepoker新号好一点吗,一直有挂(有挂猫腻)一、可以开透视的定...
最新技巧!xpoker辅助工具... 最新技巧!xpoker辅助工具,hhpoker德州透视,一贯真的有挂(存在有挂)1、许多玩家不知道辅...