Change mobile state to offline mode/Airplane mode on Android and iOS?

I have a scenario in wherein i need to move real device to offline mode.

1 Like

Use below script to achieve this:

NetworkConnectionSetting connection = new NetworkConnectionSetting(airplaneMode, wifi, data)
driver.setNetworkConnection(connection);//here driver type is Android driver

For Ex to switch device in airplane mode use below script:
NetworkConnectionSetting connection = new NetworkConnectionSetting(true, false, false)
driver.setNetworkConnection(connection);//here driver type is Android driver

1 Like

Thanks @Mitesh_Agrawal.That works.
Also is there any way to do it for iOS. As i read the Some appium docs and it was mentioned that there is no such API available for iOS.
One way mentioned was "maybe with a screen swipe that pulls the control sheet upā€“you could click the airplane then. "
Any Thoughts?

Welcome Mayuresh :smile:

I donā€™t have any idea except the one stated by you above for iOS device

have you implemented it (iOS idea)? Is it feasible?

No I havenā€™t implemented yet

Npā€¦L try it outā€¦Hope it works

I do it exactly the way you saidā€¦ Swipe the control up and click on airplane mode

What are the swiping co-ordinates u pass?As it needs to be precise i guess

@Mitesh_Agrawal thanks man it worked in android real device

Welcome Jagadeesh :smile:

Iā€™m using ruby and Iā€™m using

__swipe(0.5,0.999,0.5,0.2)

being by function:

def __swipe(start_x, start_y, end_x, end_y, duration = 1)
    swipe(:start_x => start_x, :start_y => start_y, :end_x => end_x, :end_y => end_y, :duration => duration*1000)
end

In ruby we can use % so that translate into floats, while in java I think its only possible to pass int so it may not even be possible, not sure.

Thanks Manā€¦iā€™ll Check it out in java.

In Java Swipe Up:

Dimensions d=driver.manage().window().getSize();
int width=d.getWidth();
int height=d.getHeight();

int w=(int) (.5width);
int h1=(int)(.2
height);

driver.swipe(w,height,w,h1);

This worked for me.

@Telmo_Cardoso Wanted to know how do you Click on Airplane Mode buttons(Basically interact with the Controls on control center) Are you using Direct co-ordinates(eg. (100,200)) or Giving any locators(eg.Xpath)

Direct coordinates. You are outside your app so you wont be able to interact with objects my querying them in ios

User TouchActions to interact with anything outside your app

1 Like

Ok cool :). Have used the same. Was thinking if there was an option in Appium to switch to the control center ,interact and come back to the app.This would make the code generic across all the devices.(As direct co-ordinates might change across devices)

1 Like

Thanks man! This was really helpful. However, why are you multiplying the height and width by ā€˜.5ā€™ and ā€˜.2ā€™ ? Didnā€™t get hang of that.

He his finding the middle of the width so that the swipe is performed in the center of the phone horizontally and then for the real swipe movement he his going from full height until 20% of height.

1 Like

Dear All,

Let me do my best to help you allā€¦

Use driver.swipe functionality to bring the settings from bottom.
{ now if you wonder on how to find the co- ordinates , then
A. Connect iPad to your device with Appium inspector opened.
B. Swipe up the settings manually .
C. Click on ā€˜Refreshā€™ button in Appium Inspector.
4. You may notice ā€œQuick Settingsā€ preview on Appium inspector screen itself.
5. Now choose ā€œPrecise Tapā€ from Appium Inspector and tap over ā€˜Wifi Iconā€™ (Green dot will be get marked) , this will also display the ā€œStartXā€ and ā€œStartYā€ value.
6. Pass these value as an argument to the drive.tap (method).
}

Now mode got switched to Offline.

To close the ā€œSettingsā€, you donā€™t need to swipe. Instead you may follow the same steps and tap anywhere on page.
// hiding keyboard

int tap_startX= (int) 341.2;
int tap_startY = (int)451.3;
int tap_finger= 1;
int tap_duration=2;
driver.tap(tap_finger, tap_startX , tap_startY , tap_duration );

//swipe up the settings to disable wifi
int swipe_startX= (int) 347.8;
int swipe_startY = (int)1021.4;
int swipe_endx= (int)363.6;
int swipe_endy= (int)800.6;
int swipe_duration =(int) 0.5;
driver.swipe(swipe_startX, swipe_startY, swipe_endx, swipe_endy, swipe_duration);
// disable wifi
int mode_startX= (int) 331.3;
int mode_startY = (int)907.3;
int mode_finger= 1;
int mode_duration=2;
driver.tap(mode_finger, mode_startX , mode_startY , mode_duration );

1 Like