[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();
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();
assertTrue("wifi state not enabled", checkWifiState() == );
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()),
);
} /** 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();} 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();} 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 :
Log.i(mLogTag, "wifi state disabling");
break;
case :
Log.i(mLogTag, "wifi state disabled");
break;
case :
Log.i(mLogTag, "wifi state enabling");
break;
case :
Log.i(mLogTag, "wifi state enabled");
break;
case :
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(); }
}
[uiautomator篇][11]wifi的更多相关文章
- [uiautomator篇][python] wifi接口学习网址
https://wifi.readthedocs.io/en/latest/wifi_command.html#usage
- .NET Core CSharp初级篇 1-1
.NET Core CSharp初级篇 1-1 本节内容是对于C#基础类型的存储方式以及C#基础类型的理论介绍 基础数据类型介绍 例如以下这句话:"张三是一名程序员,今年15岁重50.3kg ...
- 保护嵌入式802.11 Wi-Fi设备时需要考虑的10件事
保护嵌入式802.11 Wi-Fi设备时需要考虑的10件事 10 things to consider when securing an embedded 802.11 Wi-Fi device 随着 ...
- .NET C#教程初级篇 1-1 基本数据类型及其存储方式
.NET C# 教程初级篇 1-1 基本数据类型及其存储方式 全文目录 (博客园).NET Core Guide (Github).NET Core Guide 本节内容是对于C#基础类型的存储方式以 ...
- 【智能家居篇】wifi网络结构(上)
转载请注明出处:http://blog.csdn.net/Righthek 谢谢! WIFI是什么.相信大家都知道,这里就不作说明了. 我们须要做的是深入了解其工作原理,包含软硬件.网络结构等.先说明 ...
- ESP8266开发之旅 网络篇⑤ Scan WiFi——ESP8266WiFiScan库的使用
1. 前言 现在,通常,为了让手机连上一个WiFi热点,基本上都是打开手机设置里面的WiFi设置功能,然后会看到里面有个WiFi热点列表,然后选择你要的连接上. 基本上你只要打开手机连接WiF ...
- 26-ESP8266 SDK开发基础入门篇--编写WIFI模块 SmartConfig/Airkiss 一键配网
https://www.cnblogs.com/yangfengwu/p/11427504.html SmartConfig/Airkiss 配网需要APP/微信公众号,这节大家先使用我做好的APP/ ...
- 【智能家居篇】wifi网络接入原理(上)——扫描Scanning
转载请注明出处:http://blog.csdn.net/Righthek 谢谢! 对于低头党来说,在使用WIFI功能时,常常性的操作是打开手机上的WIFI设备,搜索到心目中的热点,输入passwor ...
- iOS开发多线程篇 11 —自定义NSOperation
iOS开发多线程篇—自定义NSOperation 一.实现一个简单的tableView显示效果 实现效果展示: 代码示例(使用以前在主控制器中进行业务处理的方式) 1.新建一个项目,让控制器继承自UI ...
随机推荐
- slf4j介绍以及与Log4j、Log4j2、LogBack整合方法
翻了一下百度和官网.这么介绍slf4j. slf4j 全称 Simple Logging Facade for Java,是日志框架的一种抽象,那么也就是说 slf4j 是不能单独使用的必须要有其他实 ...
- 死磕 java并发包之AtomicStampedReference源码分析(ABA问题详解)
问题 (1)什么是ABA? (2)ABA的危害? (3)ABA的解决方法? (4)AtomicStampedReference是什么? (5)AtomicStampedReference是怎么解决AB ...
- linux安装redis官方教程
官方链接:http://redis.io/download Download, extract and compile Redis with: $ wget http://download.redis ...
- Selenium私房菜系列5 -- 第一个Selenium RC测试案例
<Selenium简介>中讲过,Selenium RC支持多种语言编写测试案例,如:C#,Python.在工作中,我倾向于是用Python这类动态语言编写测试案例,因为这样的测试案例无需编 ...
- redis 一些使用过的命令
因为我是JAVA的,所以也是用java的api 主要是文档看起来太麻烦,自己英文也不好,每次用之前都要看一遍,自己把常用的一点点的放进来,方便使用 分布式连接池对象配置 JedisPoolConfig ...
- 关闭windows7/8的自动升级到windows10
办公室的电脑已经有好几台自动升级到windows10了. 由于用着很不习惯都要求改回windows7. 升级了就不支持退回去,只能是全部删除重新安装了,很是麻烦.但是也没有看到哪里有可以关闭自动升级的 ...
- LeetCode Isomorphic Strings 对称字符串
题意:如果两个字符串是对称的,就返回true.对称就是将串1中的同一字符都一起换掉,可以换成同串2一样的. 思路:ASCII码表哈希就行了.需要扫3次字符串,共3*n的计算量.复杂度O(n).从串左开 ...
- Bootstrap历练实例:导航中的表单
Bootstrap历练实例:导航中的表单,它是使用class.navbar-form类,这确保了表单适当的垂直对齐和在较窄的视口中折叠的行为,使用这个对齐方式选项来决定导航栏中的内容放置在哪里. 实例 ...
- HTML5<picture>元素
HTML5<picture>元素可以设置多张图片 <!DOCTYPE html><html><head><meta http-equiv=&quo ...
- graphviz layer 教程(非布局)
官方 pdf 上讲解的很少,没有图片. http://www.graphviz.org/wiki/how-use-drawing-layers-overlays 这里有图片,但是又没有说如何生成. 直 ...