Remove app from running apps by swiping out using UI automator - android

I have to write a test case using UIautomator to swipe it out from the recent apps.I am trying below code but not sure what should be "id of the app".I tried running uiautomatorviewer and all apps are showing same resourceId.
uiDevice.pressRecentApps();
UiObject app = uiDevice.findObject(new UiSelector().resourceId("The id of the app"));
app.swipeLeft(100);

You should be able to find the app using text.
UiObject app = uiDevice.findObject(new UiSelector().text("appText"));
app.swipeLeft();
If you do a search by text, then you end up getting the element that the text is actually written on.
I'm not sure if you can swipe on that element, so if you need a parent element this question covered how to do that.

Related

Appium does not find element until it is tapped on mobile device. How to find element for which it does not have focus?

I am new for automation testing.
I am trying to automate UI and want to tap on humburger menu.
I tried it with
1. MobileElement el1 = driver.findElement(By.xpath("//*[#content-desc='" + "Menu" + "']"));
el1.click();
2. MobileElement el1 = driver.findElement(By.name("Menu)"));
el1.click();
3. MobileElement el1 = (MobileElement) driver.findElementByXPath("//android.widget.ImageButton[#content-desc=\"Menu\"]");
in all the cases i got error for element not found.
But when i tap manually on menu within test befor el1.click() get executed then test get run successfully. It is just like menu element get focused and appium find it.
So how to execute this without tap or getting focus?
Try to find the element by its name. In UI Inspector search before with name or any other locator. If it searches successfully with few attempts. Sometimes element has dynamic identification.
Can you try your code again but this time set the capability automationName to uiautomator2? Your problem sounds like the ones I have sometimes and moving from UIA1 to UIA2 (uiautomator2) solved that. UIA2 is however a bit more slow detecting elements IMO.

How to remove pinned shortcuts?

Background
Starting from Android O, it's possible to create pinned shortcuts, which (on supported launchers) show a dialog to confirm the creation of them:
ShortcutInfoCompat pinShortcutInfo = new ShortcutInfoCompat.Builder(context, uniqueShortcutId)
.setShortLabel(label)
.setIntent(shortcutIntent)
.setLongLabel(label)
.setIcon(IconCompat.createWithBitmap(bitmap))
.build();
ShortcutManagerCompat.requestPinShortcut(this, pinShortcutInfo , null);
Docs:
https://developer.android.com/reference/android/content/pm/ShortcutManager.html
https://developer.android.com/guide/topics/ui/shortcuts.html
The problem
Sometimes, the pinned shortcut is not relevant anymore. For example, it points to something that doesn't exist anymore.
In this case, I want to be able to remove it.
What I've tried
I thought this is possible by the next code, but it doesn't, because it is probably about dynamic shortcuts, which is something else :
ShortcutManager shortcutManager = (ShortcutManager) getSystemService(SHORTCUT_SERVICE);
final List<ShortcutInfo> pinnedShortcuts = shortcutManager.getPinnedShortcuts();
final ArrayList<String> shortcutIdsToRemove = new ArrayList<>();
for (ShortcutInfo pinnedShortcut : pinnedShortcuts) {
final Intent pinnedShortcutIntent = pinnedShortcut.getIntent();
shortcutIdsToRemove.add(pinnedShortcut.getId());
}
shortcutManager.removeDynamicShortcuts(shortcutIdsToRemove);
// this also doesn't work : shortcutManager.disableShortcuts(shortcutIdsToRemove);
The question
How can I remove pinned shortcuts? Is it even possible?
Update: seems it isn't possible, as Google mentioned here.
So far, this API seems very restricting to me, as it has so many disadvantages:
The created icon image cannot be created from a resource ID.
On the launcher, the created icon has a tiny icon of the app that created it.
The icon cannot be removed via the API (only disabled), so if you created one that points to another app, and this app was removed, you won't be able to remove it.
The creation cannot be in the background, as it requires a confirmation dialog. It also cannot be created in a batch. Only one after another.
All pinned shortcuts your app has created will be removed if your app was removed.
So, my question now is:
Is there any way to create and remove a shortcut using the previous API, using an adb command, with root if needed ?
Is it possible to remove specific pinned shortcuts using adb command , with root if needed?
disableShortcuts() is the best you can do. It will show the user a custom error message when they try to select the disabled shortcut.
From the official documentation:
Shortcut limitations
Although you can publish up to five shortcuts (static and dynamic shortcuts combined) at a time for your app, most launchers can only display four.
However, there is no limit to the number of pinned shortcuts to your app that users can create. Even though your app cannot remove pinned shortcuts, it can still disable them.
Another section:
Because your app and its users can pin shortcuts to the device's launcher, it's possible that these pinned shortcuts could direct users to actions within your app that are out of date or no longer exist. To manage this situation, you can disable the shortcuts that you don't want users to select by calling disableShortcuts(), which removes the specified shortcuts from the static and dynamic shortcuts list and disables any pinned copies of these shortcuts. You can also use an overloaded version of this method, which accepts a CharSequence as a custom error message. That error message then appears when users attempt to launch any disabled shortcut.
Note: If you remove some of your app's static shortcuts when you update your app, the system disables these shortcuts automatically.

Android application xpath with content desc - handle the line break

Objective: To click on Access for all guest users.
I am unable to find this element.
uiautomatorviewer shows the following screenshot.
Content description is following copy/pasted
"Access for all guest users
Provider not listed? Stay tuned, we will be adding more TV providers shortly."
Notice: the line break after user. I believe this is why my xpath is not working.
My xpath is:
"//android.view.View[#content-desc ='Access for all guest usersProvider not listed? Stay tuned, we will be adding more TV providers shortly.']"
I cannot use index instead of content-desc because of the object in layers above have the same class and index.
Using tap to a specific might solve the problem but is not a good solution.
any ideas on how to handle that linebreak.
If you notice the XML hierarchy, you will notice that your element is inside a WebView (3rd node from top). So its not getting identified. You have to first switch to WebView and then use your code.
The way you mentioned the xpath should work, once you switch to WebView.

Not able to click on an element in Appium

My UI Automator Viewer Screenshot
I want to click on Play Now button and tried the following code:
driver.findElement(By.xpath("//android.view.View[contains(#content-desc,'Play Now')]"));
and this code:
driver.findElement(By.name("Play Now!"));
These codes are not showing any error but not clicking on Play Now button even.
Please help.
try this one
driver.findElementByAccessibilityId("content written on the button").click();
If you feel your Xpath is right but you are not click on that means that is because of synchronization problem. Just give time to find the element and click. Try like below example
Thread.sleep(5000);
driver.findElement(By.xpath("//android.view.View[contains(#content-desc,'Play Now')]")).click();
Thread.sleep(5000) gives 5 second time to search for the element. This should work in your case.
if click works fine not need to use any waits.
You can further use implicit or explicit wait once you are perfect with your automation.

No ID for my Android UI Testing

I would like to write some UI Test on my Android application in order to take automated screenshots.
My application is written with React-Native.
In order to write my tests in need to know the resource-id of my component but as you can see in this screenshot i can't see any resource-id with ui Automator Viewer in this React-Native example app.
Look this picture.
I would like to know if there is a way to give some IDs to my components so I can write some test or if there is another way to select my components.
Note : I'm trying to write Espresso Test.
The testID only works on iOS, and is not mapped to the resource-id in Android.
You can specify an accessibilityLabel to your component. In uiautomatorviewer it shows up as content-desc
To be cross-platform, you should specify both:
<Text style={styles.welcome} accessibilityLabel="MyId" testID="MyId">
Welcome!
</Text>
You can then search for it using something like webdriver with the tilde selector browser.waitForExist("~MyId")
First of all, ID of each UI Component of Android objects has to be defined in the resource(res) folder as layout XMLs(you need to make sure you have defined it in the right tags) and to obtain the IDs in the Android please follow the steps:
1. Launch Android Studio, Click on "Android Device Monitor" icon next to "SDK Manager"
2. Connect your device and launch the application
3. Select your device name from the list of devices on the left panel
4. Click on Dump "View Hierarchy for UI Automate"
5. Now you can hoover over any view on thde device dump and can see Resource-d in the node details in the right column.
Note: You can also take screenshots
1)First thing first if its your project you can go to the layout and add id to these component and then take screenshot using uiautomator viewer you will find your resource id
2) If option 1 is not possible(that's highly unlikely to happen) you can still test it as from your screenshot i can see only one edittext box so using uiauatomator you can test it on the basis of the class . Here below is the code:
private static UiDevice mDevice;
func(){
mDevice = UiDevice.getInstance(getInstrumentation());
UiObject xyz=new UiObject(new UiSelector().**getClass**(android.widget.EditText));
xyz.click();
xyz.setText("abcdef");
}
// I am not sure its getclass() or getclassName() check both option
//xyz.click() is necessary as in uiautoamtor you won't be able to right text
in a textbox until and unless it is selected
Yes there is! You can pass a testID prop to any component that implements the <View /> props, as documented here.
testID string
Used to locate this view in end-to-end tests.
See also this very informative blogpost about UITesting in React Native. It is about iOS testing, but the javascript part is going to be the same for Android.

Categories

Resources