树洞 Appium 入门

思寒_seveniruby · 2018年11月24日 · 1166 次阅读

安装

如果酒店网络不好,可连接 wifi:testerhome 密码 testerhome

Android 入门

adb devices
adb install ~/Downloads/com.xueqiu.android_11.10.2_190.apk
adb shell am start -W -S -n com.xueqiu.android/.view.WelcomeActivityAlias
adb shell screencap /data/local/tmp/1.png
adb pull /data/local/tmp/1.png
adb shell pm list packages
adb shell pm clear com.xueqiu.android
adb shell dumpsys activity activities top
adb shell dumpsys activity top

desktop 入门

{
  "platformName": "android",
  "deviceName": "demo",
  "appPackage": "com.xueqiu.android",
  "appActivity": ".view.WelcomeActivityAlias"
}

录制并生成代码

# 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"] = "demo"
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/open")
el1.click()
el2 = driver.find_element_by_id("com.android.packageinstaller:id/permission_allow_button")
el2.click()
el3 = driver.find_element_by_id("com.android.packageinstaller:id/permission_allow_button")
el3.click()
el4 = driver.find_element_by_id("com.xueqiu.android:id/agree")
el4.click()
el5 = driver.find_element_by_id("com.android.packageinstaller:id/permission_allow_button")
el5.click()
el6 = driver.find_element_by_id("com.xueqiu.android:id/tv_search")
el6.click()
el7 = driver.find_element_by_id("com.xueqiu.android:id/search_input_text")
el7.send_keys("alibaba")

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", "demo");
    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/open");
    el1.click();
    MobileElement el2 = (MobileElement) driver.findElementById("com.android.packageinstaller:id/permission_allow_button");
    el2.click();
    MobileElement el3 = (MobileElement) driver.findElementById("com.android.packageinstaller:id/permission_allow_button");
    el3.click();
    MobileElement el4 = (MobileElement) driver.findElementById("com.xueqiu.android:id/agree");
    el4.click();
    MobileElement el5 = (MobileElement) driver.findElementById("com.android.packageinstaller:id/permission_allow_button");
    el5.click();
    MobileElement el6 = (MobileElement) driver.findElementById("com.xueqiu.android:id/tv_search");
    el6.click();
    MobileElement el7 = (MobileElement) driver.findElementById("com.xueqiu.android:id/search_input_text");
    el7.sendKeys("alibaba");
  }

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

RobotFrameWork

# This sample code uses the Appium robot client
# pip install robotframework-appiumlibrary
# Then you can paste this into a file and simply run with robot
#
#  more keywords on: http://serhatbolsu.github.io/robotframework-appiumlibrary/AppiumLibrary.html

*** Settings ***
Library           AppiumLibrary

*** Variables ***
${REMOTE_URL}   http://localhost:4723/wd/hub
${platformName}    android
${deviceName}    demo
${appPackage}    com.xueqiu.android
${appActivity}    .view.WelcomeActivityAlias

*** Test Cases ***
Test case name
    Open Application    ${REMOTE_URL}   platformName=${platformName}  deviceName=${deviceName}  appPackage=${appPackage}  appActivity=${appActivity}
    # id=com.xueqiu.android:id/open
    Click Element    id=com.xueqiu.android:id/open
    # id=com.android.packageinstaller:id/permission_allow_button
    Click Element    id=com.android.packageinstaller:id/permission_allow_button
    # id=com.android.packageinstaller:id/permission_allow_button
    Click Element    id=com.android.packageinstaller:id/permission_allow_button
    # id=com.xueqiu.android:id/agree
    Click Element    id=com.xueqiu.android:id/agree
    # id=com.android.packageinstaller:id/permission_allow_button
    Click Element    id=com.android.packageinstaller:id/permission_allow_button
    # id=com.xueqiu.android:id/tv_search
    Click Element    id=com.xueqiu.android:id/tv_search
    # id=com.xueqiu.android:id/search_input_text
    Input Text    id=com.xueqiu.android:id/search_input_text    alibaba

*** Test Teardown ***
    Quit Application

*** Suite Teardown ***
    Close Application

测试用例编写

示例:https://github.com/appium/appium/tree/master/sample-code
单元测试框架:https://docs.python.org/3/library/unittest.html

使用了测试框架的入门用例

import unittest
from appium import webdriver


class TestStringMethods(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        caps = {}
        caps["platformName"] = "android"
        caps["deviceName"] = "demo"
        caps["appPackage"] = "com.xueqiu.android"
        caps["appActivity"] = ".view.WelcomeActivityAlias"
        caps["udid"] = "192.168.59.101:5555"
        caps["app"] = "/Users/seveniruby/Downloads/com.xueqiu.android_11.10.2_190.apk"

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

    @classmethod
    def allow(cls):
        el1 = cls.driver.find_element_by_id("com.xueqiu.android:id/open")
        el1.click()
        el2 = cls.driver.find_element_by_id("com.android.packageinstaller:id/permission_allow_button")
        el2.click()
        el3 = cls.driver.find_element_by_id("com.android.packageinstaller:id/permission_allow_button")
        el3.click()
        el4 = cls.driver.find_element_by_id("com.xueqiu.android:id/agree")
        el4.click()
        el5 = cls.driver.find_element_by_id("com.android.packageinstaller:id/permission_allow_button")
        el5.click()


    def setUp(self):
        caps = {}
        caps["platformName"] = "android"
        caps["deviceName"] = "demo"
        caps["appPackage"] = "com.xueqiu.android"
        caps["appActivity"] = ".view.WelcomeActivityAlias"
        caps["udid"] = "192.168.59.101:5555"
        caps["noReset"] = "true"

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

    def test_xueqiu_start_search_alibaba(self):
        el6 = self.driver.find_element_by_id("com.xueqiu.android:id/tv_search")
        el6.click()
        el7 = self.driver.find_element_by_id("com.xueqiu.android:id/search_input_text")
        el7.send_keys("alibaba")

    def test_xueqiu_start_search_pdd(self):
        el6 = self.driver.find_element_by_id("com.xueqiu.android:id/tv_search")
        el6.click()
        el7 = self.driver.find_element_by_id("com.xueqiu.android:id/search_input_text")
        el7.send_keys("PDD")



if __name__ == '__main__':
    unittest.main()

元素定位

<?xml version="1.0" encoding="UTF-8"?>
<hierarchy rotation="0">
   <android.widget.FrameLayout index="0" text="" class="android.widget.FrameLayout" package="com.xueqiu.android" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,0][1440,2392]" resource-id="" instance="0">
      <android.widget.LinearLayout index="0" text="" class="android.widget.LinearLayout" package="com.xueqiu.android" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,0][1440,2392]" resource-id="" instance="0">
         <android.widget.FrameLayout index="0" text="" class="android.widget.FrameLayout" package="com.xueqiu.android" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,0][1440,2392]" resource-id="android:id/content" instance="1">
            <android.widget.RelativeLayout index="0" text="" class="android.widget.RelativeLayout" package="com.xueqiu.android" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,0][1440,2392]" resource-id="" instance="0">
               <android.widget.ImageView NAF="true" index="0" text="" class="android.widget.ImageView" package="com.xueqiu.android" content-desc="" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[1136,452][1248,564]" resource-id="com.xueqiu.android:id/image_cancel" instance="0" />
               <android.widget.LinearLayout index="1" text="" class="android.widget.LinearLayout" package="com.xueqiu.android" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[191,634][1248,1758]" resource-id="com.xueqiu.android:id/container" instance="1">
                  <android.widget.TextView index="0" text="雪球需要获取下列权限&#xA;才可正常使用:" class="android.widget.TextView" package="com.xueqiu.android" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[261,739][1178,932]" resource-id="" instance="0" />
                  <android.widget.LinearLayout index="1" text="" class="android.widget.LinearLayout" package="com.xueqiu.android" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[261,932][1178,1408]" resource-id="com.xueqiu.android:id/permission_list" instance="2">
                     <android.widget.RelativeLayout index="0" text="" class="android.widget.RelativeLayout" package="com.xueqiu.android" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[261,1002][1178,1170]" resource-id="com.xueqiu.android:id/phone_permission" instance="1">
                        <android.widget.ImageView index="0" text="" class="android.widget.ImageView" package="com.xueqiu.android" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[261,1002][429,1170]" resource-id="com.xueqiu.android:id/phone_permission_icon" instance="1" />
                        <android.widget.TextView index="1" text="手机/电话权限" class="android.widget.TextView" package="com.xueqiu.android" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[475,1002][879,1087]" resource-id="com.xueqiu.android:id/phone_permission_name" instance="1" />
                        <android.widget.TextView index="2" text="校验IMEI&amp;MSI码,防止账号被盗" class="android.widget.TextView" package="com.xueqiu.android" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[475,1101][1178,1167]" resource-id="" instance="2" />
                     </android.widget.RelativeLayout>
                     <android.widget.RelativeLayout index="1" text="" class="android.widget.RelativeLayout" package="com.xueqiu.android" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[261,1240][1178,1408]" resource-id="com.xueqiu.android:id/storage_permission" instance="2">
                        <android.widget.ImageView index="0" text="" class="android.widget.ImageView" package="com.xueqiu.android" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[261,1240][429,1408]" resource-id="com.xueqiu.android:id/storage_permission_icon" instance="2" />
                        <android.widget.TextView index="1" text="存储权限" class="android.widget.TextView" package="com.xueqiu.android" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[475,1240][727,1325]" resource-id="com.xueqiu.android:id/storage_permission_name" instance="3" />
                        <android.widget.TextView index="2" text="缓存图片和视频,降低流量消耗" class="android.widget.TextView" package="com.xueqiu.android" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[475,1339][1161,1405]" resource-id="" instance="4" />
                     </android.widget.RelativeLayout>
                  </android.widget.LinearLayout>
                  <android.widget.TextView index="2" text="开启" class="android.widget.TextView" package="com.xueqiu.android" content-desc="" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[261,1513][1178,1653]" resource-id="com.xueqiu.android:id/open" instance="5" />
               </android.widget.LinearLayout>
            </android.widget.RelativeLayout>
         </android.widget.FrameLayout>
      </android.widget.LinearLayout>
   </android.widget.FrameLayout>
</hierarchy>

常见 XPath

高级框架管理

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
暫無回覆。
需要 登录 後方可回應,如果你還沒有帳號按這裡 注册