试图用浓缩咖啡测试我的登录。如果我只点击我的领域,那么所有的工作都很好。
ViewInteraction linearLayout = onView(
allOf(childAtPosition(
allOf(withId(R.id.nameEditText),
childAtPosition(
withId(R.id.inputLayout),
0)),
0),
isDisplayed()));
linearLayout.check(matches(isDisplayed()))
.perform(click());
Run Code Online (Sandbox Code Playgroud)
但是如果我需要在这个字段中放置一些文本,我会遇到一些问题:
ViewInteraction linearLayout = onView(
allOf(childAtPosition(
allOf(withId(R.id.nameEditText),
childAtPosition(
withId(R.id.inputLayout),
0)),
0),
isDisplayed()));
linearLayout.check(matches(isDisplayed()))
.perform(typeText("SomeName"));
Run Code Online (Sandbox Code Playgroud)
并得到下一个错误:
android.support.test.espresso.PerformException:在视图上执行“type text(SomeName)”时出错(父级中位置 0 的子级(ID:com.fentury.android:id/nameEditText 和父级位置 0 处的子级) id: com.fentury.android:id/inputLayout)
或者
如果我尝试这样做:
ViewInteraction appCompatEditText = onView(
allOf(withId(R.id.nameEditText),
withParent(withId(R.id.inputLayout)), isDisplayed()));
appCompatEditText.check(matches(isDisplayed()))
.perform(click()).perform(typeText("Dog"));
Run Code Online (Sandbox Code Playgroud)
我又犯了同样的错误:
android.support.test.espresso.PerformException:在视图“(id:com.fentury.android:id/nameEditText 并具有父匹配:id:com.fentury.android:id)上执行“输入文本(狗)”时出错/inputLayout 并在屏幕上显示给用户)'
我该如何解决这个错误?
我有一个 ViewPager,它一次只在屏幕上显示一个片段。该片段有一个 recyclerView,其中填充了由一个图像和两个 textView 组成的列表项。我需要选择项目并根据显示的文本执行单击。
我的 viewPage 有 Id pager,recyclerView 是listview,我正在使用 cardView 小部件,它有一个名为的相对布局cardContainer,其中有一个nickNname我感兴趣的 textView 。
Espresso 似乎只能识别pager但在视图层次结构中找不到任何其他 Id。我如何访问nickName或基本上访问viewPager 或片段中的任何其他视图?
我尝试了以下方法:
ViewInteraction listviewInteraction = onView (allOf(withId(R.id.pager)));
listviewInteraction.check(matches(hasDescendant(withId(R.id.listview))));
Run Code Online (Sandbox Code Playgroud)
我收到以下错误,因为寻呼机没有任何孩子
Expected: has descendant: with id: 2131689666
Got: "ViewPager{id=2131689659, res-name=pager, visibility=VISIBLE, width=1080, height=1533, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=144.0, child-count=0}"
Run Code Online (Sandbox Code Playgroud)
我还尝试了几种不同的方法,类似于此处发布的方法,但 Espresso 找不到listview或nickname。
我怎样才能找到视图nickName?
我又运行了一些测试,当 Espresso 运行测试时,viewPager …
android listview android-fragments android-viewpager android-espresso
我想从以下视图层次结构中找到 ID 为“buttonActionNo”的按钮。
我尝试了以下代码,但它不起作用。给出“在层次结构中没有找到匹配的视图:”错误。
**ViewInteraction appCompatImageButton3 = onView(
allOf(withId(R.id.buttonActionNo),
allOf( withhParent(withId(R.id.actionButtonPanel)),
allOf( withParent(withId(R.id.outerContainer)),
allOf( withParent(withId(R.id.questioContainer)),
withParent(withId(R.id.redFlagQuestion1))))),
isDisplayed()));
appCompatImageButton3.perform(click());**
Run Code Online (Sandbox Code Playgroud)
有没有人试图抓住一个几乎没有嵌套父级的视图?
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- redFlagQuestion1 -->
<FrameLayout android:id="@+id/questionOneContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">
<include
android:id="@+id/redFlagQuestion1"
layout="@layout/row_base" />
</FrameLayout>
<!-- redFlagQuestion 2 -->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">
<include
android:id="@+id/fillerLayout2"
layout="@layout/fill_space" />
<include
android:id="@+id/redFlagQuestion2"
layout="@layout/row_base" />
</FrameLayout>
<!-- redFlagQuestion 3 -->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">
<include
android:id="@+id/fillerLayout3"
layout="@layout/fill_space" />
<include
android:id="@+id/redFlagQuestion3"
layout="@layout/row_base" />
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
row_base.xml 视图层次结构如下:
+----------->LinearLayout{id=2131492990, res-name=redFlagQuestion1,visibility=VISIBLE, width=1080, height=780, has-focus=false, has-focusable=true, has- window-focus=true, …
我试图点击我的应用程序中匹配文本的第一个元素。但是目前我收到一个错误,告诉我由于我当前的代码行有多个匹配项。
onView(allOf(withId(R.id.offerSummaryLayout))).perform(RecyclerViewActions.actionOnItem(Matchers.allOf(hasDescendant(withText("Online sale"))), click()));
我怎样才能改变它,让它点击第一个匹配的元素?提前致谢
我刚刚开始研究 Espresso 测试,但我搜索了 throw StackOverFlow 主题,但找不到满足我需求的解决方案。
我的问题的史前史:
我有很多活动的 android 应用程序。通常为应用程序的不同流创建单独的 UI 测试(每个流都需要单独的活动,所以这就是我在不同的类中实现测试的原因)。在我的情况下,背景和 UI usync 发生,这就是为什么我不能从第一个活动开始并继续进行下一个和下一个。我找到的唯一解决方案并且它确实适用于当前的应用程序 - 将空闲资源用于应用程序活动。
问题:所以 现在我想了解如何将所有这些单独的类(每个类使用自己的规则和这些规则的活动)放到功能套件中?
我的经验:
1)正面- 使用需要更改开发人员源代码的空闲资源,但套件中仍然只有一项活动:
@RunWith(AndroidJUnit4.class
public class forSeveralActs {
@Rule
public final ActivityTestRule<TargetedActivity> mTargetedActivityRule = new ActivityTestRule<>(TargetedActivity.class, true, true);`
final String username = "test@mail.com";
final String password = "12345678";
private IdlingResource mIdlingRecourse;
@Before
public void setIdleResource() {
mIdlingRecourse = mTargetedActivityRule.getActivity().getIdlingResource();
Espresso.registerIdlingResources(mIdlingRecourse);
}
@Test
public void PassPermission_and_goto_MainActivity() {
// First activity - LoginActivity
Espresso.onView(withId(R.id.activity_login_et_login))
.perform(ViewActions.typeText(username));
Espresso.onView(withId(R.id.activity_login_et_password))
.perform(ViewActions.typeText(password))
.perform(closeSoftKeyboard());
Espresso.onView(withId(R.id.activity_login_btn_login))
.check(matches(isDisplayed()))
.check(matches(isClickable()))
.perform(ViewActions.click()); …Run Code Online (Sandbox Code Playgroud) 我有一个非常简单的测试类,用于在 Android 上运行 espresso 测试,如下所示:
import android.util.Log;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.rules.ExternalResource;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import java.io.IOException;
@RunWith(JUnit4.class)
public class Wtf {
private static class TestResources extends ExternalResource {
protected void before() {
println("before() TestResources");
}
protected void after() {
println("after() TestResources");
}
}
@ClassRule
public static final TestResources res = new TestResources();
@BeforeClass
public static void setUpClass() {
println("@BeforeClass setUpClass()");
}
@AfterClass
public static void tearDownClass() throws IOException { …Run Code Online (Sandbox Code Playgroud) 我正在用浓缩咖啡测试一个应用程序。第一个测试用例将第一个活动的数据从网络加载到本地数据库中。第二个测试用例 / 2. Activity 显示来自数据库的数据。
这就是为什么测试用例 1 需要在测试用例 2 之前运行。但这并不总是发生,浓缩咖啡偶尔会改变顺序。我该如何解决问题?我可以设置测试用例(Testclasses)的顺序吗?
我在浓缩咖啡中编写了一个测试用例,需要执行删除(退格)。我想出来的代码是这样的
onView(withId(R.id.testNumberEditText))
.perform(replaceText("123"))
.perform(pressKey(KeyEvent.KEYCODE_DEL))
.check { view, _ ->
assertEquals("12", (view as EditText).text.toString())
}
Run Code Online (Sandbox Code Playgroud)
但是这段代码不起作用,文本仍然是123. 知道我应该按哪个键来获得所需的结果我做错了什么吗?
我搜索了很多关于如何将 ViewModel 注入测试以便我可以测试它的方法。假设视图模型有一个带有一些业务逻辑交互器的构造函数注入。我可以很容易地将它注入到片段中,但在测试中没有成功。
@HiltAndroidTest
class ViewModelTest
val randomViewmodel: RandomViewmodel// now what ? since by viewModels() is not accessible in tests
@Test
fun viewModelTet() {
randomViewmodel.triggerAction()
assertEquals(RandomVIewState(1), randomViewmodel.getState())
}
Run Code Online (Sandbox Code Playgroud)
我试图在测试类中实现 byViewModels() 并且可以在没有构造函数参数的情况下注入视图模型,但没有成功。
class RandomViewmodel @ViewModelInject constructor(
private val randomInteractor: RandomInteractor
) : ViewModel
Run Code Online (Sandbox Code Playgroud)
Caused by: java.lang.InstantiationException: class app.RandomViewModel has no zero argument constructor
Run Code Online (Sandbox Code Playgroud)
原因:我希望能够完全测试我的屏幕逻辑,因为 viewModel 将处理对交互器等的依赖。在各种数据流动的背后可能有很多逻辑。测试片段很可能是可能的,但在具有大量测试的较大项目中会较慢。
我已经阅读了https://developer.android.com/jetpack/guide#test-components,它建议进行 JUnit 测试并模拟 viewModel 中的依赖项,但是您必须单独为每个依赖项创建测试并且无法真正测试整个屏幕的逻辑