Android Espresso点击并设置自定义时间。
创始人
2024-08-13 17:00:50
0

要在Android Espresso中进行点击并设置自定义时间,可以使用ViewActions类中的click方法结合ViewMatchers类中的withId方法来定位并点击目标视图。然后,可以使用Thread.sleep()方法来设置自定义时间。

以下是一个示例代码:

import android.view.View;

import androidx.test.espresso.UiController;
import androidx.test.espresso.ViewAction;
import androidx.test.espresso.matcher.ViewMatchers;
import androidx.test.ext.junit.rules.ActivityScenarioRule;
import androidx.test.filters.LargeTest;
import androidx.test.internal.util.Checks;

import org.hamcrest.Matcher;
import org.junit.Rule;
import org.junit.Test;

import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.actionWithAssertions;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.action.ViewActions.scrollTo;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withId;

@LargeTest
public class CustomClickAndSetTimeTest {

    @Rule
    public ActivityScenarioRule activityScenarioRule = new ActivityScenarioRule<>(MainActivity.class);

    @Test
    public void testCustomClickAndSetTime() {
        // 点击按钮并设置自定义时间
        onView(withId(R.id.button))
                .perform(clickWithDelay(2000)); // 设置自定义时间为2秒
    }

    public static ViewAction clickWithDelay(final long delayMillis) {
        Checks.checkNotNull(delayMillis);

        return actionWithAssertions(new ViewAction() {
            @Override
            public Matcher getConstraints() {
                return isDisplayed(); // 如果视图不可见,会抛出异常
            }

            @Override
            public String getDescription() {
                return "Click with custom delay";
            }

            @Override
            public void perform(UiController uiController, View view) {
                view.performClick(); // 点击视图

                uiController.loopMainThreadForAtLeast(delayMillis); // 等待自定义时间
            }
        });
    }
}

在上述示例中,首先使用onView()方法结合withId()方法来定位目标视图(这里假设目标视图的id是R.id.button),然后使用perform()方法搭配clickWithDelay()方法来执行点击操作,并设置自定义时间为2秒。

clickWithDelay()方法是一个自定义的ViewAction,它会在执行点击操作后等待指定的时间。在这个方法中,使用uiController.loopMainThreadForAtLeast()方法来让主线程休眠指定的时间。

请注意,为了使用clickWithDelay()方法,需要添加org.hamcrest.Matcherandroidx.test.ext.junit.rules.ActivityScenarioRule依赖。另外,要确保在build.gradle文件中添加了正确的依赖关系。

相关内容

热门资讯

安装apache-beam==... 出现此错误可能是因为用户的Python版本太低,而apache-beam==2.34.0需要更高的P...
避免在粘贴双引号时向VS 20... 在粘贴双引号时向VS 2022添加反斜杠的问题通常是由于编辑器的自动转义功能引起的。为了避免这个问题...
Android Recycle... 要在Android RecyclerView中实现滑动卡片效果,可以按照以下步骤进行操作:首先,在项...
omi系统和安卓系统哪个好,揭... OMI系统和安卓系统哪个好?这个问题就像是在问“苹果和橘子哪个更甜”,每个人都有自己的答案。今天,我...
原生ios和安卓系统,原生对比... 亲爱的读者们,你是否曾好奇过,为什么你的iPhone和安卓手机在操作体验上有着天壤之别?今天,就让我...
Android - 无法确定任... 这个错误通常发生在Android项目中,表示编译Debug版本的Java代码时出现了依赖关系问题。下...
Android - NDK 预... 在Android NDK的构建过程中,LOCAL_SRC_FILES只能包含一个项目。如果需要在ND...
Akka生成Actor问题 在Akka框架中,可以使用ActorSystem对象生成Actor。但是,当我们在Actor类中尝试...
Agora-RTC-React... 出现这个错误原因是因为在 React 组件中使用,import AgoraRTC from “ago...
安装了Anaconda之后找不... 在安装Anaconda后,如果找不到Jupyter Notebook,可以尝试以下解决方法:检查环境...