gpt4 book ai didi

android - 如何向下滚动并单击带有 Espresso 测试的按钮?

转载 作者:行者123 更新时间:2023-11-28 20:22:29 29 4
gpt4 key购买 nike

我有这个应用程序,我正在测试我需要登录的地方,然后它转到另一个 Activity ,然后它转到另一个我可以注销的 Activity 。问题是我的注销按钮在我的 ScrollView 底部,但在屏幕上看不到,espresso 也找不到该按钮。我需要先滚动然后单击按钮。你能告诉我如何滚动然后点击按钮吗?

@Before
public void init() {
// Specify a valid string.
loginText = "something";
passwordText = "something";
}

@Test
public void loginAction() {

mBrowserActivityMonitor = getInstrumentation().addMonitor(MainActivity.class.getName(), null, false);

// Type text and then press the button.
onView(withId(R.id.editText_username))
.perform(typeText(loginText), closeSoftKeyboard());
onView(withId(R.id.editText_password))
.perform(typeText(passwordText), closeSoftKeyboard());
onView(withId(R.id.button_login)).perform(click());

Activity activity = mBrowserActivityMonitor.waitForActivityWithTimeout(10000);

onView(withId(R.id.button_left)).perform(click());

Activity activity2 = mBrowserActivityMonitor.waitForActivityWithTimeout(10000);


onView(withId(R.id.button_logout)).perform(click()); **//does not recognize**

Activity activity3 = mBrowserActivityMonitor.waitForActivityWithTimeout(10000);
assertNotNull(activity3); //To see if activity 2 (Profile) is opened

这是错误日志:

Caused by: java.lang.RuntimeException: Action will not be performed because the target view does not match one or more of the following constraints: at least 90 percent of the view's area is displayed to the user.

最佳答案

onView(withId(R.id.button_logout)).perform(scrollTo(), click());

如果您正在使用的 View 位于 ScrollView(垂直或水平)内,请考虑使用 scrollTo() 来显示需要显示 View 的前面的操作(如 click() 和 typeText())。这确保在继续其他操作之前显示 View :

onView(...).perform(scrollTo(), click());

注意:如果 View 已经显示,则 scrollTo() 将无效,因此您可以在由于屏幕尺寸较大而显示 View 的情况下安全地使用它(例如,当您的测试同时在较小和较大的屏幕上运行时决议)。

您可以查看他们的文档:https://google.github.io/android-testing-support-library/docs/espresso/basics/index.html#performing-an-action-on-a-view

关于android - 如何向下滚动并单击带有 Espresso 测试的按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41567053/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com