Security exception when testing Wi-Fi on robots

I am currently working on testing Android with Robotium. When I run the following test test in Android 2.3 emulator, I get the error message "java.lang.SecurityException: WifiService: neither user 10038 nor the current process have android.permission.CHANGE_WIFI_STATE.".

Test code:

public void testScheduledPayments() { solo.clickOnButton(0); solo.enterText(0, "demo"); solo.enterText(1,"password"); solo.clickOnButton("Log In"); WifiManager wifiMan = (WifiManager) solo.getCurrentActivity().getSystemService(Context.WIFI_SERVICE); wifiMan.setWifiEnabled(false); solo.clickOnText("Payments"); solo.clickInList(2); solo.waitForText("Loading paymnets"); solo.clickInList(0); solo.clickOnMenuItem("Logout"); solo.clickOnButton("Logout"); solo.finishOpenedActivities(); } 

In the My Manifest file:

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.tesaptra" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="10" /> <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.info.native" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <uses-library android:name="android.test.runner" /> </application> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission> <uses-permission android:name="android.permission.INTERNET"></uses-permission> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission> </manifest> 

And here is my log cat file

 java.lang.SecurityException: WifiService: Neither user 10038 nor current process has android.permission.CHANGE_WIFI_STATE. at android.os.Parcel.readException(Parcel.java:1322) at android.os.Parcel.readException(Parcel.java:1276) at android.net.wifi.IWifiManager$Stub$Proxy.setWifiEnabled(IWifiManager.java:614) at android.net.wifi.WifiManager.setWifiEnabled(WifiManager.java:713) at com.testaptra.Payments.testScheduledPaymnets(Payments.java:113) at java.lang.reflect.Method.invokeNative(Native Method) at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:204) at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:194) at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:186) at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169) at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154) at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:529) at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1448) 

I added all permissions to manifest.xml , but no luck. Could you help me if you are familiar with the question below?

Thank you in advance

+6
source share
2 answers

Permission must be set in the target application. Permissions in the test application are completely ignored. This means that the instrumnetation apk never has more privileges than the target application.

+8
source

It is actually possible to give your test application more permissions than your real application, but for this you need to do something a little hidden with your Android manifest.

What you need to do is give your applications the same android: sharedUserId in your manifest, this means that if they are added to the extra permission so that both of them have access to it. Using this, you can use your test application with a lot of permissions to do whatever you want, I used this approach and a different approach (make the intent of the service your test application can catch in order to do what the application usually doesn’t run, which you are testing) to check the type of script you are trying to execute.

+1
source

Source: https://habr.com/ru/post/946565/


All Articles