一、實用xpath定位,定位菜單框中的5個元素
表達式: //android.widget.HorizontalScrollView/*/android.support.v7.app.a$c
但是在appium中無法識別“$”,要改爲點“.”或者星“*”,所以要改爲: //android.widget.HorizontalScrollView/*/*
//獲取菜單通過xpath:
public void clickMenuByXpath(int index){
driver.findElements(By.xpath("//android.widget.HorizontalScrollView/*/*")).get(index).click();
//driver.findElement(By.xpath("//android.widget.HorizontalScrollView/*/*["+index+"]")).click();兩種寫法
}
//元素上滑動
public void elementFromSwipe() throws Exception{
clickMenuByXpath(2);
//直接使用xpath定位第3個元素
//driver.findElement(By.xpath("//android.widget.HorizontalScrollView/*/*[2]")).click();
AndroidElement element = driver.findElementById("com.zhihu.android:id/recycler_view");
AppiumUtils aUtils=new AppiumUtils(driver);
for(int i=0;i<3;i++){
Thread.sleep(1000);
aUtils.swipeOnElement(element, "right", 10, 10, 500);
}
System.out.println("滑動成功");
}
二、進入設置——》滑屏找到賬戶與安全——》找到新浪微博,並根據新浪微博找到開關,並對開關進行操作
//社交賬戶通過xpath定位,如通過新浪微博定位開關
public void useSocialAccount() throws Exception{
clickMenuByXpath(4);
Thread.sleep(1000);
driver.findElement(By.xpath("//*[@text='設置']")).click();
System.out.println("打開設置成功");
//因爲賬戶與安全在設置的最下面需要翻動一屏,所以先判斷是否存在,不存在滑動屏幕,爲了防止死循環找10次找不到就算了
AppiumUtils appiumUtils=new AppiumUtils(driver);
boolean flag=appiumUtils.isElementExist(By.xpath("//*[@text='帳號與安全']"));
int conut=10;
while (conut>0) {
if(flag){
driver.findElement(By.xpath("//*[@text='帳號與安全']")).click();
conut=-1;//找到直接結束
}else {
appiumUtils.swtipe("up", 1500);
flag=appiumUtils.isElementExist(By.xpath("//*[@text='帳號與安全']"));
conut--;
}
}
//判斷這個是否開關
AndroidElement element= driver.findElement(By.xpath("//*[@text='新浪微博']/../following-sibling::*[1]/android.widget.Switch"));
String checkedOld=element.getAttribute("checked");
element.click();
String checkedNew=element.getAttribute("checked");
if(checkedOld.equals(checkedNew)){
System.out.println("狀態切換失敗");
//添加失敗截圖
File file=driver.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(file,new File("images/useSocialAccount.png"));//創建images
}else {
if(checkedNew.equals("true")){
System.out.println("打開新浪微博成功");
File file=driver.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(file,new File("images/openuseSocialAccount.png"));//創建images
}else {
System.out.println("關閉新浪微博成功");
File file=driver.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(file,new File("images/closeuseSocialAccount.png"));//創建images
}
}
}
下面的例子也適用這種情況:
//消息推送設置,通過xpath定位,通過知乎特別推薦定位開關,並操作
public void messagePush() throws Exception{
clickMenuByXpath(4);
Thread.sleep(1000);
driver.findElement(By.xpath("//*[@text='設置']")).click();
System.out.println("進入推送消息設置成功");
//因爲賬戶與安全在設置的最下面需要翻動一屏,所以先判斷是否存在,不存在滑動屏幕,爲了防止死循環找10次找不到就算了
AppiumUtils appiumUtils=new AppiumUtils(driver);
boolean flag=appiumUtils.isElementExist(By.xpath("//*[@text='推送消息設置']"));
int conut=10;
while (conut>0) {
if(flag){
driver.findElement(By.xpath("//*[@text='推送消息設置']")).click();
conut=-1;//找到直接結束
}else {
appiumUtils.swtipe("up", 1500);
flag=appiumUtils.isElementExist(By.xpath("//*[@text='推送消息設置']"));
conut--;
}
}
//判斷這個是否開關
AndroidElement element= driver.findElement(By.xpath("//*[@text='知乎特別推薦']/../following-sibling::*[1]/android.widget.CheckBox"));
String checkedOld=element.getAttribute("checked");
element.click();
String checkedNew=element.getAttribute("checked");
if(checkedOld.equals(checkedNew)){
System.out.println("勾選失敗");
//添加失敗截圖
File file=driver.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(file,new File("images/messagePush.png"));//創建images
}else {
if(checkedNew.equals("true")){
System.out.println("打開勾選成功");
File file=driver.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(file,new File("images/messagePush.png"));//創建images
}else {
System.out.println("關閉成功");
File file=driver.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(file,new File("images/messagePush.png"));//創建images
}
}
}