Setting Network Connection


#1

Is there a way I can set the network connection type when establishing the environment? I would like to turn off Wi-Fi and enable Mobile data.

Thanks!


#2

I thinks this is what you are looking for and its only for Android as mentioned in documentation.

http://appium.io/docs/en/writing-running-appium/other/network-connection/


#3

Thank you! Do you have an example of this being used?


#4

No i havent tried it because we are testing our app only on Wifi Networks and not switching while testing.


#5

I’m getting a problem where it cannot resolve method for “setNetworkConnection”


#6

Hi @Tom_Cockram , try something like this:

public void setConnectionToOFF() {
	try {
		driver.setConnection(new ConnectionStateBuilder().withWiFiDisabled().build());
		log.info("Switching OFF the connection : " + driver.getConnection());
	} catch (Exception e) {
		log.info("Connection could not be switch OFF");
	}
}

public void setConnectionToON() {
	try {
		driver.setConnection(new ConnectionStateBuilder().withWiFiEnabled().build());
		log.info("Switching On the connection: " + driver.getConnection());
	} catch (Exception e) {
		log.info("Connection could not be switch ON");
	}
}

#7

@Zuzeac Thanks very much for your example. I’ve implemented it into my class and get problems with the driver and log, where it can’t resolve the symbol. Any ideas?


#8

Just replace log.info with systemout.println, or remove them.
As for the driver, make sure you have the lastest appium and lastest Java client.
Can you show the exact error?


#9

image

Below is the whole class:

package com.metricell;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.connection.ConnectionStateBuilder;
import io.appium.java_client.remote.AndroidMobileCapabilityType;
import io.appium.java_client.remote.MobileCapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.net.MalformedURLException;
import java.net.URL;

//This method is used to setup the driver and testing environment. Will need to call this at the start of every test
public class MetricellTest {
public static AppiumDriver setupTests() {
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(MobileCapabilityType.DEVICE_NAME, “ZY224Gs7NG”);
caps.setCapability(MobileCapabilityType.APP, “com.metricell.datalogger”);
caps.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, “com.metricell.datalogger.ui.MainActivity”);
caps.setCapability(MobileCapabilityType.PLATFORM_NAME, “MobilePlatform.ANDROID”);
caps.setCapability(MobileCapabilityType.NO_RESET, “True”);
// TODO Screenshots for test failures before/after caps.setCapability(MobileCapabilityType.TAKES_SCREENSHOT, “true”);

    AppiumDriver<MobileElement> driver = null;


    try {
        driver = new AndroidDriver<>(new URL("http://0.0.0.0:4723/wd/hub"), caps);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    return driver;


}

public void setConnectionToOFf() {
    try {
        driver.setConnection(new ConnectionStateBuilder().withWiFiDisabled().build());
        log.info("Switching OFF the connection :  " + driver.getConnection());
    } catch (Exception e) {
        log.info("Connection could not be switch OFF");
    }
}

public void setConnectionToON() {
    try {
        driver.setConnection(new ConnectionStateBuilder().withWiFiEnabled().build());
        log.info("Switching On the connection: " + driver.getConnection());
    } catch (Exception e) {
        log.info("Connection could not be switch ON");
    }
}

}


#10

There might be an issue with your code, try something like this, just adjust the code to your needs.

package com.metricell;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.connection.ConnectionStateBuilder;
import io.appium.java_client.remote.AndroidMobileCapabilityType;
import io.appium.java_client.remote.MobileCapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.net.MalformedURLException;
import java.net.URL;

//This method is used to setup the driver and testing environment. Will need to call this at the start of every test
public class MetricellTest {
private AppiumDriver driver;

@Before
public void setUp() throws IOException {
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(MobileCapabilityType.DEVICE_NAME, “ZY224Gs7NG”);
caps.setCapability(MobileCapabilityType.APP, “com.metricell.datalogger”);
caps.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, “com.metricell.datalogger.ui.MainActivity”);
caps.setCapability(MobileCapabilityType.PLATFORM_NAME, “MobilePlatform.ANDROID”);
caps.setCapability(MobileCapabilityType.NO_RESET, “True”);
// TODO Screenshots for test failures before/after caps.setCapability(MobileCapabilityType.TAKES_SCREENSHOT, “true”);
driver = new AppiumDriver(new URL(“http://localhost:4723/wd/hub”), caps);

}

@After
public void tearDown() {
try {
driver.quit();
} catch (Exception ign) {}
}

@Test
public void setConnectionToOFf() {
try {
driver.setConnection(new ConnectionStateBuilder().withWiFiDisabled().build());
log.info("Switching OFF the connection : " + driver.getConnection());
} catch (Exception e) {
log.info(“Connection could not be switch OFF”);
}

@Test
public void setConnectionToON() {
try {
driver.setConnection(new ConnectionStateBuilder().withWiFiEnabled().build());
log.info("Switching On the connection: " + driver.getConnection());
} catch (Exception e) {
log.info(“Connection could not be switch ON”);
}
}
}


#11

Hi I’ve done some fixing up to get this to work as it previously did since this seems like a better way of initialising than what I was using.

I’ve had to make the “setUp” method static so that I can call it as I was getting an error when trying to call it. But my driver doesn’t like being static!! It’s providing this error:

driver = new AppiumDriver(new URL(“http://localhost:4723/wd/hub”), caps);

I’m getting this error: image


#12

Either remove the static from setUp or declare the driver as static.
private static AppiumDriver driver;


#13

Thanks, I’ve managed to get this working!