http://www.360doc.com/content/15/0114/17/1429048_440734041.shtml 解释常用的类
https://testerhome.com/topics/1047(移动测试基础 通过 python 调用 adb 命令实现用元素名称、id、class 定位元素)
http://blog.chengyunfeng.com/?p=504
手机 adb shell
查看当前窗口的activity:
adb shell(手机模式)
dumpsys activity | grep mFocusedActivity
开启指定的activity,am start -n packageNmae+activityName
点击事件:
import android.widget.LinearLayout;
import com.android.uiautomator.core.UiCollection;
import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiScrollable;
import com.android.uiautomator.core.UiSelector;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;
public class AdbShell extends UiAutomatorTestCase {
// TODO 重要注意: 在运行该测试代码的时候 需要先把手机语言环境设置为英文。
public void testDemo() throws UiObjectNotFoundException {
// 模拟 HOME 键点击事件
getUiDevice().pressHome();
// 现在打开了主屏应用,模拟点击所有应用按钮操作来启动所有应用界面。
// 如果你使用了uiautomatorviewer来查看主屏,则可以发现“所有应用”按钮的
// content-description 属性为“Apps”。可以使用该属性来找到该按钮。
UiObject allAppsButton = new UiObject(new UiSelector().description("Apps"));
// 模拟点击所有应用按钮,并等待所有应用界面起来
allAppsButton.clickAndWaitForNewWindow();
// 在所有应用界面,时钟应用位于Apps tab界面中。下面模拟用户点击Apps tab操作。
// 找到 Apps tab 按钮
UiObject appsTab = new UiObject(new UiSelector().text("Apps"));
// 模拟点击 Apps tab.
appsTab.click();
// 然后在 Apps tab界面,模拟用户滑动到计算器应用的操作。
// 由于Apps界面是可以滚动的,所有用
// UiScrollable 对象.
UiScrollable appViews = new UiScrollable(new UiSelector().scrollable(true));
// 设置滚动模式为水平滚动(默认为垂直滚动)
appViews.setAsHorizontalList();
if (allAppsButton.exists() && allAppsButton.isEnabled()) {
// allAppsButton在当前界面已经不可见了 所以这里不会执行
allAppsButton.click();
}
// 查找计算器应用并点击
UiObject settingsApp = appViews.getChildByText(
new UiSelector().className(android.widget.TextView.class.getName()), "计算器");
settingsApp.clickAndWaitForNewWindow();
// 验证当前显示 的应用包名为计算器
UiObject settingsValidation = new UiObject(new UiSelector().packageName("com.miui.calculator"));
// 如果不存在则出错提示
assertTrue("Unable to detect Clock", settingsValidation.exists());
UiCollection grandLinearLayout = new UiCollection (new UiSelector().className(LinearLayout.class.getName()));
if (grandLinearLayout.getChildCount() > 0) {
//左边集合
UiCollection parent0LinearLayout = (UiCollection) grandLinearLayout.getChild(new UiSelector().index(0));
//清除,退格,除
UiCollection clumn0 = (UiCollection) parent0LinearLayout.getChild(new UiSelector().index(0));
UiCollection clumn1 = (UiCollection) parent0LinearLayout.getChild(new UiSelector().index(1));
UiCollection clumn2 = (UiCollection) parent0LinearLayout.getChild(new UiSelector().index(2));
UiCollection clumn3 = (UiCollection) parent0LinearLayout.getChild(new UiSelector().index(3));
UiCollection clumn4 = (UiCollection) parent0LinearLayout.getChild(new UiSelector().index(4));
UiObject clumn1Object = clumn1.getChild(new UiSelector().index(0));
if (clumn1Object.isClickable()){
clumn1Object.click();
}
//右边集合
UiCollection parent1LinearLayout = (UiCollection) grandLinearLayout.getChild(new UiSelector().index(1));
UiObject clumn0Right = parent1LinearLayout.getChild(new UiSelector().index(0));
UiObject clumn1Right = parent1LinearLayout.getChild(new UiSelector().index(1));
UiObject clumn2Right = parent1LinearLayout.getChild(new UiSelector().index(2));
clumn0Right.isClickable();
clumn0Right.click();
UiObject clumn2Object = clumn2.getChild(new UiSelector().index(0));
clumn2Object.click();
UiCollection clumn3Right = (UiCollection) parent1LinearLayout.getChild(new UiSelector().index(3));
UiObject clumn3Right1 = clumn3Right.getChild(new UiSelector().index(1));
clumn3Right1.click();
}
// 模拟点击返回键
getUiDevice().pressBack();
//description下个应用
UiObject timer = new UiObject(new UiSelector().description("闹钟"));
//开启新窗口
timer.clickAndWaitForNewWindow();
}
}
<target name="install" description="Install the test package">
<exec executable="${adb}" failοnerrοr="true">
<arg line="${adb.device.arg}" />
<arg value="push" />
<arg value="${out.file}" />
<arg value="/data/local/tmp" />
</exec>
</target>
<!--以下需配置 -->
<target name="-testdex" depends="compile, -post-compile">
<dex executable="${dx}"
output="e:/tmp/xx.jar"
nolocals="@{nolocals}"
verbose="${verbose}">
<path path="${out.classes.absolute.dir}"/>
</dex>
</target>
<target name="-testjar" depends="-testdex" >
<jar destfile="${out.file}">
<fileset file="${intermediate.dex.file}" />
</jar>
</target>
<target name="test" description="Runs tests" depends="-testjar">
<!-- todo: fix this
<jar destfile="${out.file}">
<fileset file="${intermediate.dex.file}" />
</jar>-->
<exec executable="${adb}" failοnerrοr="true">
<arg line="${adb.device.arg}" />
<arg value="push" />
<arg value="e:/tmp/xx.jar" />
<arg value="/sdcard/" />
</exec>
<exec executable="${adb}" failοnerrοr="true">
<arg line="${adb.device.arg}" />
<arg value="shell" />
<arg value="uiautomator" />
<arg value="runtest" />
<arg value="/sdcard/xx.jar" />
<arg value="-e" />
<arg value="class" />
<arg value="com.zan.instructions.UpdateAll" />
</exec>
</target>
<!-- com.instructions.Add -->
<!-- com.phonesprite.instructions.ShareToFriends -->
<target name="help">