一, 摘要
元素定位本篇主要介绍如何使用 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)
- // Java
- package testscript;/*
- * @FileName testscript.Test_Calculator:
- * @author davieyang
- * @create 2018-11-20 11:02
- */
- import org.apache.log4j.xml.DOMConfigurator;
- import java.NET.MalformedURLException;
- import java.NET.URL;
- import java.util.concurrent.TimeUnit;
- import org.openqa.selenium.By;
- import org.openqa.selenium.remote.DesiredCapabilities;
- import org.testng.Assert;
- import org.testng.annotations.BeforeTest;
- import org.testng.annotations.DataProvider;
- import org.testng.annotations.Test;
- import io.appium.java_client.Android.AndroidDriver;
- public class Test_Calculator {
- static {
- // 指定 log4j 配置文件为 log4j.xml
- DOMConfigurator.configure("log4j.xml");
- }
- AndroidDriver driver;
- @BeforeTest
- public void setUp() throws MalformedURLException{
- DesiredCapabilities caps = new DesiredCapabilities();
- // des.setCapability("app", "c:\\");
- caps.setCapability("automationname", "Appium");
- caps.setCapability("platformName", "Android");
- caps.setCapability("platformVersion", "23");
- caps.setCapability("udid", "WTKDU17105005171");
- caps.setCapability("deviceName", "Honor");
- caps.setCapability("appPackage", "com.tencent.qqpimsecure");//com.Android.contacts
- caps.setCapability("appActivity", "com.tencent.server.fore.QuickLoadActivity");//.activities.PeopleActivity
- caps.setCapability("appWaitActivity", "com.tencent.server.fore.QuickLoadActivity");
- caps.setCapability("unicodeKeyboard", "True");
- caps.setCapability("resetKeyboard", "True");
- caps.setCapability("newCommandTimeout", "15");
- caps.setCapability("nosign", "True");
- driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),caps);
- driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
- }
- @Test
- public void add() {
- driver.findElement(By.xpath("com.tencent.mobileqq:id/btn_login")).click();
- }
六, 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\")");
十一, 其他定位方式
来源: https://www.cnblogs.com/davieyang/p/10063549.html