[uiautomator篇][11]wifi

package com.softwinner.network.wifi;

import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.RemoteException;
import android.support.test.InstrumentationRegistry;
import android.support.test.uiautomator.By;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiObjectNotFoundException;
import android.support.test.uiautomator.UiSelector;
import android.support.test.uiautomator.Until;
import android.util.Log;

import java.io.IOException;

import static android.support.test.InstrumentationRegistry.getArguments;

import static android.support.test.InstrumentationRegistry.getContext;
import static org.junit.Assert.assertTrue;

/**
 * @author liuzhipeng
 * Created by Administrator on 2017/6/27.
 */

public class wifiBaseClass {
    private String packageName = "com.example.black.wifiswitch";
    private UiDevice mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
    private String ssid ;
    private String password ;
    private String mLogTag ;
    private WifiManager mWifiManager;
//    = (WifiManager) InstrumentationRegistry.getContext().getSystemService(Context.WIFI_SERVICE);

    public wifiBaseClass(Context context, UiDevice device, String SSID, String passwd, String logTag, String packName){
        mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        mDevice = device;
        ssid = SSID;
        password = passwd;
        mLogTag = logTag;
        packageName = packName;
    }

    /**
     * after connect wifi, check the network is available
     * @author liuzhipeng
     * @throws UiObjectNotFoundException
     * @throws InterruptedException
     */
    public void connectWifiAndCheckNetwork() throws UiObjectNotFoundException, InterruptedException {

        final String ssidStr = "com.example.black.wifiswitch:id/ssid";
        final String passwdIdStr = "com.example.black.wifiswitch:id/password";
        final String connectIdStr = "com.example.black.wifiswitch:id/Connect" ;
        Log.i(mLogTag,"trigger on wifi");
        triggerOnWifi();
        Log.i(mLogTag,"open wifiswitch apk");
        openApplication(packageName);
        Thread.sleep(5000);
        try {GetWiFiParameters();} catch (RemoteException e) {e.printStackTrace();}
        Log.i(mLogTag,"connect wifi: " + ssid);
        wakeupScreen();
        UiObject ssidObj = mDevice.findObject(new UiSelector().resourceId(ssidStr));
        ssidObj.setText(ssid);
        wakeupScreen();
        UiObject passwordObj = mDevice.findObject(new UiSelector().resourceId(passwdIdStr));
        passwordObj.setText(password);
        wakeupScreen();
        UiObject connectObj = mDevice.findObject(new UiSelector().resourceId(connectIdStr));
        connectObj.click();
        Thread.sleep(5000);
        assertTrue("wifi state not enabled", checkWifiState() == 3);
        Log.i(mLogTag, "check network available?");
        assertTrue("wifi network unavailable", isNetworkAvailable());
        Log.i(mLogTag, "network available");
    }

    /**
     * open third application:
     * @author liuzhipeng
     * @param packageNameStr
     */
    public void openApplication(String packageNameStr){

        try {mDevice.wakeUp();} catch (RemoteException e) {e.printStackTrace();}
        UiDevice mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
        /* Start from the home screen*/
        mDevice.pressHome();

//        final String launcherPackage = mDevice.getLauncherPackageName();
//        assertThat(launcherPackage,notNullValue());
//        try {mDevice.wakeUp();} catch (RemoteException e) {e.printStackTrace();}
//        mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)),
//                5000);

        // launch the app
        Context context = InstrumentationRegistry.getContext();
        final Intent intent = context.getPackageManager()
                .getLaunchIntentForPackage(packageNameStr);
        // Clear out any previous instances
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        context.startActivity(intent);

        try {mDevice.wakeUp();} catch (RemoteException e) {e.printStackTrace();}
        // Wait for the app to appear
        mDevice.wait(Until.hasObject(By.pkg(packageNameStr).depth(0)),
                5000);
    }

    /** get wifi parameters: ssid and password
     * @author liuzhipeng
     * @throws RemoteException
     */
    private void GetWiFiParameters() throws RemoteException {

        Bundle bundle = getArguments();
        if (bundle.getString("ssid") != null) {
            ssid = bundle.getString("ssid");
            if (bundle.getString("password") != null) {
                password = bundle.getString("password");
            } else {
                password = null;
            }
        }
    }

    /**
     * trigger on wifi
     * @author liuzhipeng
     */
    public void triggerOnWifi(){

//        WifiManager mWifiManager = (WifiManager) InstrumentationRegistry.getContext().getSystemService(Context.WIFI_SERVICE);
        if (!mWifiManager.isWifiEnabled()) {
            mWifiManager.setWifiEnabled(true);
            try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}
        }
        checkWifiState();
    }

    /**
     * trigger off wifi
     * @author liuzhipeng
     */
    public void triggerOffWifi(){

//        WifiManager mWifiManager = (WifiManager) InstrumentationRegistry.getContext().getSystemService(Context.WIFI_SERVICE);
        if (mWifiManager.isWifiEnabled()) {
            mWifiManager.setWifiEnabled(false);
            try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}
        }
        checkWifiState();
    }

    /**
     * check wifi state
     * @author liuzhipeng
     * @return wifiState
     */
    public int checkWifiState(){

//        WifiManager mWifiManager = (WifiManager) InstrumentationRegistry.getContext().getSystemService(Context.WIFI_SERVICE);
        int tempInt =  mWifiManager.getWifiState();
        switch (tempInt){
            case 0:
                Log.i(mLogTag, "wifi state disabling");
                break;
            case 1:
                Log.i(mLogTag, "wifi state disabled");
                break;
            case 2:
                Log.i(mLogTag, "wifi state enabling");
                break;
            case 3:
                Log.i(mLogTag, "wifi state enabled");
                break;
            case 4:
                Log.i(mLogTag, "wifi state unknown");
                break;
            default:
                break;
        }
        return tempInt;

    }

    /**
     * @author liuzhipeng
     * check network is available
     * @return true if networkAviabile else false
     */
    public static boolean isNetworkAvailable(){

        ConnectivityManager connectivityManager = (ConnectivityManager) InstrumentationRegistry.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = connectivityManager.getActiveNetworkInfo();
        return (info != null && info.isConnected() && (info.getType() == ConnectivityManager.TYPE_WIFI));
    }

    /**
     * wakeup screen
     * @author liuzhipeng
     */
    public void wakeupScreen(){
        Context context = InstrumentationRegistry.getContext();
        PowerManager pm=(PowerManager) context.getSystemService(Context.POWER_SERVICE);
        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK,"bright");
        wl.acquire();
        wl.release();
    }

    public void quitApplication(String packageNameStr)
    {
        try {
            mDevice.executeShellCommand("am force-stop "+ packageNameStr);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    public void goToSleep(){
        Context context = InstrumentationRegistry.getContext();
        PowerManager pm =(PowerManager) context.getSystemService(Context.POWER_SERVICE);
//        pm.goTosleep()
        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ON_AFTER_RELEASE| PowerManager.PARTIAL_WAKE_LOCK,"wakeLockUtil");
        wl.acquire();
        wl.release();

    }
}
时间: 2024-11-01 01:50:32

[uiautomator篇][11]wifi的相关文章

[uiautomator篇][11]下载dump,解析xml的方法

1 project/pad(你的项目)/build-gradle 添加:红色部分 dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' androidTestCompile 'com.android.support:support-annotations:24.0.0' androidTestCompile 'com.android.support.test:

【智能家居篇】wifi网络结构(上)

转载请注明出处:http://blog.csdn.net/Righthek 谢谢! WIFI是什么,相信大家都知道,这里就不作说明了.我们需要做的是深入了解其工作原理,包括软硬件.网络结构等.先说明一下WIFI是遵循IEEE802.11协议的,802.11是最早被国际标准组织认可的无线局域网协议,应该是1999年,到现在都有15年了.那时候哥还在读小学,连电脑都没摸过!太落后了...后来发展出很多以字母为后缀的802.11标准协议,如a.b.g.n.ac等. 本章节不作802.11协议的讲解,后

【智能家居篇】wifi驱动的理解(1)——驱动架构

转载请注明出处:http://blog.csdn.net/Righthek 谢谢! 在分析WIFI驱动前,分享一下个人对Linux驱动的一些了解,其实纵观Linux众多的设备驱动,几乎都是以总线为载体,所有的数据传输都是基于总线形式的,即使设备没有所谓的总线接口,但是Linux还是会给它添加一条虚拟总线,如platform总线等:介于WIFI的驱动实在是太庞大了,同时又是基于比较复杂的USB总线,所以建议读者在看此文章之前,先了解一下USB设备驱动和网络设备驱动. 我们要看懂WIFI驱动,首先要

【智能家居篇】wifi网络结构(下)

转载请注明出处:http://blog.csdn.net/Righthek 谢谢! 由于WIFI网络具有移动性,同时WIFI以无线电波作为传输媒介,这种媒介本质上是开放的,且容易被拦截,任何人都可以通过抓包工具截取无线网络的数据包.因此,在设计WIFI协议(其实就是802.11协议)时,需要提供一些传输数据和管理的服务.         1. 分布式(Distribution) 只要基础结构型网络里的移动式站点传送任何数据,就会使用这项服务.一旦基站接收到帧.就会使用分布式服务将帧送至目的地.任

[uiautomator篇]uiwatcher 的使用场景

http://www.yangyanxing.com/article/use-watcher-in-uiautomator.html 今天在uiautomator中实践了watcher的用法,这个也是之前在python中使用uiautomator中比较喜欢的功能,它可以提前定义一些条件,当满足一些条件时,进行一些操作,这个常用于处理测试过程中某些意料之外的或者不知道什么时候弹出来的框而阻碍测试的正常进行.之前在写自动化用例的时候,遇到过小米手机在安装app的时候,会弹一个框来让用户点击安装,还有

【智能家居篇】wifi网络接入原理(中)——认证Authentication

转载请注明出处:http://blog.csdn.net/Righthek 谢谢! 还是用手机来举例,扫描完成后,我们会选择想要加入的WIFI热点.此时,大部分都会弹出一个输入密码的窗口,当然也有不用输入密码的.这个过程叫做:认证(Authentication). 有时觉得搞技术的真累,当我们辛辛苦苦地在网上找资料学习新技术时,当我们渐渐掌握这门技术之后,才发现该技术对自己来说是新的,但是对现时来说,已经过时并且被遗弃了!所以说--Shit! 由于网上大量讲解WIFI技术的资料在认证方面都是侧重

iOS开发多线程篇 11 —自定义NSOperation

iOS开发多线程篇—自定义NSOperation 一.实现一个简单的tableView显示效果 实现效果展示: 代码示例(使用以前在主控制器中进行业务处理的方式) 1.新建一个项目,让控制器继承自UITableViewController. 1 // 2 // YYViewController.h 3 // 01-自定义Operation 4 // 5 // Created by apple on 14-6-26. 6 // Copyright (c) 2014年 itcase. All rig

【智能家居篇】wifi网络接入原理(上)——扫描Scanning

转载请注明出处:http://blog.csdn.net/Righthek 谢谢! 对于低头党来说,在使用WIFI功能时,经常性的操作是打开手机上的WIFI设备,搜索到心目中的热点,输入密码,联网成功,各种低头上网...这个看似简单的过程,背后却是隐藏着大量的无线通信技术.用几个专业术语来表示这个过程,分别是:扫描(Scanning).认证(Authentication).关联(Association).下面用一张图来表示这个过程. 图1  WIFI接入网络过程 现在让我们来分析一下这个过程的工

[uiautomator篇] apk 允许uiautomator用例

http://blog.csdn.net/cxq234843654/article/details/52605441 uiautomator2.0+脱离PC运行(apk启动uiautomator2.0+)的实现方案 效果: 打开MyTest.apk,点击run uiautomator,就能直接运行你的脚本. 方案概述: 新建一个Android app工程MyTest,在Activity中添加Button,用于启动脚本 给这个app添加系统签名 在MyTest中新建一个module,命名为MyTe