移动app测试框架Appium实践

1、安装Appium Desktop,用来启动Appium服务

2、在maven工程里面添加如下配置


    io.appium
    java-client
    5.0.4


3、启动模拟器并打开app(2345浏览器)

image.png

4、检查模拟器和电脑是否连接成功

image.png

5、获取app的包名和app主界面对应的Activity

image.png

mSurface=Surface(name=com.browser2345/com.browser2345.BrowserActivity, identity=215)

6、设计Appium的自动化脚本

1)设置平台名称、平台的版本、测试设备的名称

        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("platformName", "Android");//指定测试平台
        //指定测试机的ID,通过adb命令[adb devices]获取
        capabilities.setCapability("deviceName", "127.0.0.1:26944"); 
        capabilities.setCapability("platformVersion", "4.2.2");

2)设置被测试的app的包名和app主页的Activity

        capabilities.setCapability("appPackage", "com.browser2345");
        capabilities.setCapability("appActivity", "com.browser2345.BrowserActivity");

3)创建一个驱动(用来操作app的)(app的主界面会被打开)

        driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);

4)输入搜索的关键字

        driver.findElementByXPath("//android.widget.EditText[@text='输入关键字']").sendKeys("zhaowei");

5)点击搜索

        driver.findElementByXPath("//android.widget.Button[@text=' 搜索 ']").click(); 

7、启动Appium服务

image.png

image.png

8、运行Appium测试脚本

image.png

image.png

日志如下:


image.png
[RemoteTestNG] detected TestNG version 6.11.0
八月 02, 2018 6:02:48 下午 org.openqa.selenium.remote.ProtocolHandshake createSession
信息: Detected dialect: OSS
PASSED: testSearch

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


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

完整代码如下:

package app.test;

import java.net.URL;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;

public class SearchTest {
    
    private AppiumDriver driver;
    
    @Test
    public void testAdd() throws Exception{
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("platformName", "Android");//指定测试平台
        capabilities.setCapability("deviceName", "127.0.0.1:26944"); //指定测试机的ID,通过adb命令[adb devices]获取
        capabilities.setCapability("platformVersion", "4.2.2"); 
        //根据上面获取到的包名和Activity名进行设置
        capabilities.setCapability("appPackage", "com.browser2345");
        capabilities.setCapability("appActivity", "com.browser2345.BrowserActivity");
        
        capabilities.setCapability("unicodeKeyboard", true);
        capabilities.setCapability("resetKeyboard", true);

        driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
        //driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
        Thread.sleep(3000);
        driver.findElementByXPath("//android.widget.TextView[@text='上滑更多精彩']").click();
        //
        Thread.sleep(3000);
        driver.findElementByXPath("//android.widget.Button[@text='以后再说']").click();
        Thread.sleep(3000);
        driver.findElementByXPath("//android.widget.TextView[@text='百度一下']").click();
        Thread.sleep(3000);     
        driver.findElementByXPath("//android.widget.EditText[@text='输入关键字']").sendKeys("zhaowei");
        Thread.sleep(3000);
        driver.findElementByXPath("//android.widget.Button[@text=' 搜索 ']").click(); 
        Thread.sleep(3000);
    }
    
    @AfterMethod
    public void end(){
        driver.quit();
    }
}

你可能感兴趣的:(移动app测试框架Appium实践)