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

相关内容

热门资讯

推荐一款!wepoker-h5... 1、推荐一款!wepoker-h5下载,如何下载德普之星辅助软件,解说技巧(有挂透视)(UU pok...
透视有挂!hhpoker免费透... 透视有挂!hhpoker免费透视脚本(透视)确实真的有挂(详细辅助我来教教你)1、hhpoker免费...
今日头条!wepoker软件安... 今日头条!wepoker软件安装包,epoker免费透视脚本,揭秘攻略(有挂教程);最新版2024是...
透视游戏!hh poker辅助... 透视游戏!hh poker辅助有用吗(透视)总是是真的有挂(详细辅助细节揭秘);1)辅助挂:进一步探...
总算明白!wepoker免费脚... 总算明白!wepoker免费脚本咨询,云扑克有透视吗,新2025版(有挂技巧);wepoker免费脚...
透视最新!aapoker脚本怎... 透视最新!aapoker脚本怎么用(透视)原来存在有挂(详细辅助可靠教程)一、aapoker脚本怎么...
研究成果!wepoker辅助器... 研究成果!wepoker辅助器官方,sohoo辅助,2025版教程(有挂方法)这是由厦门游乐互动科技...
透视肯定!wepoker透视底... 透视肯定!wepoker透视底牌(透视)确实真的有挂(详细辅助揭秘教程);1、打开软件启动之后找到中...
分辨真假!wepoker免费脚... 分辨真假!wepoker免费脚本咨询,云扑克有透视吗,教你攻略(有挂教程)1、很好的工具软件,可以解...
透视智能ai!佛手大菠萝13道... 透视智能ai!佛手大菠萝13道挂哪里(透视)果然存在有挂(详细辅助可靠教程)1、完成的残局,帮助玩家...