APP——自动化——python——driver其他常用的方法

一、AndroidDriver下的方法

1、启动其他app

/使用命令获取adb shell dumpsys window windows | findstr "mCurrentFocus"


Activity activity=new Activity("appPackage", "appActivity");


activity.setAppWaitActivity("appWaitActivity");                     //启动后和起始activity不一致需要添加此项


activity.setAppWaitPackage("appWaitPackage");                //启动后和起始activity不一致需要添加此项


driver.startActivity(activity);//启动第三方app


2、获取当前activity 

driver.currentActivity();


3、获取当前网络状态

driver.getConnection().name();


4、设置网络

driver.setConnection(Connection.ALL);//wifi和流量都打开
driver.setConnection(Connection.DATA);//移动流量模式driver.setConnection(Connection.WIFI);//wifi模式
driver.setConnection(Connection.NONE);//关闭所有
driver.setConnection(Connection.AIRPLANE);//飞行模式


5、获取当前界面所有资源

driver.getPageSource();


6、获取当前设备的横竖屏方向及设置方向

driver.getOrientation().name();//PORTRAIT竖屏,LANDSCAPE横屏
driver.rotate(ScreenOrientation.LANDSCAPE);//设置为横屏
driver.rotate(ScreenOrientation.PORTRAIT);//设置为竖屏


7、判断app是否安装

driver.isAppInstalled("appPackage");


8、安装app

driver.installApp("appPath");


9、卸载app

driver.removeApp("appPackage");


10、重置app(清除数据的操作),只针对当前测试的app,不能指定某个app

driver.resetApp();


11、打开通知栏

driver.openNotifications();


12、关闭app和启动app(只针对当前测试的app)

driver.closeApp();//关闭app
driver.launchApp();//再次运行app


13、获取当前设备的时间

driver.getDeviceTime();


14、滑动

driver.swipe(int startx,int starty,int xend,int yend,int duration );
前两个参数是滑动起始点的坐标,中间两个参数是滑动结束点的坐标,最后一个是持续时间

driver.swipe(300,300,300,1000,500);


15、设置隐式等待

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);//设置隐式等待为10秒


16、获取应用占用屏幕的大小

int width = driver.manage().window().getSize().getWidth();//宽
int height = driver.manage().window().getSize().getHeight();//高


17、获取当前的context(混合app时使用)

river.getContext();//获取当前context
Set<String> contexts=driver.getContextHandles();//获取所有的context driver.context("WEBVIEW_XXX");//转换到webview
driver.context("NATIVE_APP");//转到到原生的


18、设备屏幕是否被锁相关api

driver.isLocked();//判断是否锁屏
driver.unlockDevice();//解锁设备
driver.lockDevice();//锁定设备


19、设备按键操作

driver.pressKeyCode(AndroidKeyCode.BACK);//按键返回
driver.pressKeyCode(AndroidKeyCode.BACKSPACE);//按键删除driver.pressKeyCode(AndroidKeyCode.HOME);//按键home键driver.pressKeyCode(AndroidKeyCode.KEYCODE_0);//按键0 driver.longPressKeyCode(AndroidKeyCode.HOME);//长按home键

//组合键
driver.pressKeyCode(AndroidKeyCode.KEYCODE_0, AndroidKeyMetastate.META_SHIFT_ON);

20. 截图
File file=driver.getScreenshotAs(OutputType.FILE); FileUtils.copyFile(file, new File("images/file.png"));


二、AndroidElement下的方法

1、点击

element.click();


2、输入

element.sendKeys("test");
element.replaceValue("test");


3、清除输入框

element.clear();


4、获取元素的某个属性值(不能获取password,package,index,bounds这三个属性,”content-desc”使用name)

1)获取context-desc属性值,

//context-desc属性使用name和contentDescription都可以;


//第一种:如果context-desc属性没有值,那么就去获取text属性,可能会造成判断失误
String content_desc1=night.getAttribute("name");


//第二种:只会找context-desc的属性值;
String content_desc2=night.getAttribute("contentDescription");


2)获取long-Clickable的属性值

String long_Clickable=night.getAttribute("longClickable");


3)获取元素resource-id的属性值

String resource_Id=night.getAttribute("resourceId");


4)获取checked

element.getAttribute("checked");


5、获取元素坐标

int center_x = element.getCenter().getX();//中心点x
int center_y = element.getCenter().getY();//中心点y
int start_x = element.getLocation().getX();//起始点x
int start_y = element.getLocation().getY();//起始点y
int width = element.getSize().getWidth();//元素的宽
int height = element.getSize().getHeight();//元素的高


6、元素上的滑动

element.swipe(SwipeElementDirection.UP,10,10,500);//向上
element.swipe(SwipeElementDirection.DOWN,10,10,500);//向下
element.swipe(SwipeElementDirection.LEFT,10,10,500);//向左
element.swipe(SwipeElementDirection.RIGHT,10,10,500);//向右


7、获取元素的文本(text属性的值)

String text = element.getText();


原文链接:https://blog.csdn.net/jffhy2017/java/article/details/84336174

原文地址:https://www.cnblogs.com/xiaobaibailongma/p/13195976.html