Appium移动自动化测试之Java篇

1.环境准备:创建模拟器请参考:http://www.cnblogs.com/mrjade/p/5803131.html

2.新建一个java project,【File】-->【New】-->【Project】,【Java Project】-->【Next】,输入工程名称Appium,点击【Finish】,然后新建两个包,apps,libs,libs主要用存放jar包

3.访问此链接将代码下载到本地:https://github.com/appium/sample-code/

下载方式如图所示:

Appium移动自动化测试之Java篇

4.导入相关jar包

下载主要jar包selenium-server-standalone-2.48.0.jar,selenium-java-2.48.0.jar, java-client-4.1.2.jar,(其它jar包如junit,testng,log4j等等我就一一不列了,需要哪个包就自行百度,个人建议从CSDN下载)右键点击工程空白处,选择【Build Path】-->【Configure Build Path】

5.下载测试APK 

  1、下载测试的文件ContactManager.apk:源码路径:appium/sample-code/tree/master/sample-code/apps/ContactManager

  2、将下载的apk放到项目的apps目录下

6.新建测试类

如:ContactsTest.java,源码路径:appium/sample-code/tree/master/sample-code/examples/java/junit/src/test/java/com/saucelabs/appium

代码如下所示,仅供参考,请勿copy

package com.appium.test;

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

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.io.File;
import java.net.URL;
import java.util.List;
@SuppressWarnings("rawtypes")
public class ContactsTest {
private AppiumDriver driver;
@Before
public void setUp() throws Exception {
    // 设置apk的路径
    File classpathRoot = new File(System.getProperty("user.dir"));
    File appDir = new File(classpathRoot, "apps");
    File app = new File(appDir, "ContactManager.apk");
    // 设置自动化相关参数
    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability("deviceName", "Android Emulator");
    capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
    // 设置安卓系统版本
    capabilities.setCapability("platformVersion", "4.4");
    capabilities.setCapability("platformName", "Android");
    // 设置apk路径
    capabilities.setCapability("app", app.getAbsolutePath());
    // 设置app的主包名和主类名
    capabilities.setCapability("appPackage",
    "com.example.android.contactmanager");
    capabilities.setCapability("appActivity", ".ContactManager");
    capabilities.setCapability("appActivity", ".NotesList");
         driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),
      capabilities);
   }

   private AndroidDriver AndroidDriver(URL url,
    DesiredCapabilities capabilities) {
     // TODO Auto-generated method stub
      return null;
    }

   @Test
   public void addContact() {
    WebElement el = driver.findElement(By.name("Add Contact"));
    el.click();
    List<WebElement> textFieldsList = driver
    .findElementsByClassName("android.widget.EditText");
    textFieldsList.get(0).sendKeys("Some Name");
    textFieldsList.get(2).sendKeys("Some@example.com");
    driver.swipe(100, 500, 100, 100, 2);
    driver.findElementByName("Save").click();
  }

  @After
  public void tearDown() throws Exception {
  driver.quit();
  }
}

7.打开Android模拟器和Appium,eclipse点击junit进行运行。Appium日志如下所示。

Appium移动自动化测试之Java篇

上一篇:ios开发人员mac空间不够用的解决办法


下一篇:JDK8+Tomcat8配置https【转】