How to disable wi-fi on Android device in Appium? - java

In our test case: I need to disable Wi-Fi at some specific point/action.
I have verified:
driver[deviceIndex].setConnection(Connection.NONE);
assertEquals(Connection.ALL, driver[deviceIndex].getConnection());
But it's not doing anything.
My expectation is "turning OFF wi-fi" and keeping Mobile Data ON.
Current code is doing no action.

You need to add Apache common langs to your project.
https://mvnrepository.com/artifact/org.apache.commons/commons-lang3/3.9

In appium you can only toggle the wifi network using driver.toggleWifi() method. That means it will change wifi to off state if it is in on state and vice versa.
To use toggleWifi() method you must use androidDriver. If you are using AppiumDriver you can typecast it to AndroidDriver like following.
((AndroidDriver) driver).toggleWifi();
You can also use setNetworkConnection() method but it also has certain limitaions. The limitations are:

What is the type of driver you are using? You should use AndroidDriver if you are working on Android. Here are the working code for me.
// turn on all (data and wi-fi)
public void turnOnAllData() {
driver.setConnection(Connection.ALL);
}
// turn off all (data and wi-fi)
public void turnOffAllData() {
driver.setConnection(Connection.NONE);
}
// turn on airplane
public void turnOnAirplaneMode() {
driver.setConnection(Connection.AIRPLANE);
}
// turn on data
public void turnOnMobileData() {
driver.setConnection(Connection.DATA);
}
// turn on wi-fi
public void turnOnWiFi() {
driver.setConnection(Connection.WIFI);
}

Related

Carrier custom application

I need to create an Android application to set carrier configuration(VoLte e.g.). The application should fetch configs from our Back-End and apply them on the phone.
In Android documentation I found the following article: This article says, that I can create my own application and override CarrierService.
public class SampleCarrierConfigService extends CarrierService {
private static final String TAG = "SampleCarrierConfigService";
public SampleCarrierConfigService() {
Log.d(TAG, "Service created");
}
#Override
public PersistableBundle onLoadConfig(CarrierIdentifier id) {
Log.d(TAG, "Config being fetched");
PersistableBundle config = new PersistableBundle();
config.putBoolean(
CarrierConfigManager.KEY_CARRIER_VOLTE_AVAILABLE_BOOL, true);
config.putBoolean(
CarrierConfigManager.KEY_CARRIER_VOLTE_TTY_SUPPORTED_BOOL, false);
config.putInt(CarrierConfigManager.KEY_VOLTE_REPLACEMENT_RAT_INT, 6);
// Check CarrierIdentifier and add more config if needed…
return config;
}
}
I created an app with this service, but the method onLoadConfig(CarrierIdentifier id) is never called by the system.
So what I want from the system to call my overridden method, not system's. What should I do?
I found your question when researching how to do something similar.
In the article you linked it says:
The carrier app in question must be signed with the same certificate found on the SIM card, as documented in UICC Carrier Privileges.
Since we can't get the certificate from your carrier (they will never give it to you) I think we can't implement our own flavour sadly :-(

Google Api Client interface methods explanation?

#Override
public void getLeaderboardGPGS() {
if (gameHelper.isSignedIn()) {
startActivityForResult(Games.Leaderboards.getLeaderboardIntent(gameHelper.getApiClient(), getString(R.string.event_score)), 100);
}
else if (!gameHelper.isConnecting()) {
loginGPGS();
}
}
#Override
public void getAchievementsGPGS() {
if (gameHelper.isSignedIn()) {
startActivityForResult(Games.Achievements.getAchievementsIntent(gameHelper.getApiClient()), 101);
}
else if (!gameHelper.isConnecting()) {
loginGPGS();
}
}
Can anyone explain to me what these methods do? I have them as part of implementing a GoogleApi interface I made in the context of a tutorial. I especially don't understand the 100 / 101 parts, but the whole thing, in general, is quite confusing for me.
PS. I am making a game in LibGDX and this is my first time touching the Google Play API (or I think any API for that matter)
First Method getLeaderboardGPGS show you Leaderboard above your Activity
if you are already Signed in otherwise it start signing process.
Above method definition is from Libgdx wiki but it should be
private final static int REQUEST_CODE_UNUSED = 9002;
startActivityForResult(Games.Leaderboards.getLeaderboardIntent(gameHelper.getApiClient(), getString(R.string.leaderboardId)), REQUEST_CODE_UNUSED);
REQUEST_CODE_UNUSED is an arbitrary integer for the request code
getString(R.string.leaderboardId) is LEADERBOARD_ID
taken from Google wiki
Second Method getAchievementsGPGS is used to show a player's achievements, call getAchievementsIntent() to get an Intent to create the default achievements UI.
startActivityForResult(Games.Achievements.getAchievementsIntent(gameHelper.getApiClient()), REQUEST_ACHIEVEMENTS);
where REQUEST_ACHIEVEMENTS is an arbitrary integer used as the request code.

Android Content Provider Test the REAL content Provider

hope you can help me ...
tl:dr
How can i write JUnit Tests which will NOT use the classes IsolatedContext and MockContentResolver ? I want to affect the REAL content Provider and not the Mock Database.
General
I have to write JUnit Tests for a special ContentProvider at work.
This Content Provider is connected to some different Hardware and sets there some values. I must check the Hardware Values AND the Values of the Content Provider Database.
Construct
-> ContentProvider -> Hardware Interface -> Hardware -> HardwareInterface-> ContentProvider
Code
public class DataLayerTests extends ProviderTestCase2<DataLayer> {
private static final String TAG = DataLayerTests.class.getSimpleName();
MockContentResolver mMockResolver;
public DataLayerTests() {
super(DataLayer.class, Constants.DATA_LAYER_AUTHORITY);
}
#Override
protected void setUp() throws Exception {
super.setUp();
Log.d(TAG, "setUp: ");
mMockResolver = getMockContentResolver();
}
#Override
protected void tearDown() throws Exception {
super.tearDown();
Log.d(TAG, "tearDown:");
}
public void testActiveUserInsert__inserts_a_valid_record() {
Uri uri = mMockResolver.insert(ActiveUserContract.CONTENT_URI, getFullActiveUserContentValues());
assertEquals(1L, ContentUris.parseId(uri));
}}
The real Database should be affected as well as the Real ContentRescolver should be used.
How could i arcive this ?
You can use Robolectric to test the real content provider, affecting a real sqlite database.
Robolectric is an implementation of the Android framework that can be run in any JVM, and thus can be used for tests.
Please note that the sqlite database will live in a temp folder on your computer and not on a phone or emulator.
If you want the tests to happen inside a real phone, you should look into Instrumented tests

Google Fit Listen for Data Updates not working

I'm trying to implement a Google Fit Listener when data is updated into Google Fit services.
In this link of Google Fit documentation there is a simple example, however, it is not 100% clear. For that reason, I have two problems:
I don't know how to implement mResultCallback variable (there aren't any examples in this documentation).
When I define a simple ResultCallback (it seems to work but I'm not sure) and I launch the application, it gives me a result error code: java.lang.SecurityException: Signature check failed
The code within the HistortyApi lists one of android.permission.ACCESS_FINE_LOCATION or android.permission.BODY_SENSORS as being required.
Adding those permissions to my code hasn't resolved the same problem though.
Confirmed bug in Google Fit services. See discussion in https://plus.google.com/110141422948118561903/posts/Lqri4LVR7cD
mResultCallback is a ResultCallback<Status> so you need to implement a class of that type. Documentation is here, but there's only one method you need to implement:
public abstract void onResult (Status result)
The standard way is to do this using an anonymous class either when you declare mResultCallback or when you're using it as a parameter. Below is an example from Google's BasicRecordingAPI example:
Fitness.RecordingApi.subscribe(mClient, DataType.TYPE_ACTIVITY_SAMPLE)
.setResultCallback(new ResultCallback<Status>() {
#Override
public void onResult(Status status) {
if (status.isSuccess()) {
if (status.getStatusCode()
== FitnessStatusCodes.SUCCESS_ALREADY_SUBSCRIBED) {
Log.i(TAG, "Existing subscription for activity detected.");
} else {
Log.i(TAG, "Successfully subscribed!");
}
} else {
Log.i(TAG, "There was a problem subscribing.");
}
}
});
If you want to use a member variable you can simply make an assignment instead:
ResultCallback<Status> mResultCallback = new ResultCallback<Status>() {
#Override
public void onResult(Status status) {
...
}
});
Of course you can define a non-anonymous class, but if you did that for every callback you had you would end up creating a LOT of classes.

Google Cast - Cannot Resolve mSelectedDevice

I am having a problem making a Google Cast Service. I can not seem to find what to use for mSelectedDevice. Both tutorials that I am using do not provide enough explanation for this, and neither go into detail of what mSelectedDevice should be.
public class CastMediaRouterCallback extends MediaRouter.Callback{
#Override
public void onRouteSelected(MediaRouter router, MediaRouter.RouteInfo info) {
mSelectedDevice = CastDevice.getFromBundle(info.getExtras());
String routeId = info.getId();
//Startd NanoHTTPD, Find URI of photo/video, and display on Cast device
}
#Override
public void onRouteUnselected(MediaRouter router, MediaRouter.RouteInfo info) {
teardown();
mSelectedDevice = null;
}
}
(Tutorials I am using: https://developers.google.com/cast/docs/android_sender /// https://www.binpress.com/tutorial/building-an-android-google-cast-sender-app/161)
mSelecteDevice is an instance variable that is of type CastDevice. Not sure what you mean by "Google Cast Service" in your question but it seems you might be better off grabbing a sample project from oue GitHub repo as your starting point.

Categories