不在真实设备上运行测试的UI测试
创始人
2025-01-11 16:30:38
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测试,并验证登录功能的正确性。

相关内容

热门资讯

AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
AWR报告解读 WORKLOAD REPOSITORY PDB report (PDB snapshots) AW...
AWS管理控制台菜单和权限 要在AWS管理控制台中创建菜单和权限,您可以使用AWS Identity and Access Ma...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
群晖外网访问终极解决方法:IP... 写在前面的话 受够了群晖的quickconnet的小水管了,急需一个新的解决方法&#x...
​ToDesk 远程工具安装及... 目录 前言 ToDesk 优势 ToDesk 下载安装 ToDesk 功能展示 文件传输 设备链接 ...
Azure构建流程(Power... 这可能是由于配置错误导致的问题。请检查构建流程任务中的“发布构建制品”步骤,确保正确配置了“Arti...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...