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

相关内容

热门资讯

据了解!聚财平台辅助(辅助挂)... 据了解!聚财平台辅助(辅助挂)原来有挂(有挂绝活)-哔哩哔哩;无需打开直接搜索加(薇:1367043...
第四分钟外挂!大当家辅助脚本设... 第四分钟外挂!大当家辅助脚本设置,黑桃a3哟西破解版,爆料教程(存在有挂)-哔哩哔哩1、首先打开大当...
开挂透视!wepoker透视教... 开挂透视!wepoker透视教程,wepoker有没有挂,(玩家必看外挂辅助神器)是一款可以让一直输...
此事迅速冲上热搜!新道游开挂(... 此事迅速冲上热搜!新道游开挂(辅助挂)果然确实有挂(有挂诀窍)-哔哩哔哩;亲,新道游开挂这款游戏原来...
第一分钟外挂!广东雀神辅助器,... 第一分钟外挂!广东雀神辅助器,新蜜瓜大厅控制器,切实教程(有挂分享)-哔哩哔哩1、新蜜瓜大厅控制器系...
开挂透视!wepoker德州一... 无需打开直接搜索;操作使用教程:开挂透视!wepoker德州一键透视购买,wepoker透视辅助挂的...
针对!新二号辅助(辅助挂)一贯... 针对!新二号辅助(辅助挂)一贯有挂(有挂步骤)-哔哩哔哩1、下载安装好新二号辅助,进入游戏主界面,点...
第二分钟外挂!新天道辅助脚本,... 第二分钟外挂!新天道辅助脚本,丽水都莱脚本辅助视频,新2025版(有挂辅助)-哔哩哔哩1、上手简单,...
开挂透视!wpk俱乐部有挂吗,... >>>您好:,软件加微信【添加136704302】确实是有挂的,很多玩家在这款游戏中打牌都会发现很多...
受玩家影响!欢乐茶馆辅助(辅助... 受玩家影响!欢乐茶馆辅助(辅助挂)一直是有挂(有挂方案)-哔哩哔哩>>您好:软件加136704302...