我最近开始使用 Espresso 并用它来测试我开发的一些应用程序,它的效果非常好。现在,作为工作的一部分,我应该自动化第 3 方应用程序,例如,适用于 Android 的 imdb 应用程序,具有以下示例场景 - (先决条件是我已经下载了 imdb apk 文件并将该应用程序安装在目标手机)
1) 启动 imdb 应用程序(从已安装该应用程序的手机) 2) 使用我之前手动创建的帐户凭据(即测试人员)登录该应用程序 3) 执行一些操作(例如搜索对于电影、演员等)4)注销
根据我对 espresso 的了解,我认为它只是为开发人员设计的,用于测试他们自己的应用程序,而不是第三方应用程序(如果我错了,请纠正我!)。另外,我也无法在网上找到有用的材料。在这方面的任何帮助将不胜感激!
我认为这是一个错误,但我可能做错了什么。
我的布局非常简单,只包含一个浮动操作按钮:
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/contentCoordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/layoutWithoutContent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/stopButton"
android:src="@drawable/ic_stop_white_24px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
app:layout_anchor="@id/layoutWithoutContent"
app:backgroundTint="@color/colorPrimary"
app:layout_anchorGravity="bottom|right|end"/>
Run Code Online (Sandbox Code Playgroud)
我的活动只继承了包含以下内容的 onCreate 方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CoordinatorLayout coordinatorLayout = findViewById(R.id.contentCoordinatorLayout);
Snackbar.make(coordinatorLayout, R.string.snackbar_message, Snackbar.LENGTH_LONG).show();
}
Run Code Online (Sandbox Code Playgroud)
我写了一个 Espresso 测试,它只验证 Snackbar 在半秒内显示
@Rule
public ActivityTestRule<MainActivity> mainActivity = new ActivityTestRule<>(MainActivity.class, true, true);
@Test
public void testSnackbarIsShown() throws Exception { onView(withText(R.string.snackbar_message)).check(matches(isDisplayed()));
}
Run Code Online (Sandbox Code Playgroud)
当我调试这个测试时,onView(withText(R.string.snackbar_message)).check(matches(isDisplayed()));是在snackbar消失后执行的。
到目前为止,我找到了 3 种方法来解决此问题,问题在以下情况下得到解决:
我将 FloatingActionButton 的 anchorGravity …
android android-espresso floating-action-button coordinator-layout
我目前是自动化测试的新手,并使用 Android Studio 使用 Espresso 执行自动化测试,我正在尝试对登录屏幕执行自动化测试,我目前在执行特定按钮的单击时遇到问题。我尝试了多种方法按钮对我不起作用。我指的是下面的文档来实现,下面是我的示例代码和崩溃报告。
@RunWith(AndroidJUnit4.class)
@LargeTest
public class LoginTest {
@Rule
public ActivityTestRule<LoginActivity> mActivityRule =
new ActivityTestRule<>(LoginActivity.class);
@Test
public void OnLoginButtonClick() throws InterruptedException {
onView(ViewMatchers.withId(R.id.edtUserName)).perform(ViewActions.typeText("xxxxx"));
onView(ViewMatchers.withId(R.id.edtPassword)).perform(ViewActions.typeText("xxxxxxx"));
onView(ViewMatchers.withId(R.id.btnLogin)).perform(ViewActions.scrollTo(),ViewActions.click());
}
}
Run Code Online (Sandbox Code Playgroud)
android.support.test.espresso.PerformException: Error performing 'scroll to' on view 'Animations or transitions are enabled on the target device.
with id: xxx.xxxx.xxx.xxxx:id/btnLogin'.
at android.support.test.espresso.PerformException$Builder.build(PerformException.java:83)
at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:80)
at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:56)
at android.support.test.espresso.ViewInteraction.runSynchronouslyOnUiThread(ViewInteraction.java:184)
at android.support.test.espresso.ViewInteraction.doPerform(ViewInteraction.java:115)
at android.support.test.espresso.ViewInteraction.perform(ViewInteraction.java:87)
at com.msf.opx.LoginTest.OnLoginButtonClick(LoginTest.java:50)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at android.support.test.internal.statement.UiThreadStatement.evaluate(UiThreadStatement.java:55) …Run Code Online (Sandbox Code Playgroud) 如何使用 Espresso 在 Android 软键上按下 Editor Action 键?我试过:
onView(withId(R.id.edToNumber)).perform(typeText("MDO158"),ViewActions.pressKey(KeyEvent.ACTION_UP), closeSoftKeyboard())
Run Code Online (Sandbox Code Playgroud)
我有检查带有文本“产品”的行是否存在于RecyclerView以下代码中:
onView(withId(R.id.rv_list)).perform(scrollTo(hasDescendant(withText("Product"))));
onView(withItemText("Product")).check(matches(isDisplayed()));
public static Matcher<View> withItemText(final String itemText) {
checkArgument(!TextUtils.isEmpty(itemText), "itemText cannot be null or empty");
return new TypeSafeMatcher<View>() {
@Override
public boolean matchesSafely(View item) {
return allOf(
isDescendantOfA(isAssignableFrom(RecyclerView.class)),
withText(itemText)).matches(item);
}
@Override
public void describeTo(Description description) {
description.appendText("is isDescendantOfA RV with text " + itemText);
}
};
}
Run Code Online (Sandbox Code Playgroud)
如何检查所有列表中是否没有包含提供文本的行RecylerView?
我有一项活动需要有意图才能开始。我正在尝试运行一个 UI 工具测试,该测试在开始活动之前初始化意图,但我的测试失败并出现以下 java.lang.RuntimeException。
我究竟做错了什么?我在这里找到了一个例子,但我找不到任何区别......
谢谢!
java.lang.RuntimeException: Could not launch activity
at android.support.test.runner.MonitoringInstrumentation.startActivitySync(MonitoringInstrumentation.java:460)
at android.support.test.rule.ActivityTestRule.launchActivity(ActivityTestRule.java:354)
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:525)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at android.support.test.runner.AndroidJUnit4.run(AndroidJUnit4.java:101)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:384)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2074)
Caused by: java.lang.RuntimeException: Unable to resolve activity for: Intent { flg=0x14000000 …Run Code Online (Sandbox Code Playgroud) 我正在使用导航组件,并且正在尝试Fragment使用仪器测试来测试我的组件。该片段具有onViewCreated由扩展函数在方法中初始化的自定义工具栏。
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
tbBlack.init()
}
fun androidx.appcompat.widget.Toolbar.init(
menuId: Int? = null
) {
title = ""
menuId?.let {
inflateMenu(it)
}
findNavController().let {
it.graph.let { graph ->
val configuration = AppBarConfiguration(graph)
setupWithNavController(it, configuration)
}
}
}
Run Code Online (Sandbox Code Playgroud)
在我的仪器测试中的场景初始化期间,由于null导航控制器上的图形导致测试崩溃。
导航控制器在测试中被模拟,以及如下图:
@RunWith(AndroidJUnit4::class)
class LoginFragmentTest {
@Test
fun testEmptyFields() {
val mockNavController = mock(NavController::class.java)
val mockGraph = mock(NavGraph::class.java)
mockNavController.graph = mockGraph
val scenario = launchFragmentInContainer(themeResId = R.style.AppTheme) {
LoginFragment().also { fragment -> …Run Code Online (Sandbox Code Playgroud) android android-testing android-espresso android-architecture-navigation
我是一名 Android 开发人员,面临着一个非常严重的问题,但在任何地方都没有提到解决方案。
我正在尝试在我的 oneplus6t 上运行 Expresso 自动化测试。但是一年中的每一次我都无法测试。
错误信息是这样的:
"Testing started at 09:46 AM ...
04/20 09:46:21: Launching 'ASyncSettingsTest' on OnePlus ONEPLUS A6010.
Running tests
$ adb shell am instrument -w -r -e debug false -e class 'com.declaree.declaree.ASyncSettingsTest' com.demo.test/androidx.test.runner.AndroidJUnitRunner
Timed out waiting for process (com.demo) to appear on oneplus-oneplus_a6010-21663687."
Run Code Online (Sandbox Code Playgroud)
它适用于模拟器和其他制造设备,如像素、moto、三星。
我在开发人员选项中也没有找到类似的东西。
为了运行测试,我必须自己打开应用程序然后它才能运行。
仅供参考:重启、重置、擦除缓存不起作用。
我在 Android Studio 3.6.2 中工作
有人有任何解决方案吗?提前致谢。
如果我正常运行该应用程序,该应用程序工作正常,但是当我尝试测试时,它显示错误,这是错误的完整日志。
internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 由:java.lang.IllegalArgumentException:此组件上的样式需要你的应用主题是 Theme.AppCompat(或一个后代)。在 com.google.android.material.internal.ThemeEnforcement.checkTheme(ThemeEnforcement.java:243) 在 com.google.android.material.internal.ThemeEnforcement.checkAppCompatTheme(ThemeEnforcement.java:213) 在 com.google.android.material .internal.ThemeEnforcement.checkCompatibleTheme(ThemeEnforcement.java:148) 在 com.google.android.material.internal.ThemeEnforcement.obtainStyledAttributes(ThemeEnforcement.java:76) 在 com.google.android.material.floatingactionbutton.FloatingActionButton.(FloatingActionButton. java:211) 在 com.google.android.material.floatingactionbutton。
这是我的 XML 文件
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="adapter"
type="com.medium.todoapp.adapters.ToDoListAdapter" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:adapter="@{adapter}"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:listitem="@layout/item_todo" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
Run Code Online (Sandbox Code Playgroud)
这是我的测试文件
@RunWith(AndroidJUnit4::class)
class HomeFragmentInstrumentedTest {
@Test
fun testRecyclerView() {
val homeScenario = launchInContainer(HomeFragment::class.java)
homeScenario.onFragment {
Assert.assertNotEquals(it.view?.findViewById(R.id.recyclerView), null)
}
}
@Test
fun testAddTodoButton() {
val …Run Code Online (Sandbox Code Playgroud) 我想为 Espresso 创建一个自定义的正则表达式匹配器,以便我可以检查屏幕上的文本是否包含时间格式HH:mm,例如23:34或04:23
我有一个正则表达式匹配器类:
class RegexMatcher(private val regex: String) :
BoundedMatcher<View, TextView>(TextView::class.java) {
private val pattern = Pattern.compile(regex)
override fun describeTo(description: Description?) {
description?.appendText("Checking the matcher on received view: with pattern=$regex")
}
override fun matchesSafely(item: TextView?) =
item?.text?.let {
pattern.matcher(it).matches()
} ?: false
}
Run Code Online (Sandbox Code Playgroud)
还有一个功能:
private fun withPattern(regex: String): Matcher<in View>? = RegexMatcher(regex)
Run Code Online (Sandbox Code Playgroud)
屏幕上的文字说:Sometext 08:23时间当然是动态的
我的 UI 检查是这样写的:
onView(withText(startsWith("Sometext"))).check(matches(withPattern("/(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/")))
Run Code Online (Sandbox Code Playgroud)
但是测试总是失败,我不知道为什么。即使我只是使用一些简单的东西,/^Sometext但它会失败。谁能帮我?
android-espresso ×10
android ×9
kotlin ×2
android-architecture-navigation ×1
junit ×1
oneplus6t ×1
regex ×1
testing ×1
uitest ×1
unit-testing ×1