不在真实设备上运行测试的UI测试
创始人
2025-01-11 16:30:22
0

解决方法:使用模拟器或虚拟设备进行UI测试。

代码示例: 以下是使用Android模拟器进行UI测试的示例代码:

import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.LargeTest;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.rule.ActivityTestRule;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.action.ViewActions.typeText;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withId;

@RunWith(AndroidJUnit4.class)
@LargeTest
public class LoginTest {

    private String username;
    private String password;

    @Rule
    public ActivityTestRule activityRule = new ActivityTestRule<>(LoginActivity.class);

    @Before
    public void init() {
        // 初始化用户名和密码
        username = "testuser";
        password = "testpassword";
    }

    @Test
    public void loginTest() {
        // 输入用户名和密码
        onView(withId(R.id.username_edittext)).perform(typeText(username));
        onView(withId(R.id.password_edittext)).perform(typeText(password));

        // 点击登录按钮
        onView(withId(R.id.login_button)).perform(click());

        // 验证登录成功后的界面是否显示
        onView(withId(R.id.home_layout)).check(matches(isDisplayed()));
    }
}

在上述示例代码中,我们使用了AndroidJUnit4测试框架,并使用了ActivityTestRule来启动LoginActivity进行测试。在@Before注解的方法中,我们初始化了用户名和密码。在@Test注解的方法中,我们使用Espresso库的方法来模拟用户的输入和点击操作,并使用匹配器来验证登录成功后的界面是否显示。这样我们就可以在模拟器上运行这个UI测试,并验证登录功能的正确性。

相关内容

热门资讯

透视了解!aapoker透视脚... WePoke高级策略深度解析‌;透视了解!aapoker透视脚本安装包,wpk插件,介绍教程(一直有...
透视工具!wepoker辅助工... 透视工具!wepoker辅助工具,wepoker怎么买辅助,我来教教你(一直有挂);(需添加指定Q群...
透视透视!hhpoker透视下... 透视透视!hhpoker透视下载链接在哪里,wepoker底牌透视脚本,黑科技教程(真是是有挂);免...
透视插件!hhpoker透视脚... 透视插件!hhpoker透视脚本安卓,hhpoker哪个俱乐部靠谱,软件教程(竟然有挂);wpk透视...
透视插件!aapoker脚本怎... 透视插件!aapoker脚本怎么用,哈糖大菠萝有挂吗5个常用方法,详细教程(先前真的有挂);玩家必备...
透视黑科技!wpk脚本是什么,... 透视黑科技!wpk脚本是什么,aapoker辅助软件合法吗,总结教程(一贯存在有挂);科技安装教程;...
透视安卓版!智星菠萝辅助怎么买... 透视安卓版!智星菠萝辅助怎么买,we poker辅助器下载,透视教程(原先存在有挂);一、智星菠萝辅...
透视透视!德普之星透视辅助软件... 透视透视!德普之星透视辅助软件下载,hh poker透视器下载,力荐教程(最初真的有挂);亲们利用一...
透视教程!hhpoker透视脚... 透视教程!hhpoker透视脚本,菠萝辅助器免费版的特点,透明挂教程(起初真的有挂)1、很好的工具软...
透视新版!aapoker辅助器... 透视新版!aapoker辅助器是真的吗,wepoker私人局辅助挂,AI教程(固有是真的有挂)准备好...