有什么区别
1.ViewInteraction v = onView(allOf(withId(R.id.login_card_view), isDisplayed()))
和
2.v.check(matches(isDisplayed()))
isDisplayed()如果我在 2 中做同样的事情,那么 1有什么用呢?
我有多个在 for 循环中动态创建的按钮,并且没有 ID。我需要对这些按钮进行自动化测试。我怎样才能用浓缩咖啡做到这一点?这是创建各种按钮的 for 循环:
for (int i = 0; i < numberOfSamples; i++) {
TableRow.LayoutParams vl = new TableRow.LayoutParams(30,30);
Button b = new Button(context);
b.setId(((blockNumber * 10000)+i));
//b.setHint(i);
//b.setHint(blockNumber);
// double selectedGrade = 0;
// if(FinalSurvey.multi[blockNumber][i] != 0){
// selectedGrade = FinalSurvey.multi[blockNumber][i];
// }
// final int makeHighlight = selectedGrade;
b.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.a));
b.setAlpha(1.0f);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ViewsAdding va = new ViewsAdding();
va.showGrades(context, v, grades);
view = v;
maincontext = context;
}
});
if(i%5 …Run Code Online (Sandbox Code Playgroud) 我在 Circle ci 中运行 UI 测试时遇到超时问题,因为命令connectedAndroidTest 的运行时间超过 10 分钟。
所以我试图将它们分成测试套件并一次运行每个套件。
我在这里找到了如何为我的 android 测试创建套件:https://developer.android.com/reference/junit/framework/TestSuite.html
但我找不到如何使用connectedAndroidTest 命令运行它们。
我有一个像这样的浓缩咖啡测试套件类
package instrumentedtest;
import org.junit.ClassRule;
import org.junit.rules.ExternalResource;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({
Test1.class,
Test2.class,
Test3.class
})
public class POSRuleSuite {
@ClassRule
public static ExternalResource testRule = new ExternalResource() {
@Override
protected void before() throws Throwable {
System.out.println("Testing starts.........");
}
@Override
protected void after() {
System.out.println("Testing ends.........");
}
};
}
Run Code Online (Sandbox Code Playgroud)
我在 Android Studio 中使用此套件类设置了 Firebase 测试。我从 Android Studio 启动了这个 firebase 测试,它确实有效。
但是当我使用 Gcloud 命令从命令行启动它时,我未能执行测试。
gcloud firebase test android run ^
--type instrumentation ^
--app POS.apk ^
--test POS-debug-androidTest.apk ^
--test-runner-class=org.junit.runners.Suite …Run Code Online (Sandbox Code Playgroud) 谷歌解释了如何控制 Android 模拟器的许多参数,例如电池的当前电量、传感器输入等:https : //developer.android.com/studio/run/emulator-console.html。
我想创建一个 Espresso 测试,在测试期间更改模拟器的这些参数,这可能吗?我知道有以下方法:
InstrumentationRegistry.getInstrumentation().uiAutomation
.executeShellCommand("someShellCommand")
Run Code Online (Sandbox Code Playgroud)
可以在测试期间调用此方法,但要更改仿真器的系统参数,我首先必须通过登录telnet localhost 5554,然后才能操作仿真器参数,例如:power capacity 30,将电池电量更改为 30% . 我如何在 Espresso 测试期间做到这一点?
Windows 10(64 位)、Android Studio 3.1.2、Gradle 4.4、Java 1.8。
这是我的布局 xml
<TextView
android:id="@+id/loginTextView"
android:layout_width="255dp"
android:layout_height="60dp"
android:layout_marginBottom="15dp"
android:background="@drawable/sign_in_login_bg"
android:gravity="center"
app:layout_constraintBottom_toTopOf="@+id/registerTextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
Run Code Online (Sandbox Code Playgroud)
这里@drawable/sign_in_login_bg
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/color_primary" />
<corners android:radius="@dimen/text_view_rounded_corner_radius" />
</shape>
Run Code Online (Sandbox Code Playgroud)
我想编写浓缩咖啡测试来检查loginTextView是否具有背景@drawable/sign_in_login_bg
所以我写了自定义匹配器:
import org.hamcrest.Matcher;
public static Matcher<View> withBackground(final int expectedResourceId) {
return new BoundedMatcher<View, View>(View.class) {
@Override
public boolean matchesSafely(View view) {
return sameBitmap(view.getContext(), view.getBackground(), expectedResourceId);
}
@Override
public void describeTo(Description description) {
description.appendText("has background resource " + expectedResourceId);
}
};
} …Run Code Online (Sandbox Code Playgroud) 我对 Espresso 很陌生,但我正在尝试测试一个相对简单的 Activity。我的 android 应用程序有自己的自定义 Application 类。我如何告诉 Espresso 使用此类的模拟(或自定义)版本?
这是我的应用程序的自定义版本。它创建了一些测试数据(为简洁起见在此处进行了编辑)。接下来,我还将覆盖一些方法。
public class MockMyApplication extends MyApplication {
@Override
public void onCreate() {
super.onCreate();
// create some location data for testing
DataRecord rec = new DataRecord(1);
rec.setName("TestLoc1");
rec.setDescription("an important customer");
MyData.add(rec);
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用它进行测试,如下所示:
@RunWith(AndroidJUnit4.class)
@LargeTest
public class LocEditActivityTest extends AndroidJUnitRunner {
@Rule
public ActivityTestRule<LocEditActivity> activityTestRule
= new ActivityTestRule<>(LocEditActivity.class);
@Override
public Application newApplication(ClassLoader cl, String className, Context context) throws IllegalAccessException, ClassNotFoundException, InstantiationException {
return super.newApplication(cl, MockMyApplication.class.getName(), context);
}
@Test
public void …Run Code Online (Sandbox Code Playgroud) android unit-testing android-espresso android-application-class
我正在尝试测试我的 recyclerView 并且我正在使用材料卡视图进行项目显示,虽然该应用程序运行良好,但当我尝试测试时出现此错误:
android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class com.google.android.material.card.MaterialCardView
Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class com.google.android.material.card.MaterialCardView
Caused by: java.lang.reflect.InvocationTargetException
...
Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant).
Run Code Online (Sandbox Code Playgroud)
现在测试就这么简单:
@Test
fun shouldShowList() {
launchFragmentInContainer<PostsFragment>()
Thread.sleep(5000)
}
Run Code Online (Sandbox Code Playgroud)
睡眠仅用于应用程序等待尝试显示列表。奇怪的是,当我的列表项布局不使用 materialCardView 时,测试通过了。现在我已将我的应用主题更改为:
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
Run Code Online (Sandbox Code Playgroud)
但测试仍然消失,那么如何更改我的应用程序的测试主题?
我正在关注这个关于 Material Components 的代码实验室,在完成它之后,我想我应该练习编写一些仪器测试。该应用程序运行良好。但是当我运行任何测试时,我不断收到此错误:
java.lang.RuntimeException: android.view.InflateException: Binary XML file line #37: Binary
XML file line #37: Error inflating class
com.google.android.material.textfield.TextInputLayout
at androidx.test.runner.MonitoringInstrumentation.runOnMainSync(MonitoringInstrumentation.java:450)
at androidx.test.core.app.ActivityScenario.onActivity(ActivityScenario.java:564)
at androidx.fragment.app.testing.FragmentScenario.internalLaunch(FragmentScenario.java:300)
at androidx.fragment.app.testing.FragmentScenario.launchInContainer(FragmentScenario.java:282)
at com.google.codelabs.mdc.kotlin.shrine.LoginFragmentTest.username_and_password_editTexts_visible(LoginFragmentTest.kt:64)
at java.lang.reflect.Method.invoke(Native Method)
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 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 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 …Run Code Online (Sandbox Code Playgroud) junit android android-fragments android-espresso android-instrumentation
我正在尝试使用浓缩咖啡和导航组件验证传递给我的新片段的数据。我从这里拿了一个例子来简化这个问题。
@Test
fun testNavigationToInGameScreen() {
// Create a TestNavHostController
val navController = TestNavHostController(
ApplicationProvider.getApplicationContext())
navController.setGraph(R.navigation.trivia)
// Create a graphical FragmentScenario for the TitleScreen
val titleScenario = launchFragmentInContainer<TitleScreen>()
// Set the NavController property on the fragment
titleScenario.onFragment { fragment ->
Navigation.setViewNavController(fragment.requireView(), navController)
}
// Verify that performing a click changes the NavController’s state
onView(ViewMatchers.withId(R.id.play_btn)).perform(ViewActions.click())
assertThat(navController.currentDestination?.id).isEqualTo(R.id.in_game)
// HERE IS WHERE I'M TRYING TO VALIDATE ARGUMENTS
val currentDestinationArgs = navController.currentDestination.arguments
val expectedArguments = bundleOf(ARG_A to true)
assertEquals(currentDestinationArgs, expectedArguments)
}
Run Code Online (Sandbox Code Playgroud)
我不知道如何转换currentDestinationArgs …
android-espresso ×10
android ×9
casting ×1
circleci ×1
firebase ×1
gcloud ×1
hamcrest ×1
junit ×1
navigation ×1
testing ×1
unit-testing ×1