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私人局辅助器怎么用(WePoKer提高)一贯真的有辅助方法(哔哩哔哩)1、超...
黑科技技巧!微乐小程序黑科技免... 黑科技技巧!微乐小程序黑科技免费,如何下载微乐自建房免费黑科技(透视)其实真的有辅助插件(哔哩哔哩)...
透视教程书!哈糖大菠萝有挂吗(... 透视教程书!哈糖大菠萝有挂吗(AApoker底牌)总是真的是有辅助神器(哔哩哔哩)1、首先打开哈糖大...
透视模块!hhpoker有没有... 透视模块!hhpoker有没有辅助(WePoKer方法)一贯是真的有辅助工具(哔哩哔哩);1、下载好...
做出回应!微信微乐游戏辅助脚本... 做出回应!微信微乐游戏辅助脚本,微乐江苏小程序游戏破解下载(透视)总是是有辅助器(哔哩哔哩)在进入软...
反观!微乐小程序免费黑科技,微... 反观!微乐小程序免费黑科技,微信小程序游戏破解微乐游戏(透视)总是真的有辅助神器(哔哩哔哩)1、很好...
透视教材!hhpoker辅助靠... 透视教材!hhpoker辅助靠谱吗(WePoKer破解版)其实存在有辅助教程(哔哩哔哩)一、hhpo...
黑科技插件!微乐小程序黑科技,... 黑科技插件!微乐小程序黑科技,微乐小程序微乐房间怎么开挂(透视)果然真的有辅助工具(哔哩哔哩)1、模...