12-25 Appium 企业微信实战

课堂 ppt

PPT地址

代码

获取启动页的activity

mac/linux

adb logcat ActivityManager:I | grep "cmp" 

windows:

adb logcat ActivityManager:I | findstr "cmp" 

PO封装

第一层封装:
1、先搭起来架子,运行通了,再填内容
2、传递driver

第二层封装:

  • 添加基类
  • 优化__init__()方法, 将driver放到基类中
  • 在基类中添加一些公共的方法:find, swipe_find, find_click

作业

  • 完成课上实现添加联系人功能,并实现PO封装
  • 加入日志
  • 实现数据驱动(姓名,手机号)
  • 对于desire 数据,保存到数据文件中维护起来,读取yaml文件获取

课后调查表单

import time

from appium import webdriver
from appium.webdriver.common.mobileby import MobileBy

class TestWeChat():
def setup(self):
desired_caps = {}
# 设置访问的平台为Android
desired_caps[‘platformName’] = ‘Android’
# 设置版本为6.0
desired_caps[‘platformVersion’] = ‘6.0’
# 设置要连接模拟器的名字127.0.0.1:7555 (可通过命令adb devices)获取
desired_caps[‘deviceName’] = ‘127.0.0.1:7555’
# 设置访问的软件唯一包名
# 获取前台正在运行的应用的包名:adb shell dumpsys activity|grep(mac用法)mFocusedActivity
# adb shell dumpsys activity|findstr(windows用法)mFocusedActivity
# com.tencent.wework/.launch.WwMainActivity
desired_caps[‘appPackage’] = ‘com.tencent.wework’
# 设置进入后停留在哪一个页面
desired_caps[‘appActivity’] = ‘launch.WwMainActivity’
# 累计保留上次登录或者操作过关闭弹窗等操作,不清空操作数据
desired_caps[‘noReset’] = ‘true’
# 首次启动后不停止和关闭app,并停留在最后操作的页面
# desired_caps[‘dontStopAppOnReset’] = ‘true’
# 跳过安装,权限设置等操作
desired_caps[‘skipDeviceInitialization’] = ‘true’
# 连接本地主机服务
self.driver = webdriver.Remote(‘http://localhost:4723/wd/hub’, desired_caps)
# 设置可输入中文
desired_caps[‘unicodKeyBoard’] = ‘true’
# 设置可输入中文
desired_caps[‘resetKeyBoard’] = ‘true’
# 隐式等待
self.driver.implicitly_wait(10)

def teardown(self):
    # self.driver.back()
    # self.driver.back()
    time.sleep(3)
    self.driver.quit()

def test_add_member(self):
    self.driver.find_element(MobileBy.XPATH,"//*[@text='通讯录']").click()
    self.driver.find_element(MobileBy.XPATH,"//*[@text='添加成员']").click()
    self.driver.find_element(MobileBy.XPATH,"//*[@text='手动输入添加']").click()
    self.driver.find_element(MobileBy.XPATH,"//*[@text='姓名']/..//*[@resource-id='com.tencent.wework:id/au0']").send_keys('dfdssdfgv')
import time

from appium import webdriver
from appium.webdriver.common.mobileby import MobileBy


class TestWeChat():
    def setup(self):
        desired_caps = {}
        # 设置访问的平台为Android
        desired_caps['platformName'] = 'Android'
        # 设置版本为6.0
        desired_caps['platformVersion'] = '6.0'
        # 设置要连接模拟器的名字127.0.0.1:7555 (可通过命令adb devices)获取
        desired_caps['deviceName'] = '127.0.0.1:7555'
        # 设置访问的软件唯一包名
        # 获取前台正在运行的应用的包名:adb shell dumpsys activity|grep(mac用法)mFocusedActivity
        # adb shell dumpsys activity|findstr(windows用法)mFocusedActivity
        # com.tencent.wework/.launch.WwMainActivity
        desired_caps['appPackage'] = 'com.tencent.wework'
        # 设置进入后停留在哪一个页面
        desired_caps['appActivity'] = 'launch.WwMainActivity'
        # 累计保留上次登录或者操作过关闭弹窗等操作,不清空操作数据
        desired_caps['noReset'] = 'true'
        # 首次启动后不停止和关闭app,并停留在最后操作的页面
        # desired_caps['dontStopAppOnReset'] = 'true'
        # 跳过安装,权限设置等操作
        desired_caps['skipDeviceInitialization'] = 'true'
        # 连接本地主机服务
        self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
        # 设置可输入中文
        desired_caps['unicodKeyBoard'] = 'true'
        # 设置可输入中文
        desired_caps['resetKeyBoard'] = 'true'
        # 隐式等待
        self.driver.implicitly_wait(10)

    def teardown(self):
        # self.driver.back()
        # self.driver.back()
        time.sleep(3)
        self.driver.quit()

    def test_add_member(self):
        self.driver.find_element(MobileBy.XPATH,"//*[@text='通讯录']").click()
        self.driver.find_element(MobileBy.XPATH,"//*[@text='添加成员']").click()
        self.driver.find_element(MobileBy.XPATH,"//*[@text='手动输入添加']").click()
        self.driver.find_element(MobileBy.XPATH,"//*[@text='姓名']/..//*[@resource-id='com.tencent.wework:id/au0']").send_keys('dfdssdfgv')
        
class TestWX:
    def setup(self):
        desired_caps = {}
        desired_caps['platformName'] = 'Android'
        desired_caps['platformVersion'] = '6.0'
        desired_caps['deviceName'] = '127.0.0.1:7555'
        desired_caps['appPackage'] = 'com.tencent.wework'
        desired_caps['appActivity'] = '.launch.WwMainActivity'
        desired_caps['noReset'] = 'true'
        desired_caps['dontStopAppOnReset'] = 'true'
        desired_caps['skipDeviceInitialization'] = 'true'
        self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
        self.driver.implicitly_wait(5)
    def teardown(self):
        # self.driver.quit()
        pass

    def test_wx(self):
        self.driver.find_element(By.XPATH, '//*[@resource-id="com.tencent.wework:id/fyl"]').click()
        self.driver.find_element(By.XPATH, '//*[@resource-id="com.tencent.wework:id/c1_"]//*[@text = "添加成员"]')

import time

from appium import webdriver
from appium.webdriver.common.mobileby import MobileBy

class Test_demo:
def setup(self):
desire_cap = {}
desire_cap[“platformName”] = “Android”
desire_cap[“deviceName”] = “127.0.0.1:7555”
desire_cap[“appPackage”] = “com.tencent.wework”
desire_cap[“appActivity”] = “.launch.WwMainActivity”
desire_cap[“unicodeKeyBoard”] = “true”
desire_cap[“noReset”] = “true”
self.driver = webdriver.Remote(“http://127.0.0.1:4723/wd/hub”, desire_cap)
self.driver.implicitly_wait(5)

def teardown(self):
    # self.driver.quit()
    pass

def test_demo(self):
    time.sleep(5)
    self.driver.find_element(MobileBy.XPATH, "//*[@text='通讯录']").click()
    self.driver.find_element(MobileBy.XPATH, "//*[@text='添加成员']").click()
    self.driver.find_element(MobileBy.XPATH, "//*[@text='手动输入添加']").click()
    self.driver.find_element(MobileBy.ID, "com.tencent.wework:id/b7m").send_keys("可可")
    self.driver.find_element(MobileBy.ID, "com.tencent.wework:id/fwi").send_keys("12311231323")
    self.driver.find_element(MobileBy.XPATH, "//*[@text='保存']").click()

注意:

dontStopAppOnReset
``` 这个参数谨慎使用
在启动app 的时候,不关闭app

非常棒,
优化:
1、不用加强制等待
2、缺少断言
3、动态ID的时候不要用ID定位