appium 之Xpath使用


@Test(description="絕對定位”)
public void TestCase1() throws InterruptedException{
   Thread.sleep(5000);
   WebElement element=wd.findelement(By.xpath("//android.widget.TextView[contains(@text,'熱門推薦')]"));
   wd.tap(1,element,500);
   Thread.sleep(2000);
   WebElement BackButton=wd.findElement(By.xpath("//android.wdigrt.ImageButton[contains(@content-desc,'Navigate up')]"))
   wd.tap(1,BackButton,500);
   }


   封裝鍵盤上的keyEvent
public interface EnumCollection{
    public class KeyEvent{
      public static int KEYEVENT_MOVETOLAST=123;
      public static int KEYeVENT_DELETEONELETTER=67;
      public static int RETURN_BUTTON=4;
    }


    public class FingerCount{
     public static int ONE_FINGER=1;
     public static int TWO_FINGER=2;
    }


    public class NetWorkStatus{
     public static int FLYMODE=1;
     public static int WIFIMODE=2;
     public static int DATAMODE=4;
     public static int ALLMODE=6;
    }
}
調用封裝方法:
wd.sendKeyEvent(KeyEvent,RETURN_BUTTON);


封裝使用2:
public class DataCeneter{
private static String Category_Head="//android.widgt.TextView[contains(@text,'1$s')]";
public static String StringFormat(String StrText){
return String.format(Category_Head,StrText);
}
}
調用封裝方法:
WebElement element2=wd.findElement(By.xpath(DataCenetr.StringFormat(StrText)));
xpath識別


eg:driver.findElement(By.xpath("//android.widget.TextView[contains(@text,'電影')]/preceding-sibling::android.widget.ImageView[1]"));


driver.findElement(By.xpath("//andriod.support.v7.widget.RecyclerView/child::android.widget.RelativeLayout[1]"));
driver.findElement(By.xpath("//andriod.support.v7.widget.RecyclerView/child::android.widget.RelativeLayout[last()]"));
driver.findElement(By.xpath("//andriod.support.v7.widget.RecyclerView/child::android.widget.RelativeLayout[last()-1]"));
driver.findElement(By.xpath("//android.widget.TextView[contains(@text,'熱門推薦')]/parent::android.widget.RelativeLayout/following-sibling::android.widget.RelativeLayout[1]+"android.widget.TextView[last()]"));


Xpath識別
相對定位-Xpath
1.parent/descendant/following/preceding...
2.
ClassName[location]
3.

UiSelector函數


xpath獲取控件方式
一.通過判斷控件屬性獲取控件
控件的所有屬性都可以用作判斷,例如:text,index,resource_id是否可以clickable等
1.通過文本查看目標控件
WebElement el=driver.findElementByXPath("//android.widgt.TextView[contains(@text,'note2')]");
assertThat(el.getText(),equalsTo("note2"));
driver.find_element_by_xpath("//android.widget.ScrollView/android.widget.RelativeLayout[5]/android.widget.TextView[2]").click()


2.通過index查看目標控件


el = driver.findElementByXPath("//android.widget.TextView[contains(@index,0)]"); assertThat(el.getText(),equalTo("note2"));
el = driver.findElementByXPath("//android.widget.LinearLayout[1]/android.widget.FrameLayout/android.widget.ListView/android.widget.TextView[contains(@index,0)]");
 assertThat(el.getText(),equalTo("note2"));



appium 屏幕滑動
水平:
driver.swipe(x-100,elementLocation.getY(),100,elementLocation.getY(),2000);
垂直:
driver.swipe(driver.manage().window().getSize().getWidth()-100,y,100,y,2000);
public void TestCase3() throws InterruptedException{
WebElement element=null;
try{
//如果找到element
//swipe
//click
}catch{
continue;
}


}
發佈了31 篇原創文章 · 獲贊 6 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章