我了解的自动化测试有两种,
一种是点击屏幕,进行随机测试。
另一种是获取指定的事件,进行耐久性测试,比如点击1000次,用手点击测试是不可行的,比如反复点击某个按钮会不会崩溃。测试固定的位置。
这份代码主要是进行摄像头耐久性测试的,只是个简单的demo,先贴代码,整个文件可以github链接找
public class SampleTest {private AndroidDriver<AndroidElement> driver;@Beforepublic void setUp() throws Exception {DesiredCapabilities capabilities = new DesiredCapabilities();capabilities.setCapability("platformName","Android");capabilities.setCapability("deviceName","0123456789ABCDEF");capabilities.setCapability("udid","0123456789ABCDEF");capabilities.setCapability("platformVersion", "4.4");capabilities.setCapability("appPackage", "com.jdcn.sample");capabilities.setCapability("noReset",true);capabilities.setCapability("appActivity", "com.jdcn.sdk.ui.MainActivity");driver = new AndroidDriver<AndroidElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);}@Afterpublic void tearDown() throws Exception {driver.quit();}@Testpublic void appDemo() throws InterruptedException {for (int i = 1; i <= 2000; i++) {System.out.println("第---"+i+"---次");sleep(500);//driver.findElementByName("启动人脸检测").click();driver.findElementById("com.jdcn.sample:id/btn_StartDetect").click();sleep(3000);driver.pressKeyCode(4);}}
}
传github传不上去,可能是公司做了限制,可以在下载资源文件里边下载
另外一个java脚本
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.remote.DesiredCapabilities;import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;/*** Created by zhaowei20 on 2018/7/15.*/
public class Fresh_TestSDK20180926 {public static AndroidDriver<AndroidElement> driver = null;public static void main(String[] args) {//设置APK路径File classpathRoot = new File(System.getProperty("user.dir"));File appDir = new File(classpathRoot, "apps");File app = new File(appDir, "app-debug.apk");//apk名称//设置自动化相关参数DesiredCapabilities capabilities = new DesiredCapabilities();capabilities.setCapability("platformName", "Android");capabilities.setCapability("deviceName", "10.13.109.182:5555");capabilities.setCapability("udid", "10.13.109.182:5555");//capabilities.setCapability("platformVersion","7.0");capabilities.setCapability("noReset", true);capabilities.setCapability("app", app.getAbsolutePath());capabilities.setCapability("appPackage", "com.hjimi.colordepth");capabilities.setCapability("appActivity", "com.hjimi.colordepth.MainActivity");try {driver = new AndroidDriver<AndroidElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);} catch (MalformedURLException e) {e.printStackTrace();}for (int i = 0; i < 500; i++) {try {Thread.sleep(1000 * 3);/* By by = new By.ById("btn_StartDetect");//By by2 = new By.ById("iv_fp_layout_switch_pay_close");if (!isElementExist(driver,by)) {driver.pressKeyCode(4);Thread.sleep(1000 * 1);}*/driver.findElementById("btn_StartDetect").click();System.out.println(i);Thread.sleep(1000 * 5);takeScreenShot(driver,"screenshot" + System.currentTimeMillis()+".jpg");driver.pressKeyCode(4);} catch (InterruptedException e) {e.printStackTrace();}}}public static void takeScreenShot(TakesScreenshot driver, String fileName) {String folderPath = "D:\\7fresh稳定性截图\\screenshotSDK-20180926-中崎";File screenShotFile = driver.getScreenshotAs(OutputType.FILE);String filePath = folderPath + "\\" + fileName;try {FileUtils.copyFile(screenShotFile,new File(filePath));} catch (IOException e) {e.printStackTrace();}}public static boolean isElementExist(AndroidDriver<AndroidElement> driver,By by) {try {driver.findElement(by);return true;}catch(Exception e) {return false;}}
}