Appium+TestNG实现Android真机自动化测试 安装配置说明

Appium-TestNG自动化测试

安装配置

1.安装JDK,环境配置

2.ADT安装,环境配置

ANDROID_HOME 


Path

 %ANDROID_HOME%\tools;%ANDROID_HOME%\platform-tools;

3.安装Node.js

http://nodejs.org/download/

安装完毕后 cmd输入node –v 输出版本号成功

4.安装appium

cmd输入

npm --registry http://registry.cnpmjs.org install -g appium

安装完成,输入appium-doctor,输出以下内容appium环境配置成功


5.ADT中安装TestNG插件

Help->Install New Software

输入http://beust.com/eclipse 一直next直到安装完成,重启


真机测试

1.     新建测试项目

添加apps文件夹,把要测试的app放在里面


2.     添加Jar包

百度下载

Selenum-server-standalone下载

http://selenium-release.storage.googleapis.com/2.44/selenium-server-standalone-2.44.0.jar

testing下载

https://oss.sonatype.org/content/repositories/snapshots/org/testng/testng/

java-client下载

https://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22com.couchbase.client%22%20AND%20a%3A%22java-client%22


3.     新建测试类

package com.test.demo;

import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidKeyCode;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;

public class MainTest {
	private AndroidDriver driver;

	@BeforeSuite
	public void beforeSuite() throws MalformedURLException {
		// set up appium
		File classpathRoot = new File(System.getProperty("user.dir"));
		File appDir = new File(classpathRoot, "apps");
		File app = new File(appDir, "AndroidApp.apk");// 要测试的App
		DesiredCapabilities capabilities = new DesiredCapabilities();
		capabilities.setCapability("platformName", "Android");
		capabilities.setCapability("deviceName", "kaicom-k7-0123456789ABCDEF");// 真机名
		// capabilities.setCapability("deviceName","xiaomi-mi_2-9ea79c0");//真机名
		capabilities.setCapability("platformVersion", "4.3");// 生成的版本号
		capabilities.setCapability("app", app.getAbsolutePath());
		capabilities.setCapability("appPackage", "com.example.androidapp");// 包名
		capabilities.setCapability("appActivity", ".MainActivity");
		capabilities.setCapability("unicodeKeyboard", "True"); // 输入中文
		capabilities.setCapability("resetKeyboard", "True");
		driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),
				capabilities);
	}

	@AfterSuite
	public void afterSuite() {
		driver.quit();
	}

	@BeforeMethod
	public void beforeMethod() throws Exception {
		System.out.println("beforeMethod");
		// switch (driver.currentActivity()) {
		// case ".MainActivity":
		// mainTest();
		// break;
		// case ".AnotherActivity":
		// anotherTest();
		// break;
		// }
	}

	@AfterMethod
	public void afterMethod() throws Exception {
		System.out.println("afterMethod");
	}

	@Test
	public void test1() throws Exception {
		System.out.println("mainTest");
		int i;
		for (i = 0; i < 4; i++) {
			// 通过UI界面上的文字获取控件
			driver.findElementByName("按钮").click();
		}
		// 通过Id获取控件
		String result = driver.findElementById("tv").getText();
		Assert.assertEquals("点击次数:" + i, result);
		// 等待
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		// 通过控件类型获取控件列表
		List textFieldsList = driver
				.findElementsByClassName("android.widget.EditText");
		textFieldsList.get(0).sendKeys("123456789");
		// driver.findElementById("et").sendKeys("123456789");
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		driver.findElementByName("跳转").click();
	}

	@Test
	public void test2() throws Exception {
		System.out.println("anotherTest");
		String result = driver.findElementById("another_tv").getText();
		//Assert.assertEquals("1234567890", result);
		driver.findElementByName("按钮2").click();
		
		//driver.sendKeyEvent(AndroidKeyCode.BACK);
	}
}


4.     打开Appium

cmd 输入 adb devices –l 获取UDID

cmd输入

appium -a 127.0.0.1 -p 4723 -U UDID --no-reset

启动成功将打印如下


5.     运行测试

在测试项目右键->Run As->TestNG Test

 

参考

appium第一个示例

http://blog.csdn.net/testingba/article/details/23435423?utm_source=tuicool&utm_medium=referral

真机测试

http://blog.sina.com.cn/s/blog_68f262210102w024.html

TestNg教程

http://www.yiibai.com/testng/


资源案例

http://download.csdn.net/detail/q1113225201/9418703

 

你可能感兴趣的:(测试)