1. 程式人生 > >Windows環境下使用uiautomatorviewer進行元素定位

Windows環境下使用uiautomatorviewer進行元素定位

一、摘要

元素定位本篇主要介紹如何使用uiautomatorviewer,通過定位到頁面上的元素,然後進行相應的點選等操作,uiautomatorviewer 是 android-sdk 自帶的一個元素定位工具,非常簡單好用,使用 uiautomatorviewer,你可以檢查一個應用的 UI 來檢視應用的佈局和元件以及相關的屬性

在WIndows系統上進行元素定位,建議使用這個工具,在Mac上建議用Appium Inspector

二、啟動uiautomatorviewer.bat

雙擊啟動

三、連結手機

  • 確認連線手機狀態正常-》開啟手機qq頁面,讓螢幕處於點亮狀態

  • 點左上角安卓機器人按鈕 Devices Screenshot 按鈕重新整理頁面

四、定位元素

五、點選登入按鈕程式碼例項

如上圖所示,定位了登陸按鈕,程式碼例項如下

# python
'''
@Time : 2018/11/12 13:37
@Author :
@Email :
@File :
@Software: PyCharm
@Description:
'''
# encoding = utf-8
from appium import webdriver
import time
import unittest


class test_ClickButon(unittest.TestCase):
def setUp(self):
desired_caps = {
'platformName': 'Android',
'deviceName': '30d4e606',
'platformVersion': '5.0',
'appPackage': 'com.tencent.qqpimsecure',
'appActivity': 'com.tencent.server.fore.QuickLoadActivity'
}
self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)

def test_ClickButton(self):
driver = self.driver
driver.find_element_by_id("com.tencent.mobileqq:id/btn_login").click()
time.sleep(5)
 1 // Java
 2 package testscript;/*
 3  * @FileName testscript.Test_Calculator:
 4  * @author davieyang
 5  * @create 2018-11-20 11:02
 6  */
 7 import org.apache.log4j.xml.DOMConfigurator;
 8 import java.net.MalformedURLException;
 9 import java.net.URL;
10 import java.util.concurrent.TimeUnit;
11 
12
import org.openqa.selenium.By; 13 import org.openqa.selenium.remote.DesiredCapabilities; 14 import org.testng.Assert; 15 import org.testng.annotations.BeforeTest; 16 import org.testng.annotations.DataProvider; 17 import org.testng.annotations.Test; 18 19 import io.appium.java_client.android.AndroidDriver; 20 21 public class Test_Calculator { 22 static { 23 //指定log4j配置檔案為log4j.xml 24 DOMConfigurator.configure("log4j.xml"); 25 } 26 AndroidDriver driver; 27 @BeforeTest 28 public void setUp() throws MalformedURLException{ 29 DesiredCapabilities caps = new DesiredCapabilities(); 30 // des.setCapability("app", "c:\\"); 31 caps.setCapability("automationname", "Appium"); 32 caps.setCapability("platformName", "Android"); 33 caps.setCapability("platformVersion", "23"); 34 caps.setCapability("udid", "WTKDU17105005171"); 35 caps.setCapability("deviceName", "Honor"); 36 caps.setCapability("appPackage", "com.tencent.qqpimsecure");//com.android.contacts 37 caps.setCapability("appActivity", "com.tencent.server.fore.QuickLoadActivity");//.activities.PeopleActivity 38 caps.setCapability("appWaitActivity", "com.tencent.server.fore.QuickLoadActivity"); 39 caps.setCapability("unicodeKeyboard", "True"); 40 caps.setCapability("resetKeyboard", "True"); 41 caps.setCapability("newCommandTimeout", "15"); 42 caps.setCapability("nosign", "True"); 43 driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),caps); 44 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 45 } 46 @Test 47 public void add() { 48 driver.findElement(By.xpath("com.tencent.mobileqq:id/btn_login")).click(); 49 }

六、Finding Elements By ID

 

WebElement digit_5 = driver.findElement(By.id("com.android.calculator2:id/digit5"));

七、Finding Elements By Name

WebElement delete = driver.findElement(By.name("DELETE"));

八、Finding Elements By CLASSNAME

WebElement editBox = driver.findElement(By.className("android.widget.EditText"));

If the same class is used for multiple elements, then we need to select an element on the basic of indexing. For example:

List<WebElement>editBox = driver.findElements(By.className("android.widget.Button"));

editBox.get(1).click();

九、Finding Elements By AccessibilityID

WebElement plusSign=driver.findElementByAccessibilityId("plus");

 十、Finding Elements By AndroidUIAutomator

findElement(By.AndroidUIAutomator(String UIAuto));

WebElement equal = driver.findElementByAndroidUiAutomator("new UiSelector().resourceId(\"com.android.calculator2:id/equal\")")

WebElement equal = driver.findElementByAndroidUiAutomator("new UiSelector().description(\"equals\")");

 十一、其他定位方式