第一个Appium(java)例子
环境搭建好了后,可以开始写测试脚本了,这里以知乎的客户端为测试对象:
1. 新建Java工程,名字为Appium_zhihu_demo,因为我是用zhihu.apk做测试的
![](https://img-blog.csdn.net/20160817104805187?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
2. 在工程下新建apps和libs文件夹,apps文件夹用来放apk包
![](https://img-blog.csdn.net/20160817104730999?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](https://img-blog.csdn.net/20160817104751043?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
3. 配置build path,选择project->右键build path->Configure Build Path…或者直接选择Add ExternalArchives…
![](https://img-blog.csdn.net/20160817104554822?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](https://img-blog.csdn.net/20160817104710421?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
注:我学习的时候参考的资料上是添加的java-client-1.2.1.jar,但后来运行一些脚本的时候有问题,自己又添加了高版本java-client-3.1.0.jar包。
4. 添加jar包完成后,就可以开始新建包->新建java class了
![](https://img-blog.csdn.net/20160817104517514?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](https://img-blog.csdn.net/20160817104530509?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
5. 开始编写脚本,这里我们测试知乎的注册功能,代码如下(手机号码,登录密码等注册信息输入真实的即可运行成功):
右键Run as TestNG,运行截图如下:
![](https://img-blog.csdn.net/20160817104454061?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
eclipse 控制台信息:
![](https://img-blog.csdn.net/20160817104354545?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
至此,第一个Appium 自动化测试用例完成,测试功能为注册界面的交互。
代码如下:
- import org.openqa.selenium.remote.DesiredCapabilities;
- import org.testng.Assert;
- import org.testng.annotations.AfterClass;
- import org.testng.annotations.BeforeClass;
- import org.testng.annotations.Test;
-
- import io.appium.java_client.AppiumDriver;
- public class AppiumZhihuRegister {
- public AppiumDriver driver;
- private boolean isInstall = false;
-
- public void startRecord() throws IOException {
- Runtime rt = Runtime.getRuntime();
-
- rt.exec("cmd.exe /C adb shell screenrecord /sdcard/runCase.mp4");
- }
-
- @BeforeClass(alwaysRun = true)
- public void setUp() throws Exception {
-
- DesiredCapabilities capabilities = new DesiredCapabilities();
- capabilities.setCapability("deviceName","lge-nexus_4-005475cbccd279d4");
- capabilities.setCapability("automationName","Appium");
- capabilities.setCapability("platformName","Android");
- capabilities.setCapability("platformVersion","4.4.2");
-
-
- capabilities.setCapability("appPackage", "com.zhihu.android");
- capabilities.setCapability("appActivity", ".app.ui.activity.MainActivity ");
- capabilities.setCapability("sessionOverride", true);
- capabilities.setCapability("unicodeKeyboard", true);
- capabilities.setCapability("resetKeyboard", false);
- driver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
- driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
-
-
- if (isInstall) {
- File classpathRoot = new File(System.getProperty("user.dir"));
- File appDir = new File(classpathRoot, "apps");
- File app = new File(appDir, "zhihu.apk");
- capabilities.setCapability("app", app.getAbsolutePath());
- }
- startRecord();
- }
-
- @Test
- public void loginWithMicroBlog() throws InterruptedException {
-
- driver.findElementById("com.zhihu.android:id/register_btn").click();
- driver.findElementById("com.zhihu.android:id/username").sendKeys("手机号");
- driver.findElementById("com.zhihu.android:id/password").sendKeys("登录密码");
- driver.findElementById("com.zhihu.android:id/fullname").sendKeys("用户昵称");
- WebElement NxtBtn=driver.findElementById("com.zhihu.android:id/btn_progress");
- Assert.assertEquals(NxtBtn.isEnabled(), true);
- Thread.sleep(2000);
- }
-
- @AfterClass(alwaysRun = true)
- public void tearDown() throws Exception {
- driver.quit();
- }
- }