元素识别方法:
前面也说过appium也是以webdriver为基础的,对于元素的定位也基本一致,只是增加一些更适合移动平台的独特方式:
id定位: 主要看resource id的值: 函数: driver.findElementById(resource_id的值); class定位: 主要看class的值 函数: driver.findElementByClassName(class的值); AndroidUIAutomator定位: 主要看text的值 函数: driver.findElementByAndroidUIAutomator(“text(\”中文\”)”); Accessibility ID定位 函数: driver.findElementByAccessibilityId(content-desc的值); Xpath定位 主要看目标元素的位置 函数:findElementByXPath(路径) findElementByXPath(“//*[@resource-id=‘xxx’]”)click() 点击 sendKeys() 模拟键盘输入 clear() 清除 Getattribute() 获取属性值
针对应用的操作 如应用的安装、卸载、关闭、启动等。
1)安装应用:installApp(); 2)卸载应用:removeApp(); 3)关闭应用:closeAPP(); 4)启动应用:launchApp() 5)检查应用是否安装:isAppInstalled() 6)将应用置于后台:runAppInBackground() 7)应用重置:resetApp()
模拟手势操作
import io.appium.java_client.TouchAction; //要生成对象 TouchAction touchAction = new TouchAction(driver);1)按压控件:press() 2)长按控件:longPress() 3)点击控件:tap() 4)移动:moveTo() 5)暂停:wait() 6)结束操作:release() 7)将操作发送服务器:Perform()
移动设备特有的操作
1)息屏: lockDevice() 2)当前Activity:currentActivity() 3)收起键盘:hideKeyboard() 4)滑动:swipe() 5)拉出文件:pullFile() 6)推送文件:pushFile()
定位、获取元素,QQ登录和退出
package haha; import io.appium.java_client.AppiumDriver; import java.net.MalformedURLException; import java.net.URL; import org.openqa.selenium.remote.DesiredCapabilities; public class Test01 { public static void main(String[] args)throws MalformedURLException, InterruptedException { // TODO 自动生成的方法存根 DesiredCapabilities des=new DesiredCapabilities(); des.setCapability("platformName","android");//平台 des.setCapability("deviceName","Android Emulator");//设备名称 des.setCapability("platformVersion","4.4.4");//版本号 des.setCapability("noReset","true");//不要重复安装 des.setCapability("appPackage","com.tencent.mobileqq");//包名 Thread.sleep(3000);//3秒 //活动页 des.setCapability("appActivity","com.tencent.mobileqq.activity.LoginActivity"); Thread.sleep(7000);//7秒 //创建连接appium的对象 AppiumDriver dr = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"),des); //获取元素 String str=dr.findElementById("com.tencent.mobileqq:id/btn_login").getAttribute("text"); System.out.println(str);//id方法 Thread.sleep(3000);//3秒 String str2=dr.findElementByClassName("android.widget.Button").getAttribute("text"); System.out.println(str2);//name方法 Thread.sleep(6000);//5秒 String str3=dr.findElementByAndroidUIAutomator("text(\"登录\")").getAttribute("text"); System.out.println(str3);//text方法 Thread.sleep(3000);//3秒 String xp="/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.LinearLayout/android.widget.Button[2]"; dr.findElementByXPath(xp).click();//xpath方法 Thread.sleep(5000);//5秒 String str1=dr.findElementByAccessibilityId("请输入QQ号码或手机或邮箱").getAttribute("text"); System.out.println(str1);//content-desc方法 // Thread.sleep(5000);//3秒 // dr.findElementById("com.tencent.mobileqq:id/btn_login").click(); Thread.sleep(3000);//3秒 dr.findElementByAccessibilityId("请输入QQ号码或手机或邮箱").click();//点击元素 Thread.sleep(3000);//3秒 dr.findElementByAccessibilityId("请输入QQ号码或手机或邮箱").clear();//清除元素 dr.findElementByAccessibilityId("请输入QQ号码或手机或邮箱").sendKeys("302650100"); Thread.sleep(3000);//3秒 dr.findElementById("com.tencent.mobileqq:id/password").click(); Thread.sleep(3000);//3秒 dr.findElementById("com.tencent.mobileqq:id/password").clear(); dr.findElementById("com.tencent.mobileqq:id/password").sendKeys("wx3289696");//输入值 Thread.sleep(3000);//3秒 dr.findElementById("com.tencent.mobileqq:id/login").click(); Thread.sleep(3000);//3秒 dr.swipe(123,564,123,200,5000); Thread.sleep(8000);//3秒 //点击头像 dr.findElementById("com.tencent.mobileqq:id/conversation_head").click(); //点击设置 Thread.sleep(5000); dr.findElementById("com.tencent.mobileqq:id/settings").click(); //点击我的账户 Thread.sleep(5000); dr.findElementById("com.tencent.mobileqq:id/account_switch").click(); Thread.sleep(5000); //点击退出当前账户 dr.findElementByAndroidUIAutomator("text(\"退出当前账号\")").click(); Thread.sleep(5000); dr.quit(); } }