线上第七期_移动自动化测试入门_20181013

理论



















Appium Desktop


下载可以参考这里:https://testerhome.com/topics/680

Appium Server

安装方法
安装nodejs,推荐LTS版本
安装appium
npm install -g appium
学院专属工具 bashgems : proxy npm install -g appium
淘宝cnpm
npm install -g cnpm --registry=https://registry.npm.taobao.org
cnpm install -g appium
https://testerhome.com/topics/6040

获取app信息

app信息
获取当前界面元素:adb shell dumpsys activity top
获取任务列表:adb shell dumpsys  activity activities
app入口
adb logcat |grep -i displayed
aapt dump badging mobike.apk  | grep launchable-activity
apkanalyzer 最新版本的sdk中才有
启动应用
adb shell am start -W -n com.xueqiu.android/.view.WelcomeActivityAlias -S

录制代码

# This sample code uses the Appium python client
# pip install Appium-Python-Client
# Then you can paste this into a file and simply run with Python

from appium import webdriver

caps = {}
caps["platformName"] = "android"
caps["deviceName"] = "hogwarts"
caps["appPackage"] = "com.xueqiu.android"
caps["appActivity"] = ".view.WelcomeActivityAlias"

driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)

el1 = driver.find_element_by_id("com.xueqiu.android:id/agree")
el1.click()
el2 = driver.find_element_by_id("com.xueqiu.android:id/tv_search")
el2.click()
el3 = driver.find_element_by_id("com.xueqiu.android:id/search_input_text")
el3.send_keys("alibaba")
el4 = driver.find_element_by_xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.RelativeLayout/android.widget.LinearLayout/android.support.v4.view.ViewPager/android.widget.RelativeLayout/android.widget.ListView/android.widget.LinearLayout[1]/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.RelativeLayout[1]/android.widget.LinearLayout[1]/android.widget.TextView")
el4.click()

driver.quit()

import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import junit.framework.TestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.remote.DesiredCapabilities;

public class SampleTest {

  private AndroidDriver driver;

  @Before
  public void setUp() throws MalformedURLException {
    DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
    desiredCapabilities.setCapability("platformName", "android");
    desiredCapabilities.setCapability("deviceName", "hogwarts");
    desiredCapabilities.setCapability("appPackage", "com.xueqiu.android");
    desiredCapabilities.setCapability("appActivity", ".view.WelcomeActivityAlias");

    URL remoteUrl = new URL("http://localhost:4723/wd/hub");

    driver = new AndroidDriver(remoteUrl, desiredCapabilities);
  }

  @Test
  public void sampleTest() {
    MobileElement el1 = (MobileElement) driver.findElementById("com.xueqiu.android:id/agree");
    el1.click();
    MobileElement el2 = (MobileElement) driver.findElementById("com.xueqiu.android:id/tv_search");
    el2.click();
    MobileElement el3 = (MobileElement) driver.findElementById("com.xueqiu.android:id/search_input_text");
    el3.sendKeys("alibaba");
    MobileElement el4 = (MobileElement) driver.findElementByXPath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.RelativeLayout/android.widget.LinearLayout/android.support.v4.view.ViewPager/android.widget.RelativeLayout/android.widget.ListView/android.widget.LinearLayout[1]/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.RelativeLayout[1]/android.widget.LinearLayout[1]/android.widget.TextView");
    el4.click();
  }

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

用例改造

# This sample code uses the Appium python client
# pip install Appium-Python-Client
# Then you can paste this into a file and simply run with Python

from appium import webdriver

caps = {}
caps["platformName"] = "android"
caps["deviceName"] = "hogwarts"
caps["appPackage"] = "com.xueqiu.android"
caps["appActivity"] = ".view.WelcomeActivityAlias"

driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
driver.implicitly_wait(5)

el1 = driver.find_element_by_id("com.xueqiu.android:id/agree")
el1.click()
el2 = driver.find_element_by_id("com.xueqiu.android:id/tv_search")
el2.click()
el3 = driver.find_element_by_id("com.xueqiu.android:id/search_input_text")
el3.send_keys("alibaba")
driver.find_element_by_id("stockName").click()
#el4 = driver.find_element_by_xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.RelativeLayout/android.widget.LinearLayout/android.support.v4.view.ViewPager/android.widget.RelativeLayout/android.widget.ListView/android.widget.LinearLayout[1]/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.RelativeLayout[1]/android.widget.LinearLayout[1]/android.widget.TextView")
#el4.click()

driver.quit()

import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import junit.framework.TestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.remote.DesiredCapabilities;

public class XueqiuTest {

    private AndroidDriver driver;

    @Before
    public void setUp() throws MalformedURLException {
        DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
        desiredCapabilities.setCapability("platformName", "android");
        desiredCapabilities.setCapability("deviceName", "hogwarts");
        desiredCapabilities.setCapability("appPackage", "com.xueqiu.android");
        desiredCapabilities.setCapability("appActivity", ".view.WelcomeActivityAlias");

        URL remoteUrl = new URL("http://localhost:4723/wd/hub");

        driver = new AndroidDriver(remoteUrl, desiredCapabilities);
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    }

    @Test
    public void sampleTest() {
        MobileElement el1 = (MobileElement) driver.findElementById("com.xueqiu.android:id/agree");
        el1.click();
        MobileElement el2 = (MobileElement) driver.findElementById("com.xueqiu.android:id/tv_search");
        el2.click();
        MobileElement el3 = (MobileElement) driver.findElementById("com.xueqiu.android:id/search_input_text");
        el3.sendKeys("alibaba");

        driver.findElementById("stockName").click();
        //MobileElement el4 = (MobileElement) driver.findElementByXPath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.RelativeLayout/android.widget.LinearLayout/android.support.v4.view.ViewPager/android.widget.RelativeLayout/android.widget.ListView/android.widget.LinearLayout[1]/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.RelativeLayout[1]/android.widget.LinearLayout[1]/android.widget.TextView");
        //el4.click();
    }

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


Android Studio配置

AS地址:https://developer.android.com/studio/

模拟器

测试用例编写

capability所有设置: http://appium.io/docs/en/writing-running-appium/caps/

元素定位

  • id定位
  • xpath定位
  • accessibility id定位
  • find elements + index

xpath定位
绝对定位
/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.RelativeLayout/android.widget.LinearLayout/android.widget.HorizontalScrollView/android.widget.LinearLayout/android.widget.RelativeLayout[2]/android.widget.RelativeLayout/android.widget.TextView

相对定位

(//android.widget.TextView[@text='股票'])[1]
//android.widget.TextView[@text='股票' and @resource-id='com.xueqiu.android:id/text']
//*[@text='股票' and @resource-id='com.xueqiu.android:id/text']
//android.widget.TextView[@instance=2]
//*[contains(@resource-id, 'text') and @text='股票']

常见动作

    def test_swipe(self):
        self.driver.find_element_by_id("agree").click()
        TouchAction(self.driver).press(x=600, y=2000).move_to(x=200, y=2000).release().perform()
        TouchAction(self.driver).press(x=600, y=2000).move_to(x=600, y=200).release().perform()

    @Test
    public void testSwipe(){
        driver.findElementById("com.xueqiu.android:id/agree").click();
        new TouchAction(driver)
                .press(PointOption.point(600, 2000))
                .moveTo(PointOption.point(200, 2000))
                .release()
                .perform();
        new TouchAction(driver)
                .press(PointOption.point(600, 2000))
                .moveTo(PointOption.point(600, 200))
                .release()
                .perform();

    }

作业1

主页面的个人信息图标 -> 登录雪球 -> 手机及其他登陆 -> 邮箱手机号登陆 -> 输入用户名密码

作业2

搜索 -> 输入小米 -> 点击股票类型 -> 点击小米股票

作业3

行情 -> 版块 -> 食品饮料 -> 滚动到最后 -> 选择最后一个股票

关闭