I have a viewpager and I want to check for an imageview in it. I have used following code but its not working.
onView(allOf(withId(R.id.img_flag), hasSibling(withText("some
text"))));
this one always returns true
onView(withId(R.id.img_flag)).check(doesNotExist());
this one throws AmbiguousViewMatcherException
I don't know what you're going to check, but to resume:
The first one is always true, because img_flag would be certainly always (I mean: until you put it in another ViewGroup) a sibling to a TextView with some text. It's like mathematical 2+2=4.
So this test only checked if img_flag has sibling with "some text".
AmbiguousViewMatcherException means only that there are more than one view with img_flag id. Espresso don't know which of them shoul not exist so it informs about it.
If you wanna fix the second one please read about onData() Espresso matcher.
Visit also these sites:
https://guides.codepath.com/android/UI-Testing-with-Espresso
https://google.github.io/android-testing-support-library/docs/espresso/index.html
On the second one you would find Espresso Cheat Sheet, where struct of onData() matcher is enough explained
If you have more quesrtions, please feel free to ask.
Related
I'm using UIAutomator test framework for long tests (concerning to my acceptance test). And I need to wait until some activity is started.
I decided to use By.clazz (android.support.test.uiautomator package) methods to find activity object. I expected that something like
uiDevice.wait(Until.findObject(By.clazz(SomeActivity.class)), 30000);
will work. But it doesn't. I suppose that object of my activity cannot be found. I tried to use other By.clazz methods with different params but without success.
So, my code is pretty simple:
UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
/*.... do something...
like click on buttons which will open some activities...
*/
//does not work, time value just for sample
uiDevice.wait(Until.findObject(By.clazz(SomeActivity.class)), 30000);
I found workaround solution with using By.res, like
uiDevice.wait(Until.findObject(By.res(BASIC_PACKAGE, "someMainIdInSomeFragment")), 30000);
But I have very complicated structure of the app with base activities and so on. I often have the same layout for different activities with load different fragments. So I need to know that we started exactly SomeActivity ,regardless of loaded fragments.
So, the questions are:
Is it possible to use By.clazz for Activity to find its object?
Is there some another way to find activity object with UIAutomator?
Did I do everything right? Or maybe there are some mistakes? Is it possÑ–ble to do with UiAutomator?
Thanks!
Using class with UiObject2
Find the EditText, make sure to click on it (legacySetText would do it implicitly), and set your text.
val input = By.clazz(EditText::class.java.canonicalName)
device.wait(Until.findObject(input), TIMEOUT).apply {
click()
text = "your_text"
}
Yes, could be done through the id.
// android:id="#+id/widget_id"
By.res(applicationId, "widget_id")
Your syntax seems good to me. Just make sure no spinner (or any other widget) is blocking your view during the click attempt.
I'm writing an Android program I put a ImageView on screen and then remove it but another part of the code is still trying to detect the ImageView after it has been removed. How do I write an if statement that will allow me to detect if the imageView has been removed? I've tried a few things but get no result
if (arrowObj000.get(6).findViewWithTag(arrowObj000.get(6))!=null) {
}
arrowObj00 is a list object/array holding refrences to the ImageView this isn't the problem though
Problem is I need to write an if statement that detects if the ImageView is currently attached.
Walk up the chain of parents, comparing each to the root view of the activity/fragment. If you hit the root, you're attached. If you find a null first, you aren't.
Although a better way to handle this would probably to store the state information somewhere and not use your view as state.
After thoroughly looking through the code hints I found the answer
if (arrowObj000.get(6).isAttachedToWindow()) {
}
I have this simple Espresso interaction:
onView(atIndex(withId(R.id.editTextTextWidget), 0)).inRoot(authViewRootMatcher)
.check(matches(allOf(isDisplayed(), isEnabled())))
.perform(typeText("1"));
The check(matches(allOf(isDisplayed(), isEnabled()))) passes as expected, but the following perform(typeText("1")) does not. I cannot figure out why, for the life of me.
So, I can't believe I'm asking this, but how in the name of Android do I use Espresso to type text into my EditText whose ID is R.id.editTextTextWidget?
I fixed the problem by splitting the check(...) call and perform(...) call:
onView(atIndex(withId(R.id.editTextTextWidget), 0)).inRoot(authViewRootMatcher)
.check(matches(allOf(isDisplayed(), isEnabled())));
onView(atIndex(withId(R.id.editTextTextWidget), 0)).inRoot(authViewRootMatcher)
.perform(typeText("1"));
For some reason this works, and the original doesn't. #GooglePlz
I am very new to TDD and doing TFD in particular. I have not written any codes yet and I wanted to write a test first before develop everything in conjunction with TDD. I want your insight. It seems I am copy pasting my test code. I have made my 'pseudo' user story for me to practice. I am going to paraphrase as this is an actual personal project so please bear with me.
"User can search for a tag"
I have a UI that allows to add and to search for a tag. I made it a little bit conservative by using minimal design. I have a button which toggles between add/search string. I have a CardView to represent this, that CardView is part of the list (its like Facebook). Now I wanted to test that when the user press the button, the content on that card will change to search mode. I pretty much have an idea on how to do this but copy pasting my test code per each test is kind of bothering me.
Here is my test:
public class TagListActivityTest
{
#Test
public void shouldHaveAddTagCard()
{
// User tapped to expand the card
onView(withId(R.id.edittext_description_minimized))
.perform(click());
// User sees the expanded card
onView(withId(R.id.linearlayout_add_note_maximize))
.check(matches(isDisplayed()));
// User sees the expanded card's quick action buttons
onView(withId(R.id.relativelayout_quick_action_button))
.check(matches(isDisplayed()));
// User clicks the add tag button
onView(withId(R.id.imagebutton_tag))
.perform(click());
// User sees the tag list
onView(withId(R.id.coordinatorlayout_tag_list))
.check(matches(isDisplayed()));
// User sees the add tag card
onView(withId(R.id.cardview_add_tag))
.check(matches(isDisplayed()));
}
#Test
public void shouldToggleToSearch()
{
// I am going to do the exact same thing as shouldHaveAddTagCard
// starting from my parent activity until here...
onView(withId(R.id.edittext_description_minimized))
.perform(click());
onView(withId(R.id.linearlayout_add_note_maximize))
.check(matches(isDisplayed()));
onView(withId(R.id.relativelayout_quick_action_button))
.check(matches(isDisplayed()));
onView(withId(R.id.imagebutton_tag))
.perform(click());
onView(withId(R.id.coordinatorlayout_tag_list))
.check(matches(isDisplayed()));
}
}
The TagListActivity is originating from a parent activity. There is some bunch of things you have to do before you can go through the TagListActivity and I already have written test for it. So when I test TagListActivity I have to go first in application's homescreen and navigate from there as you can see from my test procedure shouldHaveAddTagCard. This is my problem, I have to write that procedure over and over again. So when I wanted to test shouldToggleSearch I have to go from the parent activity and write those tests again until I reached TagListActivity. I think I am doing something wrong.
So my question is:
How can I organize this when there is a known user action procedure.
I have written test per procedure to make sure it does what I wanted
to be.
no. 1 makes me feel there is something wrong in what I am doing. I am testing per action (ie user adds tag, user search tag, user deletes tag). So the pre-procedure
I did before user can add tags is the same as user can search tag and I have
to copy paste those pre-procedure before I can actually test.
Also, it seems that I cannot call a test method from a test method as discussed here. I am thinking of reusing test code but it is not advisable.
Am doing things correctly? Any thoughts?
To be honest your tests look very good if this is your first time doing TDD.
Reducing duplication
You can use the #Before annotation to execute some code before each test. In your case, it might look something like this:
// this method will be executed before each test
#Before
public void clickOnEditTextDescription() {
onView(withId(R.id.edittext_description_minimized))
.perform(click());
// put as much set up code in here as you need
}
Bear in mind that, in general, you should not make any assertions in the #Before method. It is for set up code only.
But is it always a good thing?
#Before methods are great, however, remember that copying and pasting test code is not always a bad thing. It's a different balance to production code. In production code, you want no duplication because any given piece of business logic should only exist in one place. In test code however, each test needs to be completely independent from all the other tests. If got rid of all the duplication in your test code, then it would be very difficult to change the shared code without breaking all your tests. Furthermore, your tests would be harder to read because you would have to keep referring to the shared code.
I recommend that you do some research on DAMP (descriptive and meaningful phrases) vs DRY (don't repeat yourself). DAMP is more relevant for unit tests, and allows you to repeat yourself sometimes. DRY is more relevant for production code. The following answer is great at explaining this:
https://stackoverflow.com/a/11837973/6816469
I want to perform a click on an item in my preference activity. I have tried following code,
onData(withKey("preference-key")).perform(click());
But it throws an exception like,
Caused by:
android.support.test.espresso.AmbiguousViewMatcherException: Multiple
Ambiguous Views found for matcher is assignable from class: class
android.widget.AdapterView
How can I properly perform a click on this item?
First of all, AmbiguousViewMatcherException as you notice means that you have at least two views with the same id, key, text, contentDescription etc.
Your code tells me that you're trying to write AdapterView test like you would have one view, so you try to use onData matcher like it was onView matcher. Sorry, but AdapterView tests are never as simple.
Instead of
onData(withKey("preference-key")).perform(click());
write code like this
onData(anything())
.inAdapterView(allOf(
isDescendantOfA(withId(R.id.fragment1)),
withId(R.id.listview)))
.atPosition(4)
.perform(click());
Look for AmbiguousViewMatcherException Espresso issues on StackOverflow. It's quit often problem, maybe someone had view like yours.