由于最近忙于mirage项目,在分析客户的一个问题时,客户提到了使用TestNG来进行一些压力测试,比如连续拍照500次以上,不断的进入设置、滑动、退出200次。 当时咨询了下项目里的测试同僚,发现我司在压力测试方面做得比较少,性能测试方面主要使用的monkey测试。无奈,只有自己学习琢磨了。
本文一来用于记录学过的东西,二来也可以给会用到这个工具的测试或开发同事借鉴。
通过这个工具,开发同事可以验证自己模块是否存在问题。
PS:如果文字有不正确的地方,请务必帮忙纠正。
下载地址:http://www.oracle.com/technetwork/java/javase/downloads/index.html 注意:x64是64位的,如果电脑是32位的,请到官网下载Windows x86 jdk-7u79-windows-i586.exe 配置环境变量 1.找到jdk安装目录,如D:\Program Files\Java\jdk1.7.0_51\bin 2.计算机->右键属性->高级系统设置->环境变量,找到系统变量中的Path变量,将上面的目录添加到变量值中,切记不要误删掉其他值,每个值之间使用;隔开。保存。 3.打开cmd控制台,执行java -version,确定版本安装正确
将sdk目录配置到环境变量中,如配置jdk一样
下载地址:https://bitbucket.org/appium/appium.app/downloads/
下载地址:http://pan.baidu.com/s/1jGy588I
1.选择 Help / Install new software 2.输入 http://beust.com/eclipse 后选择TestNG,安装 3.安装成功后,依次选择Wondow->Show View->找到TestNG(JAVA下)->OK
File->New->Java Project,输入项目名称,完成 右键项目名称->Build Path->Configure Build Path 选择Libraries->Add External JARs...,将从服务器上拷贝下来的testng下的包全部导入
eg1:编写连续拍照500次 - TestCamera.java
package com.ckt.test;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import java.net.URL;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class TestCamera {
private AppiumDriver mAppiumDriver;
Scanner mScanner;
int num = 500; //执行测试500次
@BeforeTest
public void setUp() throws Exception {
/** 动态输入执行次数,需要动态输入就将注释去掉
System.out.println("Please enter the no of times you want to capture picture :");
mScanner = new Scanner(System.in);
num = mScanner.nextInt();
*/
// Set up desired capabilities and pass the Android app-activity and app-package to Appium
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(MobileCapabilityType.BROWSER_NAME, "Android");
capabilities.setCapability(MobileCapabilityType.VERSION, "4.4.2"); //sdk platform version
capabilities.setCapability(MobileCapabilityType.PLATFORM, "Windows");
capabilities.setCapability("deviceName", "0123456789ABCDEF"); //adb devices,devices name
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("newCommandTimeout", 120);
//test package name ,有源码的话,直接看AndroidManifest.xml
//cmd控制台 输入 uiautomatorviewer,启动相机,点击uiautomatorviewer 上的Device Screenshot
//在uiautomatorviewer右侧Note Detail中会显示当前应用包名
capabilities.setCapability("appPackage", "org.codeaurora.snapcam");
//执行adb shell dumpsys activity|grep -A 5 "Running activities",查找当前运行的activity
//没有grep的windows,执行adb shell dumpsys activity activities > d:\activity.txt,搜索Running activities
capabilities.setCapability("appActivity", "com.android.camera.CameraActivity");
mAppiumDriver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
mAppiumDriver.manage().timeouts().implicitlyWait(360, TimeUnit.SECONDS);
}
@Test
public void capturing() throws Exception {
for (int i = 0; i < num; i++) {
//点击uiautomatorviewer左侧相机按钮,右侧Note Detail中的resource-id会显示相机拍照按钮element
mAppiumDriver.findElement(By.id("org.codeaurora.snapcam:id/shutter_button")).click();
Thread.sleep(2000);
System.out.println("TestCamera cases " + (i+1) + " successfully executed");
}
}
@AfterTest
public void quit() throws Exception
{
if (mAppiumDriver != null) {
mAppiumDriver.quit();
}
}
}
package com.ckt.test;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import java.net.URL;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class TestSettings {
private AppiumDriver mAppiumDriver;
Scanner mScanner;
@BeforeTest
public void setUp() throws Exception
{
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(MobileCapabilityType.BROWSER_NAME, "Android");
capabilities.setCapability(MobileCapabilityType.VERSION, "4.4.2");
capabilities.setCapability(MobileCapabilityType.PLATFORM, "Windows");
capabilities.setCapability("deviceName", "0123456789ABCDEF");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("appPackage", "com.android.settings");
capabilities.setCapability("appActivity", "com.android.settings.Settings");
mAppiumDriver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
mAppiumDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
}
@Test
public void settings() throws Exception {
Thread.sleep(1000);
mAppiumDriver.swipe(661, 1105, 597, 50, 0);
mAppiumDriver.swipe(661, 1105, 597, 50, 0);
mAppiumDriver.swipe(661, 1105, 597, 50, 0);
mAppiumDriver.swipe(661, 1105, 597, 50, 0);
mAppiumDriver.swipe(599, 300, 661, 1105, 0);
mAppiumDriver.swipe(599, 300, 661, 1105, 0);
mAppiumDriver.swipe(599, 300, 661, 1105, 0);
mAppiumDriver.swipe(599, 300, 661, 1105, 0);
System.out.println("TestSettings successfully executed");
}
@AfterTest
public void quit() throws Exception {
if (mAppiumDriver != null) {
mAppiumDriver.quit();
}
}
}
下载地址:http://pan.baidu.com/s/1pJFWvLh 下载到导入eclipse,java项目,导入必要的包后即可运行
运行安装好的Appium,点击右上角三角形按钮启动服务
选择测试类,如TestCamera,右键Run As->TestNG Test
选择批量配置文件,如ExceteSetting.xml,右键Run As->TestNG TestNG
执行情况会显示在Console中
将以下内容放入capture.sh中,chmod +x capture.sh,添加可执行权限
#!/bin/bash
for((i=1;i<1000;i++))
do
adb shell input tap 550 1800
echo $i
sleep 2
done
连接设备,执行./capture.sh即可
以上方法采用adb shell input模拟输入事件实现,(550,1800)是相机拍照按钮在屏幕上的坐标。分辨率不同就需要重新修改
http://testng.org/doc/index.html