How to remove all network connection in appium? – pCloudy – Question and Answers
Home >> Appium Tricks and Tricks >> How to remove all network connection ...

How to remove all network connection in appium?


Code Snippet:

package com.networkConnection;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.Connection;
import io.appium.java_client.android.HasNetworkConnection;

public class Runner {
	AppiumDriver driver;

	@BeforeTest
	public void setUpSuite() throws Exception {

		// You may start Appium server by below code or start manually
		// service = AppiumDriverLocalService.buildDefaultService();
		// service.start();
	}

	@BeforeMethod
	public void prepareTest() throws IOException, InterruptedException {

		
	}

	@Test
	public void Test1() throws MalformedURLException, InterruptedException {
		DesiredCapabilities capabilities = new DesiredCapabilities();
		capabilities.setCapability("newCommandTimeout", 600);
		capabilities.setCapability("launchTimeout", 90000);
		capabilities.setCapability("deviceName", "4200dd90b45eb39");
		capabilities.setCapability("platformName", "Android");
		capabilities.setCapability("appPackage", "io.cloudgrey.the_app");
		capabilities.setCapability("appActivity", "io.cloudgrey.the_app.MainActivity");
		capabilities.setCapability("rotatable", true);
		driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
		//All Connection Removed
		((HasNetworkConnection) driver).setConnection(Connection.NONE); 
		//All Connection Established
		((HasNetworkConnection) driver).setConnection(Connection.ALL);
		driver.findElement(By.xpath("//android.widget.TextView[ @text='Echo Box']")).click();
		driver.findElement(By.xpath("//android.widget.EditText[@text='Say something']")).sendKeys("hii");
		driver.findElement(By.xpath("//android.widget.TextView[ @text='Save']")).click();
		
	}


	@AfterMethod
	public void endTest() throws IOException {

		if (driver != null)
			driver.quit();
	}

	@AfterTest
	public void tearDownSuite() throws IOException {

		// Appium stop in AfterTest
		// service.stop();
	}


}

 

1 thought on “How to remove all network connection in appium?

  1. Mamtha M R Reply

    When i use this code, i am getting this error: for android version7 and above

    An Unknown Server-Side Error Occurred While Processing The Command. Original Error: Error Executing AdbExec. Original Error: ‘Command ”C://Program Files//Android//Sdk//Platform-Tools//Adb.Exe’ -P 5037 -S HKL3GJDH Shell Am Broadcast -A Android.Intent.Action.AIRPLANE_MODE –Ez State False’ Exited With Code 4294967295′; Stderr: ‘Security Exception: Permission Denial: Not Allowed To Send Broadcast Android.Intent.Action.AIRPLANE_MODE From Pid=3670 Uid=

    how to resolve it

Leave a Reply

Your email address will not be published. Required fields are marked *