How to allow "Stop Battery Optimizing usage" permission using appium

Hi,

I am working to automate one android application. I see a below permission popup on launching the application.

I have used capability like “capabilities.setCapability(“autoGrantPermissions”,“true”)” to grant all permissions but I am still getting the permission dialog.

I tried with other way by granting permission using adb like:
adb shell pm grant !!applicationpackagename - To be replaced with application package name!! android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS

But I am getting below error while running above adb command:

Please suggest how I can bypass this permission dialog while launching the application in automation session.

Thanks

Hi @abhi02101987 i am not sure how can we handle this via appium. But we can disable this at device/app level. Please follow the instruction based on your mobile device model. I followed for Samsung and it’s working
https://support.convoy.com/hc/en-us/articles/360053911434-How-to-Disable-Android-Battery-Optimization

@jagadeeshkotha: Thanks for your reply.

I want to enable this option for my application as I need to do some long running operations in background. But, I want to grant this permission when application is launched in automation session.
Please suggest how we can handle this alert using appium or some adb command.

I solved this by creating a thread that launch at the begining of the test and wait for the popup to appear then it click on allow

Thread batteryPopupHandler = new Thread(() -> {
			try {
				// Check for the presence of the popup element
				if (isElementDisplayed(okButtonAlert())) {
				             okButtonAlert().click();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		});

batteryPopupHandler.start();

You could try to disable battery optimizations for a particular package using adb:

adb shell dumpsys deviceidle whitelist +<package_name>
1 Like