北京第三期_appium 用例编写_20180805

Appium Server

npm version
npm view appium versions --json
#需要***,一般是没法安装成功的
npm install -g appium --verbose

使用cnpm安装

npm install -g cnpm --registry=https://registry.npm.taobao.org
cnpm install -g appium

ADB

  • adb client,这是我们常用的adb命令
  • adb server,开放5037端口的一个常驻进程
  • adb调试协议,adb client与adb server的通讯
  • adbd,设备端上的一个daemon进程,用于处理与手机的通讯
  • 不通过usb线直连Android真机 adb tcpip + adb connect

findElement的核心逻辑

uiautomator2

private Object findElement(By by) throws ClassNotFoundException, UiAutomator2Exception,
            UiObjectNotFoundException {
        if (by instanceof ById) {
            String locator = getElementLocator((ById) by);
            return getInstance().findObject(android.support.test.uiautomator.By.res(locator));
        } else if (by instanceof By.ByAccessibilityId) {
            return getInstance().findObject(android.support.test.uiautomator.By.desc(by.getElementLocator()));
        } else if (by instanceof ByClass) {
            return getInstance().findObject(android.support.test.uiautomator.By.clazz(by.getElementLocator()));
        } else if (by instanceof By.ByXPath) {
            return getXPathUiObject(by.getElementLocator(), null /* AndroidElement */);
        } else if (by instanceof By.ByAndroidUiAutomator) {
            return getInstance().findObject(findByUiAutomator(by.getElementLocator()));
        }
        String msg = String.format("By locator %s is currently not supported!", by.getClass().getSimpleName());
        throw new UnsupportedOperationException(msg);
    }

toast识别

    @Test
    public void testToast() throws InterruptedException {
        driver.findElementByXPath("//*[@text='Views']").click();
        driver.findElementByAndroidUIAutomator(
                "new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(" +
                        "new UiSelector().text(\\"Popup Menu\\").instance(0));").click();
        driver.findElementByXPath("//*[contains(@text, 'Make')]").click();
        driver.findElementByXPath("//*[@text='Search']").click();
        //System.out.println(driver.findElementByClassName("android.widget.Toast").getText());
        System.out.println(driver.findElementByXPath("//*[@class='android.widget.Toast']").getText());
        
    }

TestXueqiu

import io.appium.java_client.MobileElement;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.GsmCallActions;
import io.appium.java_client.pagefactory.WithTimeout;
import io.appium.java_client.touch.WaitOptions;
import io.appium.java_client.touch.offset.PointOption;
import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.ScreenOrientation;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
import java.util.concurrent.TimeUnit;

public class ApiDemos {

    private AndroidDriver driver;

    @Before
    public void setUp() throws MalformedURLException {
        DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
        desiredCapabilities.setCapability("appPackage", "com.example.android.apis");
        desiredCapabilities.setCapability("appActivity", ".ApiDemos");
        desiredCapabilities.setCapability("platformName", "android");
        desiredCapabilities.setCapability("deviceName", "emulator-5554");
        desiredCapabilities.setCapability("avd", "Three");
        desiredCapabilities.setCapability("automationName", "uiautomator2");

        URL remoteUrl = new URL("http://localhost:4723/wd/hub");
        driver = new AndroidDriver(remoteUrl, desiredCapabilities);
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

    }

    @Test
    public void testToast() throws InterruptedException {
        driver.findElementByXPath("//*[@text='Views']").click();
        driver.findElementByAndroidUIAutomator(
                "new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(" +
                        "new UiSelector().text(\\"Popup Menu\\").instance(0));").click();
        driver.findElementByXPath("//*[contains(@text, 'Make')]").click();
        driver.findElementByXPath("//*[@text='Search']").click();
        //System.out.println(driver.findElementByClassName("android.widget.Toast").getText());
        System.out.println(driver.findElementByXPath("//*[@class='android.widget.Toast']").getText());

    }

    @Test
    public void testSwipe() throws InterruptedException, IOException {
        Thread.sleep(3000);
        for(int i=0;i<10;i++) {
            swipe(0.8, 0.8, 0.4, 0.4);
            FileUtils.copyFile(
                    driver.getScreenshotAs(OutputType.FILE).getCanonicalFile(),
                    new File(i+".png"));
        }
    }

    @Test
    public void testRorate() throws InterruptedException {
        driver.rotate(ScreenOrientation.LANDSCAPE);
        Thread.sleep(2000);
        driver.rotate(ScreenOrientation.PORTRAIT);
        driver.findElementByXPath("//*[@text='App']").click();
        driver.findElementByXPath("//*[@text='Alarm']").click();
        driver.navigate().back();
        driver.navigate().back();

    }

    @Test
    public void testCall(){
        driver.sendSMS("15600534760", "hello from seveniruby");
        driver.makeGsmCall("15600534760", GsmCallActions.CALL);
    }

    public void swipe(Double startX, Double startY, Double endX, Double endY) throws InterruptedException {
        Dimension size=driver.manage().window().getSize();

        TouchAction touchAction = new TouchAction(driver);
        touchAction.press(PointOption.point((int)(size.width*startX), (int)(size.height*startY)));
        touchAction.waitAction(WaitOptions.waitOptions(Duration.ofSeconds(1)));
        touchAction.moveTo(PointOption.point((int)(size.width*endX), (int)(size.height*endY)));
        touchAction.release();
        touchAction.perform();
        Thread.sleep(1000);
    }

    @After
    public void tearDown() {
        //driver.quit();
    }
}

作业1

  • appium server安装
  • 运行你之前的用例
  • 提取所有的appium shell命令回复到帖子里

作业2

  • 连接远程的appium地址 http://10.130.32.76:4723/wd/hub
  • 运行你的测试用例,演练下滑动和切换后台。
  • 体现你的名字或者手机号,unicodeKeyBoard

参考资料

答案

#!/bin/bash
PLATFORM_TOOL="/usr/local/Cellar/android-sdk/24.4.1_1/platform-tools/adb.ori"
#打印时间
echo "#`date +%Y%m%d_%H%M` $0" >> /tmp/adb.log
#打印所有的参数
echo "adb $@">> /tmp/adb.log
test -x "$PLATFORM_TOOL" && exec "$PLATFORM_TOOL" "$@"
echo "It appears you do not have 'Android SDK Platform-tools' installed."
echo "Use the 'android' tool to install them: "
echo "    android update sdk --no-ui --filter 'platform-tools'"
关闭