51Testing软件测试论坛
标题:
appium 处理动态控件
[打印本页]
作者:
马琰的春天
时间:
2018-4-8 09:11
标题:
appium 处理动态控件
最近有人问我appium能否处理动态生成的控件,由于以没做过,不敢肯定,所以就自己谢了一个测试Demo
由于自己不小心appiumDemo打错写为apppiumDemo
第一步: 创建Android project 在onCreate方法里添加一个button控件
复制代码
1 Button bt = new Button(this);
2 bt.setText("Test");
3 bt.setPadding(300, 0, 0, 0);
4
5 RelativeLayout relativeLayout = (RelativeLayout) getLayoutInflater()
6 .inflate(R.layout.activity_main, null);
7 RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
8 ViewGroup.LayoutParams.MATCH_PARENT,
9 ViewGroup.LayoutParams.WRAP_CONTENT);
10 layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, -1);
11 relativeLayout.addView(bt, layoutParams);
12 setContentView(relativeLayout);
复制代码
下载地址:
http://pan.baidu.com/s/1o6mokLO
第二步:使用uiautomatorviewer获取控件
[attach]112803[/attach]
其实动态生成的控件和使用xml的一样,只不过没有添加resource-id
第三部: 获取控件
a.使用 findByClassName
driver.findElementByClassName("android.widget.Button");
b.使用xpath
driver.findElementByXPath("//android.widget.Button");
第三部:使用appium验证
复制代码
1 package com.dbyl.core;
2
3 import org.openqa.selenium.remote.CapabilityType;
4 import org.openqa.selenium.remote.DesiredCapabilities;
5 import org.testng.Assert;
6 import org.testng.annotations.AfterMethod;
7 import org.testng.annotations.BeforeMethod;
8 import org.testng.annotations.Test;
9
10 import io.appium.java_client.android.AndroidDriver;
11
12 import java.io.File;
13 import java.net.URL;
14 import java.util.concurrent.TimeUnit;
15
16 public class AppDemo {
17 private AndroidDriver driver;
18
19 @BeforeMethod(alwaysRun = true)
20 public void setUp() throws Exception {
21 // set up appium
22 DesiredCapabilities capabilities = new DesiredCapabilities();
23 capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
24 capabilities.setCapability("platformName", "Android");
25 capabilities.setCapability("deviceName", "Android Emulator");
26 capabilities.setCapability("platformVersion", "4.4");
27 // if no need install don't add this
28
29 File classpathRoot = new File(System.getProperty("user.dir"));
30 File appDir = new File(classpathRoot, "apps");
31 File app = new File(appDir, "apppiumDemo.apk");
32 capabilities.setCapability("app", app.getAbsolutePath());
33
34 capabilities.setCapability("appPackage", "com.example.apppiumdemo");
35 // support Chinese
36 capabilities.setCapability("unicodeKeyboard", "True");
37 capabilities.setCapability("resetKeyboard", "True");
38 // no need sign
39 capabilities.setCapability("noSign", "True");
40 capabilities.setCapability("appActivity", ".MainActivity");
41 driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),
42 capabilities);
43 }
44
45 @AfterMethod(alwaysRun = true)
46 public void tearDown() throws Exception {
47 driver.quit();
48 }
49
50 @Test(groups = { "Test" })
51 public void Test() {
52 driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
53 Assert.assertEquals(driver.findElementByClassName("android.widget.Button").getText(),"Test");
54 System.out.println(driver.findElementByXPath("//android.widget.Button").getText());
55
56
57 }
58 }
复制代码
复制代码
运行结果:
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等
欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/)
Powered by Discuz! X3.2