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

相关内容

热门资讯

透视开挂!pokemmo辅助官... 透视开挂!pokemmo辅助官网,cloudpoker怎么开挂-果然有辅助教程(哔哩哔哩)1、下载好...
透视分享!wepoker破解器... 透视分享!wepoker破解器有用吗,wepoker私人局辅助挂-好像是真的有辅助软件(哔哩哔哩)1...
透视辅助!pokemomo辅助... 透视辅助!pokemomo辅助工具,pokernow辅助控制-竟然是真的有辅助软件(哔哩哔哩)1、下...
透视开挂!德普之星透视辅助软件... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家在这款游戏中打...
透视普及!wpk辅助是什么,w... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】很多玩家在这款游戏中打...
透视曝光!wpk辅助器,wpk... 透视曝光!wpk辅助器,wpk软件是真的吗-切实有辅助技巧(哔哩哔哩)透视方法中分为三种模型:靠谱、...
透视普及!wepokerplu... 透视普及!wepokerplus万能挂,wepoker透视脚本免费下载-竟然真的是有辅助软件(哔哩哔...
透视推荐!wepoker辅助器... 透视推荐!wepoker辅助器最新版本更新内容,wepoker科技辅助器-本来是有辅助工具(哔哩哔哩...
透视普及!wepoker免费脚... 透视普及!wepoker免费脚本咨询,wepoker可以开透视吗-切实真的有辅助攻略(哔哩哔哩)1、...
透视曝光!wepoker辅助真... 透视曝光!wepoker辅助真的假的,wepoker辅助器安装包-确实真的是有辅助神器(哔哩哔哩)1...