标签:ui-testing android android-espresso
这是我的意式浓缩咖啡的测试:
@RunWith(AndroidJUnit4::class)
class AddTraderActivityNetworkTest {
private val context = InstrumentationRegistry.getInstrumentation().getContext()
private lateinit var mockServer: MockWebServer
@Rule
@JvmField
var addTraderIntentTestRule: IntentsTestRule<AddTraderActivity> = IntentsTestRule(AddTraderActivity::class.java)
@Before
fun setup() {
mockServer = MockWebServer()
mockServer.start(8081)
}
@Test
fun buttonStart_click_clientError_showToast() {
mockServer.enqueue(MockResponse()
.setResponseCode(400))
onView(withId(R.id.baseTextInputEditText))
.perform(typeText(BASE_TEST))
onView(withId(R.id.quoteTextInputEditText))
.perform(typeText(QUOTE_TEST))
onView(withId(R.id.startButton))
.perform(click())
onView(withText(R.string.client_error)).inRoot(ToastMatcher())
.check(matches(isDisplayed()))
}
@Test
fun buttonStart_click_clientError_traderNotFound_showToast() {
mockServer.enqueue(MockResponse()
.setResponseCode(404)
.setBody(FileUtil.getStringFromFile(context, "trader_404_not_add.json")))
onView(withId(R.id.baseTextInputEditText))
.perform(typeText(BASE_TEST))
onView(withId(R.id.quoteTextInputEditText))
.perform(typeText(QUOTE_TEST))
onView(withId(R.id.startButton))
.perform(click())
onView(withText(R.string.trader_not_added)).inRoot(ToastMatcher())
.check(matches(isDisplayed()))
}
当统计所有这些测试时,其中之一肯定会失败并显示以下消息:
androidx.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with string from resource id: <2131623975>[client_error] value: Client Error
View Hierarchy:
+>LinearLayout{id=-1, visibility=VISIBLE, width=566, height=149, has-focus=false, has-focusable=false, has-window-focus=false, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=WM.LayoutParams{(0,192)(wrapxwrap) gr=#51 ty=2005 fl=#1000098 fmt=-3 wanim=0x1030004}, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}
|
+->AppCompatTextView{id=16908299, res-name=message, visibility=VISIBLE, width=416, height=83, has-focus=false, has-focusable=false, has-window-focus=false, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@b08322e, tag=null, root-is-layout-requested=false, has-input-connection=false, x=75.0, y=33.0, text=Trader not added, input-type=0, ime-target=false, has-links=false}
|
at dalvik.system.VMStack.getThreadStackTrace(Native Method)
at java.lang.Thread.getStackTrace(Thread.java:580)
at androidx.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:96)
at androidx.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:59)
at androidx.test.espresso.ViewInteraction.waitForAndHandleInteractionResults(ViewInteraction.java:316)
at androidx.test.espresso.ViewInteraction.check(ViewInteraction.java:300)
at com.myproject.activity.AddTraderActivityNetworkTest.buttonStart_click_clientError_showToast(AddTraderActivityNetworkTest.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 androidx.test.internal.runner.junit4.statement.RunBefores.evaluate(RunBefores.java:80)
at androidx.test.internal.runner.junit4.statement.RunAfters.evaluate(RunAfters.java:61)
at androidx.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:531)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
发生这种情况是因为开始了新的测试,但是仍然没有隐藏上一测试的吐司.结果我得到了找不到消息的吐司的错误:
androidx.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with string from resource id: <2131623975>[client_error] value: Client Error
问题是:
我如何才能等到烤面包被藏起来并且只有在开始第二次测试之后?
解决方法:
这是Java中的一个函数,用于显示x毫秒的Toast
private Toast myToast;
private void showToast(int ms) {
myToast = Toast.makeText(this, "Your message", Toast.LENGTH_LONG);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
myToast.cancel(); // do this to be sure Toast is gone
// here you can start your new test or set a flag to say Toast is finish
}
}, ms);
}
标签:ui-testing,android,android-espresso 来源: https://codeday.me/bug/20191010/1886041.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。