android自动化测试实例,Study:基于Selenium进行Android客户端自动化测试的例子。

注意:此处省略ADB和Node.js的配置。

代码示例:

package com.hisky.seleniumTest;

import java.io.BufferedReader;

import java.io.File;

import java.io.IOException;

import java.io.InputStreamReader;

import java.net.MalformedURLException;

import java.net.URL;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.List;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.remote.CapabilityType;

import org.openqa.selenium.remote.DesiredCapabilities;

import org.openqa.selenium.remote.RemoteWebDriver;

/**

*

* @author zhenglin.yang

*

*/

public class AndroidTest {

private static WebDriver driver;

public static void main(String[] args) {

killProcessor("node.exe");

killProcessor("adb.exe");

startAppuim();

launchApp();

clickTest();

}

private static void clickTest() {

try {

driver.findElement(By.name("Storage")).click();

Thread.sleep(1000);

driver.findElement(By.name("Backup")).click();

Thread.sleep(1000);

driver.findElement(By.name("Locate")).click();

Thread.sleep(1000);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

private static void launchApp() {

File classPath = new File(System.getProperty("user.dir"));

File app = new File(classPath, "spark-normal-36.98.apk");

DesiredCapabilities capabilities = new DesiredCapabilities();

capabilities.setCapability("device", "Android");

capabilities.setCapability(CapabilityType.VERSION, "4.4.2");

capabilities.setCapability(CapabilityType.BROWSER_NAME, "");

capabilities.setCapability(CapabilityType.PLATFORM, "Windows");

capabilities.setCapability("app", app.getAbsolutePath());

capabilities.setCapability("app-package", "com.asurion.android.spark");

capabilities.setCapability("app-activity", "com.asurion.android.setup.activity.ZeroActivity");

capabilities.setCapability("app-wait-activity", "com.asurion.android.common.ui.actionbar.MainActionBarActivity");

capabilities.setCapability("newCommandTimeout", 1200);

try {

driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);

} catch (MalformedURLException e) {

e.printStackTrace();

}

System.out.println("launching new activity");

}

private static void startAppuim() {

List cmdLine = new ArrayList(Arrays.asList(new String[] { "cmd.exe", "/C", "start", "node", "D:/java/eclipse-automation/Appium/node_modules/appium", "--no-reset" }));

ProcessBuilder pb = new ProcessBuilder(cmdLine);

try {

pb.start();

} catch (IOException e) {

e.printStackTrace();

}

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

e.printStackTrace();

}

System.out.println("node start");

}

private static void killProcessor(String processName) {

try {

Process p = Runtime.getRuntime().exec(System.getenv("windir") + "\system32\tasklist.exe");

BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));

String line = null;

while ((line = br.readLine()) != null) {

if (line.indexOf(processName) != -1) {

String cmd = "taskkill /F /IM " + processName;

Runtime.getRuntime().exec(cmd);

System.out.println("killing processor:" + processName);

}

}

br.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}

注意:

1、Android客户端放在项目路径下:

D:javaworkspacesmyworkspacesseleniumTest-0.0.1-SNAPSHOTspark-normal-36.98.apk

2、Android手机需要设置屏幕始终是亮的状态。

你可能感兴趣的:(android自动化测试实例)