appium 自动化测试案例

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了appium 自动化测试案例相关的知识,希望对你有一定的参考价值。

原文地址http://www.cnblogs.com/tobecrazy/p/4579631.html

原文地址http://www.cnblogs.com/tobecrazy/ 

该博主有很多干货,可以多去研究研究

配置目录如下:把知乎客户端命名为zhihu.apk

编写基于java testng的测试用例:

由于本人是熟悉selenium webdriver,所以在android对象识别时,采用的策略和selenium webdriver 类似

怎么去识别APP的元素的,这里用到Android SDK  tools 目录下的工具uiautomatorviewer ,执行uiautomatorviewer.bat就可

  • 首先解析zhihu app 拿到其package 和 activity,这里使用appium gui界面,具体操作如图:

  • 接下来识别APP的元素,比如登陆按钮,看到resourceId,可以是雨天webdriver的find Element By.id

至于登陆用户名和密码,在Android中都是EditText控件,可以使用By.class

 

 


完成对象识别,最终代码如下:

 

 

 

 

复制代码
package com.dbyl.core;
 
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.Assert;
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;
import java.util.List;
import java.util.concurrent.TimeUnit;
 
public class zhiHu {
    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, "zhihu.apk");
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
        capabilities.setCapability("platformName", "Android");
        capabilities.setCapability("deviceName","Android Emulator");
        capabilities.setCapability("platformVersion", "4.4");
        //if no need install don\'t add this
        capabilities.setCapability("app", app.getAbsolutePath());
        capabilities.setCapability("appPackage", "com.zhihu.android");
        //support Chinese 
        capabilities.setCapability("unicodeKeyboard" ,"True");
        capabilities.setCapability("resetKeyboard", "True");
        //no need sign
        capabilities.setCapability("noSign", "True");
        capabilities.setCapability("appActivity", ".ui.activity.GuideActivity");
        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={"ZHTest"})
    public void Login(){
        //find login button
        WebElement loginButton = driver.findElement(By.id("com.zhihu.android:id/login"));
        loginButton.click();
        
        //wait for 20s
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        
        //find login userName and password editText 
        List<WebElement> textFieldsList = driver.findElementsByClassName("android.widget.EditText");
        textFieldsList.get(0).sendKeys("seleniumcookies@126.com");
        textFieldsList.get(1).sendKeys("cookies123");
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        
        //find ok button byName
        driver.findElementByName("OK").click();
        driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS);
        
        //find keyword 首页 and verify it is display
        Assert.assertTrue( driver.findElement(By.name("首页")).isDisplayed());
        
        
    }
}
复制代码

 

接下来就可以debug,首先启动Android模拟器 ,启动完毕后再明明很测试,若结果如下:

说明模拟器可以使用

接下来在命令行运行apppium

等待1-2分钟

run as testng,就能看到自动安装,登陆知乎

PASSED: Login

===============================================
Default test
Tests run: 1, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================

[TestNG] Time taken by org.testng.reporters.EmailableReporter2@4474c7fe: 1938 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@1fdf50a3: 121 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 0 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter@697748ae: 423 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@68450212: 1669 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@7f92deaf: 240 ms

以上是关于appium 自动化测试案例的主要内容,如果未能解决你的问题,请参考以下文章

python+appium自动化测试-元素等待

python+appium自动化测试 —— Appium并发测试之多设备启动

Appium+python自动化(三十九)-Appium自动化测试框架综合实践 - 代码实现(超详解)

有关于appium自动化测试的问题

如何用appium测试ios包

测试新人如何使用Python代码封装自动化测试的用例?