Cannot be cast to org.openqa.selenium.mobile.NetworkConnection

@wreed @pr4bh4sh
Getting error while running below in AppiumDriver:

Error: cannot be cast to org.openqa.selenium.mobile.NetworkConnection

Code:
NetworkConnection mobileDriver = (NetworkConnection) driver;
if (mobileDriver.getNetworkConnection() != ConnectionType.AIRPLANE_MODE) {
// enabling Airplane mode
mobileDriver.setNetworkConnection(ConnectionType.AIRPLANE_MODE);
}

Have any additional code needed to set Airplane mode in device using NetworkConnection interface?

Thanks,
sanoj

 public static AppiumDriver<WebElement> driver;
 public static AndroidDriver<WebElement> _driver;

// Pass Caps

_driver = new AndroidDriver<WebElement>(new URL("http://127.0.0.1:4725/wd/hub"),capabilities);
driver = ((AppiumDriver<WebElement>)_driver);


	  @Test
	  public void networkConnectionTest() {
	    
		System.out.println("networkConnectionTest()");  
		NetworkConnectionSetting networkConnection = new NetworkConnectionSetting(false, true, false);
	    networkConnection.setData(true);  // enable mobile data
	    networkConnection.setWifi(false); // close wifi
	    _driver.setNetworkConnection(networkConnection);
	    networkConnection = _driver.getNetworkConnection();
	    System.out.println("airplaneModeEnabled() :: " + networkConnection.airplaneModeEnabled() +
	    				   "\ndataEnabled() :: " + networkConnection.dataEnabled() +
	    				   "\nwifiEnabled() :: " + networkConnection.wifiEnabled()
	    				  ); 
	    //assertEquals(new NetworkConnectionSetting(false, false, false), networkConnection);

	  }

@sanoj27, A friendly advise. Before you raise question in forum. Always tried to provide details. Like what you tried so far.
In additional note the question you are asking has nothing to do with Appium. Its basic question in Java. So try to find out question your self like why you are not able to cast? am i follow correct class hierarchy or what??
So i means to say always narrow down approach and first try to find solution your self. i believe thats is the only ways to learning.

People in this forum are very helpful and kind, and always try to help other. But provide your findings and details before you shoot your questions.

Take this as learning lesson :slight_smile:

Thanks,
Priyank Shah

3 Likes

sure @Priyank_Shah I will do as well

Your “driver” is a reference to an object of type AppiumDriver. AppiumDriver does not inherit from NetworkConnection, so you should rightfully get a casting exception. Check the Javadocs to see if there is a way for you to find the functionality you need. Hint: If this is an Android test, look at the documentation for AndroidDriver.

Thank you very much @afwang