How to use withDataDisabled method of io.appium.java_client.android.connection.ConnectionStateBuilder class

Best io.appium code snippet using io.appium.java_client.android.connection.ConnectionStateBuilder.withDataDisabled

Run io.appium automation tests on LambdaTest cloud grid

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

MobileConnectionType.java

Source: MobileConnectionType.java Github

copy
1package com.synergy.server.enums;
2
3import io.appium.java_client.android.connection.ConnectionState;
4import io.appium.java_client.android.connection.ConnectionStateBuilder;
5
6public enum MobileConnectionType {
7
8    WIFI ("wifi"),
9    NONE ("none"),
10    AIRPLANE ("airplane"),
11    DATA ("data"),
12    ALL ("all");
13
14    private final String value;
15
16    MobileConnectionType(String value) {
17    this.value = value;
18  }
19
20    public String value() {
21    return value;
22  }
23  
24    public static MobileConnectionType getEnumByString(String value) {
25        if (value == null) {
26            return null;
27        }
28        
29        for (MobileConnectionType connection : MobileConnectionType.values()) {
30    	    if (value.equalsIgnoreCase(connection.value)) {
31        	    return connection;
32            }
33         }
34        return null;
35    }
36
37    public static ConnectionState getConnection(MobileConnectionType type) {
38        if (type.equals(NONE)) {
39            return new ConnectionStateBuilder().withAirplaneModeDisabled().withDataDisabled().withWiFiDisabled()
40                    .build();
41        } else if (type.equals(AIRPLANE)) {
42            return new ConnectionStateBuilder().withAirplaneModeEnabled().withDataDisabled().withWiFiDisabled()
43                    .build();
44        } else if (type.equals(WIFI)) {
45            return new ConnectionStateBuilder().withAirplaneModeDisabled().withDataDisabled().withWiFiEnabled()
46                    .build();
47        } else if (type.equals(DATA)) {
48            return new ConnectionStateBuilder().withAirplaneModeDisabled().withDataEnabled().withWiFiDisabled()
49                    .build();
50        } else if (type.equals(ALL)) {
51            return new ConnectionStateBuilder().withAirplaneModeDisabled().withDataEnabled().withWiFiEnabled()
52                    .build();
53        }
54
55        return null;
56    }
57  
58}
59
Full Screen

Connection_State.java

Source: Connection_State.java Github

copy
1package week2.day2;
2
3import java.net.MalformedURLException;
4import java.net.URL;
5import java.util.concurrent.TimeUnit;
6import org.openqa.selenium.WebElement;
7import org.openqa.selenium.remote.DesiredCapabilities;
8import io.appium.java_client.android.AndroidDriver;
9import io.appium.java_client.android.connection.ConnectionState;
10import io.appium.java_client.android.connection.ConnectionStateBuilder;
11
12public class Connection_State {
13
14	public static void main(String[] args) throws MalformedURLException, InterruptedException {
15
16		DesiredCapabilities dc = new DesiredCapabilities();
17
18		dc.setCapability("deviceName", "Redmi Note 7 Pro");
19		dc.setCapability("platformName", "Android");
20		dc.setCapability("automationName", "UiAutomator2");
21		dc.setCapability("noReset", true);
22		dc.setCapability("appPackage", "com.testleaf.leaforg");
23		dc.setCapability("appActivity", "com.testleaf.leaforg.MainActivity");
24
25		AndroidDriver<WebElement> driver = new AndroidDriver<WebElement>(new URL("http://0.0.0.0:4723/wd/hub"), dc);
26		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
27		
28		driver.openNotifications();
29		
30		ConnectionStateBuilder csb = new ConnectionStateBuilder();
31		ConnectionState state = csb.withDataDisabled().build();
32		
33		driver.setConnection(state);
34	
35	}
36
37}
Full Screen

actions.java

Source: actions.java Github

copy
1package Actions;
2
3import io.appium.java_client.PerformsTouchActions;
4import io.appium.java_client.android.AndroidDriver;
5import io.appium.java_client.android.connection.ConnectionStateBuilder;
6import org.openqa.selenium.By;
7import org.openqa.selenium.Dimension;
8import org.openqa.selenium.WebDriver;
9import io.appium.java_client.TouchAction;
10import io.appium.java_client.touch.offset.PointOption;
11
12import static io.appium.java_client.touch.WaitOptions.waitOptions;
13import static java.time.Duration.ofMillis;
14
15import org.openqa.selenium.WebElement;
16import org.openqa.selenium.support.ui.ExpectedConditions;
17import org.openqa.selenium.support.ui.WebDriverWait;
18import setUp.appiumSetUp;
19
20public class actions extends appiumSetUp {
21
22    @SuppressWarnings("rawtypes")
23    PointOption pointOption = new PointOption();
24    private WebDriver driver;
25
26    public actions() {
27
28        this.driver = getDriver();
29    }
30
31    public void swipeLeft() {
32        final int ANIMATION_TIME = 200; // ms
33        final int PRESS_TIME = 200; // ms
34        int edgeBorder = 10;
35        PointOption pointOptionStart, pointOptionEnd;
36        Dimension dims = driver.manage().window().getSize();
37        pointOptionStart = PointOption.point(dims.width / 2, dims.height / 2);
38        pointOptionEnd = PointOption.point(edgeBorder, dims.height / 2);
39        // execute swipe using TouchAction
40        try {
41            new TouchAction((PerformsTouchActions) driver)
42                    .press(pointOptionStart)
43                    // a bit more reliable when we add small wait
44                    .waitAction(waitOptions(ofMillis(PRESS_TIME)))
45                    .moveTo(pointOptionEnd)
46                    .release().perform();
47        } catch (Exception e) {
48            System.err.println("swipeScreen(): TouchAction FAILED\n" + e.getMessage());
49            return;
50        }
51
52        // always allow swipe action to complete
53        try {
54            Thread.sleep(ANIMATION_TIME);
55        } catch (InterruptedException e) {
56            // ignore
57        }
58    }
59
60    public WebDriverWait waitUntilVisible(WebDriver driver, long timeOutInSeconds) {
61        return new WebDriverWait(driver, timeOutInSeconds);
62    }
63
64    public void disableInternetConnection() {
65        ((AndroidDriver<WebElement>) driver).setConnection(new ConnectionStateBuilder().withWiFiEnabled().withDataEnabled().build());
66    }
67
68    public void enableInternetConnection() {
69        ((AndroidDriver<WebElement>) driver).setConnection(new ConnectionStateBuilder().withWiFiDisabled().withDataDisabled().build());
70    }
71
72
73    public void swipe(int startX, int startY, int endX, int endY) throws InterruptedException {
74
75        TouchAction t = new TouchAction((AndroidDriver<WebElement>) driver);
76        t.press(PointOption.point(startX, startY)).moveTo(PointOption.point(endX, endY)).release().perform();
77    }
78
79    public void waitForVisibility(By e, long timeOutInSeconds) {
80        WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
81        wait.until(ExpectedConditions.visibilityOfElementLocated(e));
82    }
83}
84
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

Trigger withDataDisabled code on LambdaTest Cloud Grid

Execute automation tests with withDataDisabled 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)