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

相关内容

热门资讯

总算了解!we poker辅助... 总算了解!we poker辅助器v3.3,拱趴大菠萝自动计算机器人,其实有挂(存在有挂)1、该软件可...
必知教程!xpoker辅助工具... 必知教程!xpoker辅助工具,德州局怎么透视,原来是有挂(有挂讲解)1、全新机制【ai辅助工具激活...
技巧知识分享!pokerwor... 技巧知识分享!pokerworld软件,德州局hhpoker,一贯是有挂(讲解有挂)1、任何透视是真...
如何分辨真伪!佛手在线大菠萝辅... 如何分辨真伪!佛手在线大菠萝辅助,hhpoker破解工具,其实是真的有挂(有挂解密)1、打开软件启动...
专业讨论!wepoker私人局... 专业讨论!wepoker私人局外卦,werplan脚本,真是是有挂(证实有挂)1、起透看视 辅助软件...
如何分辨真伪!扑克之星辅助,w... 如何分辨真伪!扑克之星辅助,wepoker辅助脚本,好像是真的有挂(有挂技巧)1、全新机制【wepo...
我来向大家传授!拱趴大菠萝机器... 我来向大家传授!拱趴大菠萝机器人,we-poker辅助软件教程,总是存在有挂(有挂细节)1、公共底牌...
揭秘攻略!wepoker破解器... 揭秘攻略!wepoker破解器激活码,hhpoker是真的吗,真是真的有挂(有挂秘诀)1、全新机制【...
玩家必看科普!wepoker私... 玩家必看科普!wepoker私人局俱乐部怎么进,wepoker手机版透视脚本,原来是有挂(有挂教程)...
玩家必备科普!wepoker透... 玩家必备科普!wepoker透视底牌,wpk透视工作室,一直真的是有挂(真的有挂)破解侠是真的助透视...