Reputation: 1
*Hello! I use Espresso for testing UI. I want to check that after user text a word and click on the button the textView get the correct output.
After ui -testing of my programm:*
@RunWith(AndroidJUnit4.class)
public class MainActivityTest {
private static final String STRING_INPUT_WORD = "Foxminded cool 24/7";
private static final String STRING_INPUT_WORD_CONVERTED = "dednimxoF looc 24/7";
private static final String STRING_IGNORED_SYMBOLS = "0123456789<([{\\^-=$!|]})?*+.>";
@Rule
public ActivityScenarioRule<MainActivity> activityScenarioRule
= new ActivityScenarioRule<>(MainActivity.class);
@Test
public void onCreate() {
// Type text ignored and then press the button ADD
onView(withId(R.id.editTextTextIgnoredСharacters))
.perform(typeText(STRING_IGNORED_SYMBOLS))
.perform(closeSoftKeyboard());
onView(withId(R.id.buttonAddIgnoredСharacters));
// Type text and then press the button CONVERT
onView(withId(R.id.editTextInput))
.perform(typeText(STRING_INPUT_WORD))
.perform(closeSoftKeyboard());
onView(withId(R.id.buttonConvert))
.perform(click());
// Check that the text was changed
onView(withId(R.id.textViewAnagramOfWord))
.check(matches(withText(STRING_INPUT_WORD_CONVERTED)));
}
@Test
public void onSaveInstanceState() {
}
}
I recieve this message:
androidx.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'with error: is "dednimxoF looc 24/7"' doesn't match the selected view.
Expected: with error: is "dednimxoF looc 24/7"
Got: "MaterialTextView{id=2131231105, res-name=textViewAnagramOfWord, visibility=VISIBLE, width=601, height=85, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=androidx.constraintlayout.widget.ConstraintLayout$LayoutParams@7ddd14, tag=null, root-is-layout-requested=false, has-input-connection=false, x=240.0, y=477.0, text=dednimxoF looc 7/42, input-type=0, ime-target=false, has-links=false}"
at dalvik.system.VMStack.getThreadStackTrace(Native Method)
at java.lang.Thread.getStackTrace(Thread.java:1566)
at androidx.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:16)
at androidx.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:36)
at androidx.test.espresso.ViewInteraction.waitForAndHandleInteractionResults(ViewInteraction.java:103)
at androidx.test.espresso.ViewInteraction.check(ViewInteraction.java:31)
at com.example.anagram.MainActivityTest.onCreate(MainActivityTest.java:46)
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.rules.ExternalResource$1.evaluate(ExternalResource.java:48)
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 androidx.test.ext.junit.runners.AndroidJUnit4.run(AndroidJUnit4.java:154)
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 androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:395)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1932)
Caused by: junit.framework.AssertionFailedError: 'with error: is "dednimxoF looc 24/7"' doesn't match the selected view.
Expected: with error: is "dednimxoF looc 24/7"
Got: "MaterialTextView{id=2131231105, res-name=textViewAnagramOfWord, visibility=VISIBLE, width=601, height=85, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=androidx.constraintlayout.widget.ConstraintLayout$LayoutParams@7ddd14, tag=null, root-is-layout-requested=false, has-input-connection=false, x=240.0, y=477.0, text=dednimxoF looc 7/42, input-type=0, ime-target=false, has-links=false}"
at androidx.test.espresso.matcher.ViewMatchers.assertThat(ViewMatchers.java:17)
at androidx.test.espresso.assertion.ViewAssertions$MatchesViewAssertion.check(ViewAssertions.java:15)
at androidx.test.espresso.ViewInteraction$SingleExecutionViewAssertion.check(ViewInteraction.java:10)
at androidx.test.espresso.ViewInteraction$2.call(ViewInteraction.java:11)
at androidx.test.espresso.ViewInteraction$2.call(ViewInteraction.java:2)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Tests ran to completion.
Also when it's running in the emulator I see that it work correct. I can't resolve this problem. Does anyone know what my mistake is?
Upvotes: 0
Views: 672
Reputation: 1300
Yes, becuase, after run your script you have the text on view: "dednimxoF looc 7/42"
But you expect to see "dednimxoF looc 24/7"
Pay attention to the last 4 symbols in the string.
Looks like you need to correct your expected result. because if you revert the string:
"24/7 cool Foxminded".
you will get
"dednimxoF looc 7/42"
But NOT
"dednimxoF looc 24/7"
in other words you forget to revert this part of string "24/7" in STRING_INPUT_WORD_CONVERTED
Upvotes: 1