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


#1

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


#2

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


#3

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?


#4

Welcome Mayuresh :smile:

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


#5

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


#6

No I haven’t implemented yet


#7

Np…L try it out…Hope it works


#8

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


#9

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


#10

@Mitesh_Agrawal thanks man it worked in android real device


#11

Welcome Jagadeesh :smile:


#12

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.


#13

Thanks Man…i’ll Check it out in java.


RUBY[IOS]Can we toggle flight mode in IOS
#14

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.


#15

@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)


#16

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


#17

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)


#18

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.


#19

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.


#20

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 );