概述
生成当前时间:
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(df.format(new Date()));
生成随机数:
生成随机数:
Random ra = new Random( );
int i = ra.nextInt( 10 ); // 生成10以内的随机数
将String转换为int:
int count = Integer.parseInt(dynamic_count.getAttribute("text")); // dynamic_count的text属性,string类型转为int
UiAutomator之输入各功能键
UiDevice.getInstance().pressBack(); // 按返回键
UiDevice.getInstance().pressHome(); // 按Home键
UiDevice.getInstance().pressMenu(); // 按菜单键
UiDevice.getInstance().pressRecentApps(); // 按最近使用app列表
UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_A); // 输入小写的a (注:输入的时候,如果键盘被调出则输入不上来)
UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_B, 1); // 输入大写的B
点击屏幕中央:
int height = UiDevice.getInstance().getDisplayHeight();
int width = UiDevice.getInstance().getDisplayWidth();
UiDevice.getInstance().click(width/2, height/2);
点击指定的坐标:
UiDevice.getInstance().click(270, 1230);
找出某个元素的矩阵坐标:
UiObject qq = new UiObject(new UiSelector().text("QQ"));
Rect r=qq.getBounds();
int x0 = r.left; // 左上角
int y0 = r.top;
int x1 = r.right; // 右下角
int y1 = r.bottom;
System.out.println("[" +x0 + "," + y0 + "]");
System.out.println("[" +x1 + "," + y1 + "]");
int xcenter = r.centerX(); // 元素的x坐标中间
int ycenter = r.centerY(); // 元素的y坐标中间
UiDevice.getInstance().click(xcenter, ycenter); // 点击这个元素的中间位置
找出某个元素的矩阵坐标:
UiObject qq = new UiObject(new UiSelector().text("QQ"));
Rect r=qq.getBounds();
int x0 = r.left; // 左上角
int y0 = r.top;
int x1 = r.right; // 右下角
int y1 = r.bottom;
System.out.println("[" +x0 + "," + y0 + "]");
System.out.println("[" +x1 + "," + y1 + "]");
int xcenter = r.centerX(); // 元素的x坐标中间
int ycenter = r.centerY(); // 元素的y坐标中间
UiDevice.getInstance().click(xcenter, ycenter); // 点击这个元素的中间位置
使用多个属性值,查找元素:
UiObject fileList = new UiObject(new UiSelector()
.resourceId("com.sec.android.app.myfiles:id/text1")
.text("Download"));
fileList.click();
UiObject fileList = new UiObject(new UiSelector()
.resourceId("com.sec.android.app.myfiles:id/text1")
.text("Download"));
fileList.click();
输入文本,清除文本:
UiObject search_text = new UiObject(new UiSelector().resourceId("android:id/search_src_text"));
search_text.setText("1234567890"); // 输入文本
search_text.clearTextField(); // 清除文本
UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_MOVE_END); // 定位到最后,输入一个end键
UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_DEL); // 输入一个删除键
search_text.setText("1234567890"); // 输入文本
search_text.clearTextField(); // 清除文本
UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_MOVE_END); // 定位到最后,输入一个end键
UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_DEL); // 输入一个删除键
两手指拉动收缩和放开:
UiObject pic = new UiObject(new UiSelector().resourceId("com.sec.android.gallery3d:id/gl_root_view"));
pic.pinchIn(20, 5); // 缩小20%
pic.pinchOut(20, 10); // 外拉20%
pic.pinchIn(20, 5); // 缩小20%
pic.pinchOut(20, 10); // 外拉20%
模拟两个手指手势:
Point startPoint1, startPoint2, endPoint1, endPoint2;
startPoint1 = new Point();
startPoint2 = new Point();
endPoint1 = new Point();
endPoint2 = new Point();
startPoint1.x=57; startPoint1.y=400;
startPoint2.x=57; startPoint2.y=900;
endPoint1.x=600; endPoint2.y=400;
endPoint2.x=600; endPoint2.y=1000;
pic.performTwoPointerGesture(startPoint1, startPoint2, endPoint1, endPoint2, 5);
startPoint1 = new Point();
startPoint2 = new Point();
endPoint1 = new Point();
endPoint2 = new Point();
startPoint1.x=57; startPoint1.y=400;
startPoint2.x=57; startPoint2.y=900;
endPoint1.x=600; endPoint2.y=400;
endPoint2.x=600; endPoint2.y=1000;
pic.performTwoPointerGesture(startPoint1, startPoint2, endPoint1, endPoint2, 5);
判断对象是否存在:
objectxx.waitForExists(2000)
objectxx.exist()
如果黑屏,则点亮屏幕
if(!UiDevice.getInstance().isScreenOn()){
UiDevice.getInstance().wakeUp();
}
UiDevice.getInstance().wakeUp();
}
Selenium 刷新页面:
driver.navigate.refresh();
Selenium最大化窗口:
driver.manage().window().maximize();
Selenium通过cssSelector来查找元素:
WebElement chat = wd.findElement(By.cssSelector("div[class='chat_item slide-left ng-scope']"));
向上向下按键:
(new Actions(wd)).sendKeys(Keys.ARROW_DOWN).build().perform();
(new Actions(wd)).sendKeys(Keys.ARROW_UP).build().perform();
遍历map:
for(String key : map.keySet())
{ System.out.println(key + " : " + map.get(key)); }
最后
以上就是老迟到宝贝为你收集整理的Java自动化测试之测试常用功能集集锦(UiAutomator、Selenium等)的全部内容,希望文章能够帮你解决Java自动化测试之测试常用功能集集锦(UiAutomator、Selenium等)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复