appium 之Xpath使用

来源:互联网 发布:java实例变量 编辑:程序博客网 时间:2024/06/05 06:27

@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;
}


}
0 0
原创粉丝点击