appium 处理动态控件

时间:2024-08-24 10:36:50

环境怎么搭建,参考:http://www.cnblogs.com/tobecrazy/p/4562199.html

知乎Android客户端登陆:http://www.cnblogs.com/tobecrazy/p/4579631.html

appium实现截图和清空EditText:http://www.cnblogs.com/tobecrazy/p/4592405.html

appium处理滑动:http://www.cnblogs.com/tobecrazy/p/4612133.html

最近有人问我appium能否处理动态生成的控件,由于以没做过,不敢肯定,所以就自己谢了一个测试Demo

由于自己不小心appiumDemo打错写为apppiumDemo

第一步: 创建Android project 在onCreate方法里添加一个button控件

 Button bt = new Button(this);
bt.setText("Test");
bt.setPadding(300, 0, 0, 0); RelativeLayout relativeLayout = (RelativeLayout) getLayoutInflater()
.inflate(R.layout.activity_main, null);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, -1);
relativeLayout.addView(bt, layoutParams);
setContentView(relativeLayout);

下载地址:http://pan.baidu.com/s/1o6mokLO

第二步:使用uiautomatorviewer获取控件

appium 处理动态控件

其实动态生成的控件和使用xml的一样,只不过没有添加resource-id

第三部: 获取控件

a.使用 findByClassName

 driver.findElementByClassName("android.widget.Button");

b.使用xpath

driver.findElementByXPath("//android.widget.Button");

第三部:使用appium验证

 package com.dbyl.core;

 import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test; import io.appium.java_client.android.AndroidDriver; import java.io.File;
import java.net.URL;
import java.util.concurrent.TimeUnit; public class AppDemo {
private AndroidDriver driver; @BeforeMethod(alwaysRun = true)
public void setUp() throws Exception {
// set up appium
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("deviceName", "Android Emulator");
capabilities.setCapability("platformVersion", "4.4");
// if no need install don't add this File classpathRoot = new File(System.getProperty("user.dir"));
File appDir = new File(classpathRoot, "apps");
File app = new File(appDir, "apppiumDemo.apk");
capabilities.setCapability("app", app.getAbsolutePath()); capabilities.setCapability("appPackage", "com.example.apppiumdemo");
// support Chinese
capabilities.setCapability("unicodeKeyboard", "True");
capabilities.setCapability("resetKeyboard", "True");
// no need sign
capabilities.setCapability("noSign", "True");
capabilities.setCapability("appActivity", ".MainActivity");
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),
capabilities);
} @AfterMethod(alwaysRun = true)
public void tearDown() throws Exception {
driver.quit();
} @Test(groups = { "Test" })
public void Test() {
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
Assert.assertEquals(driver.findElementByClassName("android.widget.Button").getText(),"Test");
System.out.println(driver.findElementByXPath("//android.widget.Button").getText()); }
}

运行结果:

PASSED:Test

===============================================
Default test
Tests run: 1, Failures: 0, Skips: 0
===============================================

===============================================
Default suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================

总结: 无论何种方式生成的控件,都可以识别,如果没有resource-id可以使用其他方式,比如ByClass ByXpath等