java-appium-527 WebDriver协议&针对控件的操作
1.WebDriver协议
https://www.w3.org/TR/webdriver/#list-of-endpoints
1.1查看当前所有的session情况
http://127.0.0.1:4723/wd/hub/sessions
<img src="https://img2018.cnblogs.com/blog/1418970/201810/1418970-20181005113050038-1374996098.png" width="500"
1.2查看指定session
http://127.0.0.1:4723/wd/hub/session/${sessionId}
1.3获取source
http://127.0.0.1:4723/wd/hub/session/${sessionId}/source
2.针对控件进行操作
- click()
- sendKeys()
- clear() 清空
- findElement() 二次定位,用的少
- getAtttibute() 获取属性
- getLocation()获取X Y
- 是否展示isDisplayed()
public class XueqiuDemo {
private AndroidDriver driver;
@Before
public void setUp() throws MalformedURLException {
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.setCapability("platformName", "android");
desiredCapabilities.setCapability("deviceName", "domo");
desiredCapabilities.setCapability("appPackage", "com.xueqiu.android");
desiredCapabilities.setCapability("appActivity", ".view.WelcomeActivityAlias");
URL remoteUrl = new URL("http://localhost:4723/wd/hub");
driver = new AndroidDriver(remoteUrl, desiredCapabilities);
driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
}
@Test
public void sampleTest() throws InterruptedException {
exist_do("//*[@text=\"允许\"]").click();
Thread.sleep(2000);
locate("com.xueqiu.android:id/user_profile_icon").click();
Thread.sleep(2000);
locate("com.xueqiu.android:id/tv_login").click();
Thread.sleep(2000);
locate("com.xueqiu.android:id/tv_login_by_phone_or_others").click();
Thread.sleep(2000);
System.out.println("获取某个属性getAttribute:"+locate("com.xueqiu.android:id/register_phone_number").getAttribute("text"));
System.out.println("获取坐标getLocation():"+locate("com.xueqiu.android:id/register_phone_number").getLocation());
System.out.println("获取相对位置getRect():"+locate("com.xueqiu.android:id/register_phone_number").getRect());
System.out.println("获取长度和高度getSize():"+locate("com.xueqiu.android:id/register_phone_number").getSize());
System.out.println("获取控件的className getTagName():"+locate("com.xueqiu.android:id/register_phone_number").getTagName());
System.out.println("获取text属性getText()"+locate("com.xueqiu.android:id/register_phone_number").getText());
System.out.println("是否展示isDisplayed()"+locate("com.xueqiu.android:id/register_phone_number").isDisplayed());
System.out.println("toString():"+locate("com.xueqiu.android:id/register_phone_number").toString());
System.out.println("是否可用isEnabled():"+locate("com.xueqiu.android:id/register_phone_number").isEnabled());
System.out.println("是否选中isSelected():"+locate("com.xueqiu.android:id/register_phone_number").isSelected());
System.out.println("hashCode():"+locate("com.xueqiu.android:id/register_phone_number").hashCode());
System.out.println("getClass()"+locate("com.xueqiu.android:id/register_phone_number").getClass());
//System.out.println(""+locate("com.xueqiu.android:id/register_phone_number").);
locate("com.xueqiu.android:id/register_phone_number").sendKeys("123456789");
//寻找子元素
locate("//android.widget.LinearLayout[@resource-id=\"com.xueqiu.android:id/register_module\"]").findElement(By.id("com.xueqiu.android:id/register_phone_number")).clear();
driver.pressKeyCode(4);
}
public WebElement exist_do(String el){
if (locate(el).isDisplayed()){
return locate(el);
}
else{
return null;
}
}
public WebElement locate(String locate){
if (locate.matches("\\/\\/.*")){
return driver.findElementByXPath(locate);
}else{
return driver.findElementById(locate);
}
}
@After
public void tearDown() {
driver.quit();
}
}
执行结果:
objc[16328]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/bin/java (0x10bdb24c0) and /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/libinstrument.dylib (0x10ddf14e0). One of the two will be used. Which one is undefined.
十月 04, 2018 6:33:49 下午 io.appium.java_client.remote.AppiumCommandExecutor$1 lambda$0
信息: Detected dialect: W3C
获取某个属性getAttribute:请输入手机号
获取坐标getLocation():返回左上方的坐标(198, 655)
获取相对位置getRect():org.openqa.selenium.Rectangle@d0006a20
getRect().width,getRect().height,返回区域大小,同getRect()
getRect().x,getRect().y,返回左上角的坐标,同getLocation()
获取长度和高度getSize():(849, 121)
获取控件的className getTagName():android.widget.EditText
获取text属性getText()请输入手机号
是否展示isDisplayed()true
toString():[[io.appium.java_client.android.AndroidDriver, Capabilities: {appActivity=.view.WelcomeActivityAlias, appPackage=com.xueqiu.android, databaseEnabled=false, desired={platformName=android, appActivity=.view.WelcomeActivityAlias, appPackage=com.xueqiu.android, deviceName=domo}, deviceManufacturer=Xiaomi, deviceModel=Redmi Note 5, deviceName=406e8f3, deviceScreenSize=1080x2160, deviceUDID=406e8f3, javascriptEnabled=true, locationContextEnabled=false, networkConnectionEnabled=true, platform=LINUX, platformName=Android, platformVersion=8.1.0, takesScreenshot=true, warnings={}, webStorageEnabled=false}] -> id: com.xueqiu.android:id/register_phone_number]
是否可用isEnabled():true
是否选中isSelected():false
hashCode():1573
getClass()class io.appium.java_client.android.AndroidElement
Process finished with exit code 0
3.针对driver进行操作
driver.getPageSource(); 可用,获取页面的source
driver.getCapabilities(); 可用,获取capabilities
driver.navigate().back(); 可用,后退同back
driver.navigate().forward(); 怎么用?应用中前进失败,浏览器前进失败
driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS); 可用
driver.getKeyboard().sendKeys().releaseKey().pressKey() 怎么用?字符串传入失败
driver.rotate();切换屏幕方向
- driver.rotate(ScreenOrientation.LANDSCAPE); 横屏
- driver.rotate(ScreenOrientation.PORTRAIT);竖屏
driver.execute();怎么用?传入命令失败
driver.getAutomationName(); 如果capabilities没有传入automationName,输出null
driver.getCurrentUrl();怎么用?方法未实现
driver.getDeviceTime();Fri Oct 5 16:38:26 CST 2018
driver.getPlatformName();android
driver.hideKeyboard();可用
driver.lockDevice(); 锁屏可用
driver.runAppInBackground();怎么用?
driver.openNotifications();打开通知栏
driver.startActivity(ac);怎么用?页面没有启动
#java
Activity ac = new Activity("com.xueqiu.android",".view.WelcomeActivityAlias");
driver.startActivity(ac);
- shake 没有找到这个命令
- driver.getCurrentPackage();可用,返回当前包名
- driver.currentActivity();可用,返回当前页面
4.针对app操作
- driver.installApp("/Users/title.apk");可用
- driver.removeApp(bundleId);iOS专用,未验证
- driver.isAppInstalled(bundleId);iOS专用,未验证
- driver.closeApp();可用,停止应用
- driver.launchApp();可用,注意:启动过程需要时间等待。
public void TestDriver() throws InterruptedException{
exist_do("//*[@text=\"允许\"]").click();
Thread.sleep(8000);
System.out.println("close app");
driver.closeApp();
day_time();
Thread.sleep(10000);
System.out.println("launch app");
driver.launchApp();
day_time();
Thread.sleep(8000);
exist_do("//*[@text=\"允许\"]").click();
}
public void day_time(){
Date date = new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(df.format(date));
}
- driver.resetApp(); 重置应用
- driver.getAppStringMap()怎么用
- driver.getContextHandles();[NATIVE_APP]
- driver.getContext().toString();NATIVE_APP
- driver.context("NATIVE_APP");io.appium.java_client.android.AndroidDriver, Capabilities: {appActivity=.view.WelcomeActivityAlias, appPackage=com.xueqiu.android, automationName=UiAutomator2, databaseEnabled=false, desired={platformName=android, appActivity=.view.WelcomeActivityAlias, appPackage=com.xueqiu.android, automationName=UiAutomator2, deviceName=domo, newCommandTimeout=1200, noReset=true}, deviceApiLevel=27, deviceManufacturer=Xiaomi, deviceModel=Redmi Note 5, deviceName=406e8f3, deviceScreenDensity=440, deviceScreenSize=1080x2160, deviceUDID=406e8f3, javascriptEnabled=true, locationContextEnabled=false, networkConnectionEnabled=true, newCommandTimeout=1200, noReset=true, pixelRatio=2.75, platform=LINUX, platformName=Android, platformVersion=8.1.0, statBarHeight=66, takesScreenshot=true, viewportRect={left=0, top=66, width=1080, height=1964}, warnings={}, webStorageEnabled=false}
5.手势操作
- press 按
- release 释放
- moveTo 滑动
- tap wait
** tap与click的区别:tap是针对屏幕的点击,传入的是坐标PointOption,click针对的是元素
根据web driver协议,click需要传入element id,tap不需要
- longPress 长按
- cancel 取消
- perform 执行
- scroll 滚动执行失败
注意:按照官方示例,代码提示参数错误。
// 示例代码https://github.com/appium/appium/blob/master/docs/en/commands/interactions/touch/scroll.md
TouchActions action = new TouchActions(driver);
action.scroll(element, 10, 100);
action.perform();
API传参发生改变,注意参数类型,press只支持PointOption.point(x坐标,y坐标)此处为绝对坐标,
//官方示例
@Test
public void horizontalSwipingTest() throws Exception {
login();
driver.findElementByAccessibilityId("slider1").click();
MobileElement slider = driver.findElementByAccessibilityId("slider");
Dimension size = slider.getSize();
TouchAction swipe = new TouchAction(driver).press(slider, 0, size.height / 2).waitAction(2000)
.moveTo(slider, size.width / 2, size.height / 2).release();
swipe.perform();
}
结合示例,页面滑动代码
public void TestDriver() throws InterruptedException{
Thread.sleep(8000);
//获取控件列表,并迭代打印text
List<WebElement> con = driver.findElementsById("com.xueqiu.android:id/topic_text");
for(WebElement sub:con){
System.out.println(sub.getText());
Thread.sleep(3000);
}
TouchAction act = new TouchAction(driver).press(PointOption.point(con.get(1).getLocation().x,con.get(1).getLocation().y)).moveTo(PointOption.point(con.get(1).getLocation().x,500)).perform();
swipeToUp(driver);
Thread.sleep(5000);
}
可以在SwipeClass.java中,写入滑动的方法
public class SwipeClass {
static Duration duration=Duration.ofSeconds(1);
public void swipeToUp(AndroidDriver driver){
int width = driver.manage().window().getSize().width;
int height = driver.manage().window().getSize().height;
TouchAction act = new TouchAction(driver).press(PointOption.point(width/2,height*3/4)).waitAction(WaitOptions.waitOptions(duration)).moveTo(PointOption.point(width/2,height/4)).release();
act.perform();
}
public void swipeToDown(AndroidDriver driver){
int width = driver.manage().window().getSize().width;
int height = driver.manage().window().getSize().height;
TouchAction act = new TouchAction(driver).press(PointOption.point(width/2,height*1/4)).waitAction(WaitOptions.waitOptions(duration)).moveTo(PointOption.point(width/2,height*3/4)).release();
act.perform();
}
public void swipeToLeft(AndroidDriver driver){
int width = driver.manage().window().getSize().width;
int height = driver.manage().window().getSize().height;
TouchAction act = new TouchAction(driver).press(PointOption.point(width*3/4,height/2)).waitAction(WaitOptions.waitOptions(duration)).moveTo(PointOption.point(width/4,height/2)).release();
act.perform();
}
public void swipeToRight(AndroidDriver driver){
int width = driver.manage().window().getSize().width;
int height = driver.manage().window().getSize().height;
TouchAction act = new TouchAction(driver).press(PointOption.point(width/4,height/2)).waitAction(WaitOptions.waitOptions(duration)).moveTo(PointOption.point(width*3/4,height/2)).release();
act.perform();
}
}
//直接引用即可
@Test
public void TestDriver() throws InterruptedException{
Thread.sleep(8000);
swipe.swipeToUp(driver);
Thread.sleep(1000);
swipe.swipeToLeft(driver);
Thread.sleep(1000);
swipe.swipeToRight(driver);
Thread.sleep(1000);
swipe.swipeToDown(driver);
Thread.sleep(1000);
}
FAQ
1.遇到不会的方法,去查看appium示例
https://github.com/appium/appium/tree/master/docs/en 有些API已经变更,不一定能用
https://github.com/appium-boneyard/sample-code GitHub可能搜索不到sample-code
java-appium-527 WebDriver协议&针对控件的操作的更多相关文章
- WebDriver测试EXT控件(基于C#)
WebDriver测试EXT控件(基于C#)http://www.docin.com/p-748096409.html
- Aspose是一个很强大的控件,可以用来操作word,excel,ppt等文件
Aspose是一个很强大的控件,可以用来操作word,excel,ppt等文件,用这个控件来导入.导出数据非常方便.其中Aspose.Cells就是用来操作Excel的,功能有很多.我所用的是最基本的 ...
- Appium自动化(7) - 控件定位工具之Appium 的 Inspector
如果你还想从头学起Appium,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1693896.html 前言 上一篇文章介绍了另一块控件定 ...
- appium自动化测试(3)-控件定位&中文输入
参考-控件定位 http://www.2cto.com/kf/201410/340345.html appium接口 http://appium.io/slate/en/master/?python# ...
- appium ios 相对坐标点击和控件滑动操作
环境: 系统:ios 10.13.6 (17G12034) appium:1.14.0 xcode:10.1 iphone:iphone7 12.4 在尝试使用driver.tap([(a,b)],5 ...
- Selenium webdriver 之select 控件封装,解决onchange问题
使用webdriver的时候,select 控件经常会绑定onchange 事件,在selenium2.09 之前click 方法对onchange 事件有bug,2.09 以后修复了,但是根据经验也 ...
- appium通过index查找目标控件
2.1 通过判断控件属性获取控件 控件的所有属性都可以用作判断,比如它的text,index,resource-id是否clickable等,例如: 2.1.1 通过文本查找目标控件 1 2 el = ...
- java文件夹上传下载控件分享
用过浏览器的开发人员都对大文件上传与下载比较困扰,之前遇到了一个需要在JAVA.MyEclipse环境下大文件上传的问题,无奈之下自己开发了一套文件上传控件,在这里分享一下.希望能对你有所帮助. 以下 ...
- Appium自动化(6) - 控件定位工具之uiautomatorviewer 的详细介绍
如果你还想从头学起Appium,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1693896.html 前言 app定位不如web定位那么 ...
随机推荐
- L1-018 大笨钟
微博上有个自称“大笨钟V”的家伙,每天敲钟催促码农们爱惜身体早点睡觉.不过由于笨钟自己作息也不是很规律,所以敲钟并不定时.一般敲钟的点数是根据敲钟时间而定的,如果正好在某个整点敲,那么“当”数就等于那 ...
- DevExpress v17.2新版亮点—WPF篇(七)
用户界面套包DevExpress v17.2终于正式发布,本站将以连载的形式为大家介绍各版本新增内容.本文将介绍了DevExpress WPF v17.2 新的Hamburger Menu.Sched ...
- DevExpress XtraScheduler日程管理控件应用实例(1)-- 基本使用
在一些应用场景中,我们可能需要记录某一天,某个时段的日程安排,那么这个时候就需要引入了 DevExpress 的日程控件XtraScheduler 了,这个控件功能非常强大,提供了很好的界面展现方式, ...
- 解决Android4.3版本下,手机短彩接收中文文件名附件,中文名字的附件无法保存(第二步:解决从从数据库中读取附件文件名,并在长按后保存附件时,中文乱码导致的无法保存附件)
从第一步我们发现,在第一步修改之后,在短彩绘画界面中中文附件名的附件已无法显示,经过打印堆栈我们发现还是中文乱码在作祟.下面我们接着进行分析,这次我们从UI层往逻辑处理层进行分析.首先我们找到保存附件 ...
- FlytestingToolkit工具派送,懒人的测试思考
工欲善其事必先利其器,在IT路上摸爬这些年,去年我们分享了<Fiddler录制jmeter脚本,干货分享>,今天我们有另外的思考,我懒,故我思考. 下载解压后是这样的: 双击 Flytes ...
- caffe编译问题-src/caffe/net.cpp:8:18: fatal error: hdf5.h: No such file or directory compilation terminated.
错误描述 src/caffe/net.:: fatal error: hdf5.h: No such : recipe 操作过程 step1: 在Makefile.config文件更改INCLUDE_ ...
- 配置海康相机SDK文件
前言 项目使用到海康摄像机,进行二次开发需要首先对SDK文件进行相关配置. 实现过程 1.下载SDK开发包: 网址:http://www.hikvision.com/cn/download_61.ht ...
- python海龟绘图
最近学了python,看了几本书之后,才明白python的强大,python是一种解释型的语言,即每写一行程序就执行一行. 而且在科学计算方面,处理的能力特别的方便. 比如python中的字典dict ...
- smarty学习——基本概念
学习一种框架,我们最基本的就是掌握框架的思想,同时了解框架的基本语法. 1.对于定界符的了解 有的smarty模板标签都被加上了定界符. 默认情况下是 { 和},但它们是可被改变的.例如,我们假定你在 ...
- 彻底删除vscode及安装的插件和个人配置信息
1.卸载vscode应用软件(在控制面板里面找不到改软件,所以只能进入应用所在文件夹进行卸载) ## 此步骤虽然删掉了应用软件,但是此时重新安装会发现之前下载的插件和个人配置信息都还会重新加载出来,所 ...