我比较熟悉的是Java代码,Appium也支持多种语言的测试。所以第一个例子还是用最熟悉的语言吧。熟悉原理之后再试试其他的语言。
依赖的jar包:(相关依赖包已上传百度云盘,文章最后有共享路径)
java-client-2.2.0.jar
selenium-server-standalone-2.46.0.jar
testng-6.8.8.jar
1. eclipse安装testng的插件:
离线安装包:eclipse-testng离线包.zip。安装步骤:
解压zip把,将plugins和features目录里的内容分别拷贝到eclipse目录下的plugins和features目录里。
重启eclipse。邮件任意个项目 new->Others->在Select Wizard查询框中输入TestNG。显示如下界面即可。
2. 新建java项目,将以下三个jar包添加到build path。
java-client-2.2.0.jar
selenium-server-standalone-2.46.0.jar
testng-6.8.8.jar
项目下新建Apps目录,将notepad.apk拷贝到目录。
3.测试代码:
package com.com.test; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; import org.openqa.selenium.remote.CapabilityType; import org.openqa.selenium.remote.DesiredCapabilities; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import io.appium.java_client.android.AndroidDriver; import java.io.File; import java.net.URL; public class AppDemo { private AndroidDriver driver; @BeforeMethod(alwaysRun=true) public void setUp() throws Exception { // set up appium File classpathRoot = new File(System.getProperty("user.dir")); File appDir = new File(classpathRoot, "apps"); File app = new File(appDir, "notepad.apk"); System.out.println(app.isFile() + "==" + app.exists()); DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability(CapabilityType.BROWSER_NAME, ""); capabilities.setCapability("platformName", "Android"); capabilities.setCapability("deviceName","Android Emulator"); capabilities.setCapability("platformVersion", "23"); capabilities.setCapability("app", app.getAbsolutePath()); capabilities.setCapability("appPackage", "com.example.android.notepad"); capabilities.setCapability("appActivity", "com.example.android.notepad.NotesList"); driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities); } @AfterMethod(alwaysRun=true) public void tearDown() throws Exception { driver.quit(); } @Test(groups={"noteTest"}) public void noteTest1() { driver.sendKeyEvent(82); try { Thread.sleep(1000); }catch(Exception e) { System.out.println(e.getMessage()); } //Click on the "Add Note" menu entry WebElement el = driver.findElement(By.name("Add note")); el.click(); //Enter the note info and save it WebElement text = driver.findElementByClassName("android.widget.EditText"); text.sendKeys("Hello World"); driver.sendKeyEvent(82); el = driver.findElement(By.name("Save")); el.click(); }
4. 启动模拟器。一定要等到模拟器的首页出现,如果一直停在黑色屏幕并中间带Android字样的界面是没法测试的。
5. 启动Appium。我安装的时候创建了桌面图标,直接点击就可以。注意: Appium界面右上角有一个开始按钮要点一下。等到如下面界面即Appium启动成功。
5. Eclipse 里面运行测试用例。 相关安装软件和依赖包的共享路径: 链接:http://pan.baidu.com/s/1c10nXM4 密码:ud21