How to use getConnection method of io.appium.java_client.android.connection.HasNetworkConnection class

Best io.appium code snippet using io.appium.java_client.android.connection.HasNetworkConnection.getConnection

Run io.appium automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

NetworkDemo.java

Source: NetworkDemo.java Github

copy
1package day4AppiumScripts;
2
3import java.net.MalformedURLException;
4import java.net.URL;
5
6import org.openqa.selenium.ScreenOrientation;
7import org.openqa.selenium.remote.DesiredCapabilities;
8
9import io.appium.java_client.AppiumDriver;
10import io.appium.java_client.LocksDevice;
11import io.appium.java_client.MobileElement;
12import io.appium.java_client.android.AndroidDriver;
13import io.appium.java_client.android.SupportsNetworkStateManagement;
14import io.appium.java_client.android.connection.HasNetworkConnection;
15
16public class NetworkDemo {
17
18	public static void main(String[] args) throws MalformedURLException, InterruptedException {
19		// TODO Auto-generated method stub
20DesiredCapabilities cap = new DesiredCapabilities();
21		
22		cap.setCapability("deviceName", "DemoSonal");
23		cap.setCapability("platformName", "ANDROID");
24		cap.setCapability("platformVersion", "8.1.0");
25		cap.setCapability("appPackage","com.android.messaging");
26		cap.setCapability("appActivity","com.android.messaging.ui.conversationlist.ConversationListActivity");
27		
28		AppiumDriver <MobileElement> driver = new AndroidDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"),cap);
29		
30	
31		
32		//Lock a device
33		
34		((LocksDevice) driver).lockDevice();
35		
36		Thread.sleep(2000);
37		
38		
39		((LocksDevice) driver).unlockDevice();
40		
41		
42		((AndroidDriver<MobileElement>) driver).openNotifications();
43		
44		Thread.sleep(2000);
45		
46		driver.rotate(ScreenOrientation.LANDSCAPE);
47		
48		
49		Thread.sleep(2000);
50		
51		driver.rotate(ScreenOrientation.PORTRAIT);
52		
53		// it tell you if wifi is enabled or not
54		// if wifi is ON : true
55		//if wifi is OFF : false
56		boolean wifi =((HasNetworkConnection) driver).getConnection().isWiFiEnabled();
57		
58		System.out.println(wifi);
59		
60		((SupportsNetworkStateManagement) driver).toggleWifi(); // switch off wifi
61		
62		Thread.sleep(2000);
63		
64		boolean wifi1 =((HasNetworkConnection) driver).getConnection().isWiFiEnabled();
65		
66		System.out.println("IS wifi enabled" + wifi1);  //false
67		
68		boolean air =((HasNetworkConnection) driver).getConnection().isAirplaneModeEnabled();
69		
70		System.out.println(air);
71		
72		Thread.sleep(2000);
73		
74		((SupportsNetworkStateManagement) driver).toggleWifi(); // switch ON wifi
75		
76
77		Thread.sleep(2000);
78		
79boolean wifi2 =((HasNetworkConnection) driver).getConnection().isWiFiEnabled();
80		
81		System.out.println("IS wifi enabled" + wifi2);  //True
82		
83		
84		
85		
86		
87		
88		
89		
90		
91		
92		
93		
94		
95		
96		
97	}
98
99}
100
Full Screen

Toggle.java

Source: Toggle.java Github

copy
1package com.epam.jdi.light.mobile.elements.common.network;
2
3import io.appium.java_client.android.AndroidDriver;
4import io.appium.java_client.android.SupportsNetworkStateManagement;
5import io.appium.java_client.android.connection.ConnectionState;
6import io.appium.java_client.android.connection.HasNetworkConnection;
7
8import static com.epam.jdi.light.mobile.MobileUtils.executeDriverMethod;
9
10/**
11 * Only for Android
12 */
13public class Toggle {
14
15    public static void toggleWifi(){
16        executeDriverMethod(AndroidDriver.class, SupportsNetworkStateManagement::toggleWifi);
17    }
18
19    /**
20     * Work only on android version >= 7
21     */
22    public static void toggleAirplaneMode(){
23        executeDriverMethod(AndroidDriver.class, SupportsNetworkStateManagement::toggleAirplaneMode);
24    }
25
26    public static void toggleData(){
27        executeDriverMethod(AndroidDriver.class, SupportsNetworkStateManagement::toggleData);
28    }
29
30    public static ConnectionState getConnection(){
31        return executeDriverMethod(AndroidDriver.class, HasNetworkConnection::getConnection);
32    }
33}
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Most used method in HasNetworkConnection

Trigger getConnection code on LambdaTest Cloud Grid

Execute automation tests with getConnection on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)