.

How to turn Wifi ON/OFF using Appium?


If you have worked on appium for the automation of native apps or mobile browser(firefoxOS, chromium or native), then this is not a weird thought to automate actions which are neither app specific nor browser specific. I'm talking about system features like launching settings and turning wifi ON/OFF, dialing a number using dialer.

All you need to know id the package name of settings app and its launcher activity. Once it is launched, you can do anything on the screen as you do for native apps.

Here is a sample test case to turn wifi ON using appium.


@Test()
    public void turnOnWifi() {

        AppiumDriver driver = null;
        DesiredCapabilities capabilities = null;
        capabilities = new DesiredCapabilities();
        capabilities.setCapability("platformName", "Android");
        capabilities.setCapability("deviceName", "4a119f6b");
        capabilities.setCapability("appPackage", "com.android.settings");
        capabilities.setCapability("appActivity",
                "com.android.settings.Settings");
        capabilities.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT,
                120);
        try {
            driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),
                    capabilities);
        } catch (Exception e) {
            System.out.println("Unable to start driver.");
        }
        try {
            WebElement wifiBtn = driver.findElement(
                    By.id("com.android.settings:id/wifi_settings"))
                    .findElement(By.id("com.android.settings:id/switchWidget"));

            if (wifiBtn.getAttribute("checked").equals(false)) {
                wifiBtn.click();
            } else {
                System.out.println("Wifi Already ON");
            }

            Thread.sleep(5000);
        } catch (Exception e) {
            System.out.println("Error while turning ON Wifi");
        }
    }


2 comments :

  1. Hi, How can i turn on or off the wifi in IOS?

    ReplyDelete
  2. Hei, how can I do the same with robot framework? I tried to put such a keyword with args:

    Open Application http://localhost:4723/wd/hub platformName=Android platformVersion=5.0 deviceName=${device_name} appPackage=com.android.settings appActivity=com.android.settings.Settings but got an error : WebDriverException: Message: A new session could not be created. (Original error: Activity used to start app doesn't exist or cannot be launched! Make sure it exists and is a launchable activity)

    ReplyDelete

Recent Posts