是否可以在Appium中切换wifi网络?

时间:2018-07-13 07:29:35

标签: appium appium-android

在执行测试套件时,我必须在Android设备中可用的不同wifi网络之间进行切换。 在执行的中间,我必须将wifi连接从Wifi_Network_1更改为Wifi_Network_2,前提是知道wifi网络的ssid名称和密码。怎么做到呢? 我正在使用:

setTimeout(function(){ alert("Hello"); }, 3000);

由于NetworkConnectionSetting已过时,因此无法找到替代方法。 我可以使用

打开/关闭wifi
Appium server -->1.8.1
java-client --> 6.1.0
selenium-java --> 3.13.0

但是它不适合我的情况,因为我需要使用SSID名称及其密码在不同的wifi网络之间切换。 预先感谢。

1 个答案:

答案 0 :(得分:0)

嗨,我正在使用此方法,因为Appium不赞成使用其wifi开关,而且我已经通过控制台通过adb的命令创建了自己的替代项。它对我有用,请尝试一下:

public synchronized boolean wifiSetup(String udid, boolean flg) {
    synchronized (this) {
        String flgEnabled = (flg) ? "enable" : "disable";


        List<String> output = Console.runProcess(false, "adb -s " + udid + " shell am broadcast -a io.appium.settings.wifi --es setstatus " + flgEnabled);
        for (String line : output) {
            System.err.println(line);
            if (line.equalsIgnoreCase("Broadcast completed: result=-1"))
                return true;
        }
        return false;
    }

}

用于关闭

wifiSetup("xxxUDIDfromAndroid", false); 

用于打开

wifiSetup("xxxUDIDfromAndroid", true); 

这是调用控制台的一部分:

public class Console {
    private static final String[] WIN_RUNTIME = { "cmd.exe", "/C" };
    private static final String[] OS_LINUX_RUNTIME = { "/bin/bash", "-l", "-c" };

    private Console() {

    }

    private static <T> T[] concat(T[] first, T[] second) {
        T[] result = Arrays.copyOf(first, first.length + second.length);
        System.arraycopy(second, 0, result, first.length, second.length);
        return result;
    }

    @SuppressWarnings({ "hiding"})
    private static <String> String[] concatStr(String[] first, String[] second) {
        String[] result = Arrays.copyOf(first, first.length + second.length);
        System.arraycopy(second, 0, result, first.length, second.length);
        return result;
    }



    public static List<String> runProcess(boolean isWin, String... command) {           
        String[] allCommand = null;
        try {
            if (isWin) {
                allCommand = concat(WIN_RUNTIME, command);
            } else {
                allCommand = concat(OS_LINUX_RUNTIME, command);
            }
            ProcessBuilder pb = new ProcessBuilder(allCommand);
            pb.redirectErrorStream(true);
            Process p = pb.start();
            p.waitFor();
            BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String _temp = null;
            List<String> line = new ArrayList<String>();
            while ((_temp = in.readLine()) != null) {
//              system.out.println("temp line: " + _temp);
                line.add(_temp);
            }
//          system.out.println("result after command: " + line);
            return line;

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}

希望这会有所帮助,

相关问题