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

相关内容

热门资讯

辅助透视!德普之星透视辅助软件... 辅助透视!德普之星透视辅助软件下载,微信小程序游戏辅助器,详细脚本(真的有挂)-哔哩哔哩1.德普之星...
透视教你!德普之星透视辅助,德... 透视教你!德普之星透视辅助,德普之星辅助工具如何打开(透视)其实是真的器(哔哩哔哩)1)有没有挂:进...
透视脚本!wepoker透视挂... 透视脚本!wepoker透视挂底牌,微乐自建房免费黑科技有哪些,专业脚本(发现有挂)-哔哩哔哩进入游...
脚本曝光!aapoker辅助器... 脚本曝光!aapoker辅助器怎么用,aapoker插件(透视)本来真的是有器(哔哩哔哩)1、点击下...
透视挂透视!约局吧是否有挂,微... 透视挂透视!约局吧是否有挂,微信小程序微乐辅助免费版,透视脚本(的确有挂)-哔哩哔哩1、让任何用户在...
透视规律!约局吧开挂神器是真的... 透视规律!约局吧开挂神器是真的吗,微乐智能辅助真的假的,揭幕脚本(发现有挂)-哔哩哔哩约局吧开挂神器...
透视辅助!wepoker怎么提... 透视辅助!wepoker怎么提高运气,wepoker辅助软件视频(透视)确实存在有器(哔哩哔哩)1、...
随着!hhpoker开挂教程,... 随着!hhpoker开挂教程,微信小程序辅助器脚本,辅助透视(确实有挂)-哔哩哔哩1、完成hhpok...
脚本专业!wepoker轻量版... 脚本专业!wepoker轻量版透视方法,wepoker脚本下载(透视)确实真的是有教程(哔哩哔哩)一...
最新消息!aapoker发牌逻... 最新消息!aapoker发牌逻辑,微乐自建房安装挂免费,普及透视(有挂教程)-哔哩哔哩1、aapok...