一。 一个小demo
先实现一个小demo,能够对appium的自动化有一定的了解,理解他的流程。pom配置
<dependencies> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>2.42.1</version> </dependency> <dependency> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.12.4</version> </dependency> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.8.8</version> </dependency> <dependency> <groupId>io.appium</groupId> <artifactId>java-client</artifactId> <version>3.2.0</version> </dependency> <dependency> <groupId>com.google.android</groupId> <artifactId>android</artifactId> <version>4.1.1.4</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies>
demo代码:
先手动启动appium server。在4723端口
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.remote.DesiredCapabilities; import java.net.URL; public class demo1 { private AppiumDriver driver; @Before public void calc() throws Exception { DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability("clearSystemFiles","True"); capabilities.setCapability("androidDeviceReadyTimeout",5); capabilities.setCapability("deviceName","hammerhead"); capabilities.setCapability("platformVersion","5.0"); capabilities.setCapability("platformName","Android"); capabilities.setCapability("appPackage", "com.android.calculator2"); capabilities.setCapability("appActivity", ".Calculator"); driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),capabilities); } @After public void tearDown() throws Exception { driver.quit(); } @Test public void add() { driver.findElement(By.id("com.android.calculator2:id/digit_1")).click(); driver.findElement(By.id("com.android.calculator2:id/op_add")).click(); driver.findElement(By.id("com.android.calculator2:id/digit_2")).click(); driver.findElement(By.id("com.android.calculator2:id/eq")).click(); } }
从这个简单的demo中可以看出:
1. 启动appium server
2. 配置capabilities
3. 启动,获取driver
4. 通过driver找到element。然后进行操作