appium元素定位

來源:互聯網
上載者:User

標籤:contain   匹配   resource   and   沒有   一個   功能   sele   androidui   

通過id定位

安卓裡面的id即為resource-id

driver.findElementById("com.tencent.mm:id/do")

或者

WebElement element = driver.findElement(By.id("com.tencent.mm:id/do"));

 

通過name定位

一般text為name

WebElement el = driver.findElement(By.name("暱稱"));

 

通過classname定位

Classname即為uiautomator裡面的class資訊

WebElement el = driver.findElement(By.ClassName("android.widget.TextView"));

 

通過xpath定位

在appium中xpath定位元素,執行很慢,效率比較低,很少用

WebElement el = driver.findElement(By.xpath("//android.widget.Button[@index=‘1‘]"));

 

通過accessibilityID定位

在uiautomator中的content-desc資訊為該定位元素

WebElement el = driver.findElementByAccessibilityId("sharebutton");

 

通過androiduiautomator定位元素
AndroidUIAutomator是一個強有力的元素定位方式,它是通過Android UIAutomator類庫去找元素,
findElement(By.AndroidUIAutomator(String UIAuto));

可以選項id,classname,name,description作為傳入的字串

WebElement el = driver.findElementByAndroidUIAutomator("new UiSelector().description(\"sharebutton\")");

Text屬性的方法,uiautomator中的text資訊

driver.find_element_by_android_uiautomator(‘new UiSelector().text("Custom View")‘).click()         #text
driver.find_element_by_android_uiautomator(‘new UiSelector().textContains("View")‘).click()        #textContains
driver.find_element_by_android_uiautomator(‘new UiSelector().textStartsWith("Custom")‘).click()    #textStartsWith
沒有endswith方法
driver.find_element_by_android_uiautomator(‘new UiSelector().textMatches("^Custom.*")‘).click()    #textMatches
使用Regex必須完全符合,比如上面使用”^Custom”是不正確的

classname屬性的方法

#className
driver.find_element_by_android_uiautomator(‘new UiSelector().className("android.widget.TextView").text("Custom View")‘).click()    
#classNameMatches
driver.find_element_by_android_uiautomator(‘new UiSelector().classNameMatches(".*TextView$").text("Custom View")‘).click()   

resourceID屬性的方法

#resourceId
driver.find_element_by_android_uiautomator(‘new UiSelector().resourceId("android:id/text1")‘)   
#resourceIdMatches
driver.find_element_by_android_uiautomator(‘new UiSelector().resourceIdMatches(".*id/text1$")‘)

通過contentdescription定位

driver.find_element_by_android_uiautomator(‘new UiSelector().description("Custom View")‘).click()         
driver.find_element_by_android_uiautomator(‘new UiSelector().desctiptionContains("View")‘).click()        
driver.find_element_by_android_uiautomator(‘new UiSelector().descriptionStartsWith("Custom")‘).click()   
driver.find_element_by_android_uiautomator(‘new UiSelector().descriptionMatches("^Custom.*")‘).click()   

元素的其他屬性的方法

driver.find_element_by_android_uiautomator(‘new UiSelector().clickable(true).text("Custom View")‘).click() 

通過偽xpath方法

通過UiSelector.fromParent或UiObject.getFromParent方法

快速尋找已知元素的父元素的兄弟資訊

他們的功能是一樣的

Getfromparent方法

  1. save =  new UiObject(new UiSelector().text("Save"));  
  2. assertEquals(save.getText(),"Save");  
  3. delete = save.getFromParent(new UiSelector().text("Delete"));  
  4. assertEquals(delete.getText(),"Delete"); 

promparent方法

  1. delete = new UiObject(new UiSelector().text("Save").fromParent(new UiSelector().text("Delete")));  
  2. assertEquals(delete.getText(),"Delete");  

通過UiSelector.childSelector或UiObject.getChild方法

快速尋找在已知元素的子項目

Getchild方法

  1. UiObject parentView = new UiObject(new UiSelector().className("android.view.View"));  
  2. save = parentView.getChild(new UiSelector().text("Save"));  
  3. assertEquals(save.getText(),"Save");  

childselector方法

  1. save = new UiObject(new UiSelector().className("android.view.View").childSelector(new UiSelector().text("Save")));  
  2. assertEquals(save.getText(),"Save"); 

appium元素定位

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.