package Modules;
import Config.button;
import Config.module_config;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.MarkerManager;
import uiautomatorrpc.UiDevice;
import uiautomatorrpc.UiObject;
import uiautomatorrpc.UiSelector;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* Created by qiumin on 2015/8/14.
*/
public class module {
private HashMap<String, button> buttons;
private module_config config;
public Logger logger= LogManager.getLogger(this.getClass());
private final Marker MARKER = MarkerManager.getMarker("MODULE");
public UiDevice device=UiDevice.getInstance();
public Marker get_mark(){return MARKER;}
public void set_map(){
this.buttons=config.set_map();
}
public void set_cfg(module_config cfg) throws Exception{
this.config=cfg;
set_map();
}
public String get_pkg2_cfg(){return config.get_pkg2();}
public String get_pkg_cfg(){return config.get_pkg();}
public String get_apk_name(){return config.get_apk_name();}
public String get_attr(String name){
return this.config.get_attr(name);
}
public module_config.method_sel get_sel(String name){return this.config.get_sel(name);}
public button get_button(String name) throws Exception{
if(buttons.containsKey(name)) {
//logger.debug(MARKER,"'{}' is exist in config map.",name);
return buttons.get(name);
}else{
logger.error(MARKER,"The '{}' is not in config map.",name);
throw new Exception();
}
}
public void print_cfg(){
config.print_cfg();
}
public void open_apk() throws Exception{
boolean find=false;
logger.info(MARKER,"apk name is '{}'",config.name);
try {
find = scroll_find(config.name, false);
find = new UiObject(new UiSelector().text(config.name)).exist();
logger.info("find is {}",find);
if(!find){
for(int i=0;i<15;i++){
device.swipe(585, 360, 180, 360, 20);
find = new UiObject(new UiSelector().text(config.name)).exist();
if(find){
logger.info("finally find apk,click it");
click_button(new UiObject(new UiSelector().text(config.name)));
this.clicks("D1");
return;
}
logger.info("open-apk : {}",i);
}
}else {
logger.info("Click open apk");
click_button(new UiObject(new UiSelector().text(config.name)));
}
}catch (Exception e){
logger.error(MARKER," can't find '{}' .",config.name);
throw e;
} catch (Throwable e) {
e.printStackTrace();
logger.error(MARKER," ~ can't find '{}' .",config.name);
}
}
public void check_if_terminated() throws Exception{
clicks("D5");
String real_pkg=get_current_pkg();
if(real_pkg.contains(config.pkg)){
logger.info(MARKER, "The package name is correct. this apk is running correctly.");
}else {
logger.error(MARKER,"The package name is not correct (real({}) vs expect({})), this apk is not running correctly.",real_pkg,config.pkg);
throw new Exception();
}
}
public void clicks(String btns_in) throws Exception{
String [] btns=btns_in.split("\\|");
logger.debug(Arrays.toString(btns));
for(String btn : btns){
if(btn.contains("D")){
String a=btn.replace("D","");
int n=Integer.parseInt(a);
Thread.sleep(1000*n);
logger.info(MARKER,"Wait for "+n+" seconds.");
}
else if(btn.equals("HOME")){
press_home();
}
else if(btn.equals("BACK")){
press_back();
}
else if(btn.equals("CALL")){
press_call();
}
else if(btn.equals("CAMERA")){
press_camera();
}
else if(btn.equals("NOTIFY")){
open_notification();
}
else if(btn.equals("WAKE")){
wakup();
}
else{
click_button(btn);
}
}
}
public void click_button(String name) throws Exception{
button btn=get_button(name);
UiObject click_obj=button_to_uiobject(btn);
boolean b=button_exist(click_obj);
if(b) {
try {
click_obj.click();
logger.info(MARKER, "Try to click the button '{}' ", name);
// if (!a) {
// throw new Exception();
// }
} catch (Throwable throwable) {
logger.error(MARKER, "Click button '{}'failed.", name);
throw new Exception(throwable);
}
}else{
logger.error(MARKER,"'{}' is configed,but not exist..",name);
throw new Exception();
}
}
public void click_button(button btn) throws Exception{
UiObject click_obj=button_to_uiobject(btn);
boolean b=button_exist(click_obj);
if(b) {
try {
click_obj.click();
logger.info(MARKER, "Try to click the button '{}' ", btn.attribute);
// if (!a) {
// throw new Exception();
// }
} catch (Throwable throwable) {
logger.error(MARKER, "Click button '{}'failed.", btn.attribute);
throw new Exception(throwable);
}
}else{
logger.error(MARKER,"'{}' is configed,but not exist..",btn.attribute);
throw new Exception();
}
}
public void click_button(UiObject obj) throws Exception{
boolean b=button_exist(obj);
if(b) {
try {
obj.click();
logger.info(MARKER, "Try to click a UiObject.");
// if (!a) {
// throw new Exception();
// }
} catch (Throwable throwable) {
logger.error(MARKER, "Click a UiObject Failed.");
throw new Exception(throwable);
}
}else{
logger.error(MARKER,"This uiobject is not exist.");
throw new Exception();
}
}
public void write_button(String name,String text) throws Exception{
button btn=get_button(name);
UiObject write_obj=button_to_uiobject(btn);
boolean b=button_exist(write_obj);
if(b) {
try {
boolean a = write_obj.setText(text);
logger.info(MARKER, "Try to write '" + text + "' in button :" + name);
if (!a) {
throw new Exception();
}
} catch (Throwable throwable) {
logger.error(MARKER, "Write in button '" + name + "'failed.");
throw new Exception(throwable);
}
}else{
logger.error(MARKER,"'{}' is configed,but not exist..",name);
throw new Exception();
}
}
public void write_button(button btn,String text) throws Exception{
UiObject write_obj=button_to_uiobject(btn);
boolean b=button_exist(write_obj);
if(b) {
try {
boolean a = write_obj.setText(text);
logger.info(MARKER, "Try to write '{}' in button : '{}'", text, btn.attribute);
if (!a) {
throw new Exception();
}
} catch (Throwable throwable) {
logger.error(MARKER, "Write in button '{}'failed.", btn.attribute);
throw new Exception(throwable);
}
}else{
logger.error(MA
阿齐Archie
- 粉丝: 3w+
- 资源: 2474
最新资源
- 基于django以及xadmin的在线教育平台。全部资料+详细文档+高分项目.zip
- 基于mysql+django的学生成绩管理系统全部资料+详细文档+高分项目.zip
- 基于Python+Django+MySQL+Redis的商城练手项目(天天生鲜商城项目)全部资料+详细文档+高分项目.zip
- 基于Flask的信息发布,管理系统详细文档+全部资料+高分项目.zip
- 基于Python和Django的后台管理框架!全部资料+详细文档+高分项目.zip
- 基于python2.7.13+django+mysql编写 前端使用layui框架 web扫描器,主要提供子域名扫描,端口扫描,目录扫描,插件扫描的项目功能。还
- 基于Flask开发内网云盘,简单易上手详细文档+全部资料+高分项目.zip
- 基于flask框架的观影清单项目详细文档+全部资料+高分项目.zip
- 基于tensorflow的水果识别小程序,前端采用uni-app框架,后端采用django框架部署在阿里云服务器上。全部资料+详细文档+高分项目.zip
- 基于Flask框架的BBS网站,支持发帖,私信,人脸识别,情绪分类。详细文档+全部资料+高分项目.zip
- 基于Python flask的聚合api详细文档+全部资料+高分项目.zip
- 基于flask框架的数据分析网站详细文档+全部资料+高分项目.zip
- 基于Flask微电影网站详细文档+全部资料+高分项目.zip
- 基于使用 Flask 和 Bootstrap 构建的轻量博客,基于 Git 存储博客内容。支持自动更新、全文检索、Docker 部署详细文档+全部资料+高分项目.zip
- 基于Python Flask框架编写的Web应用作业提交系统,用于收集班级作业详细文档+全部资料+高分项目.zip
- 基于React+flask搭建的跳蚤市场,实现登录注册,图片裁剪,上架下架物品,搜索、收藏物品,聊天等功能详细文档+全部资料+高分项目.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈