2022.11.13App自动化训练营

PPT

代码地址

课后调查表

https://jinshuju.net/f/v9yBfk

Faker用法

>>> from faker import Faker
>>> fake = Faker("zh_CN")
>>> fake.name()
'张杰'
>>> fake.name()
'杨柳'
>>> fake.name()
'盘秀华'
>>> fake.name()
'亢桂芳'
>>> fake.name()
'陈荣'
>>> fake.name()
'黄慧'
>>> fake.phone_number()
'18946539134'
>>> fake.phone_number()
'18959763610'
>>> fake.phone_number()
'15198913652'
>>> fake.phone_number()
'18202670223'
>>> fake.phone_number()
'18010752856'
>>> 

学习路线总结

appium 学习路线.xmind.zip (252.4 KB)

打开指针位置

  • 设置页面 -找到关于- 找到版本号,连点七下,返回到设置页面
  • 进入开发者选项,找到 指针位置,打开

课后练习

企业微信实战(打卡功能)

  • 前提条件:
    • 1、提前注册企业微信管理员帐号
    • 2、手机端安装企业微信
    • 3、企业微信 app 处于登录状态
  • 实现打卡功能
    • 打开【企业微信】应用
    • 进入【工作台】页面
    • 点击【打卡】
    • 选择【外出打卡】tab
    • 点击【第 N 次打卡】
  • 验证点:提示【外出打卡成功】
from appium import webdriver
import time

from appium.webdriver.common.appiumby import AppiumBy


class TestLogin:
    def setup_class(self):
        caps = {}
        caps["deviceName"] = "hogwarts"
        caps["platformName"] = "Android"
        caps["appPackage"] = "com.tencent.wework"
        caps["appActivity"] = ".launch.LaunchSplashActivity"
        caps["noReset"] = True
        self.driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", caps)
        self.driver.implicitly_wait(10)

    def teardown_class(self):
        self.driver.quit()

    def test_add(self):
        self.driver.find_element(AppiumBy.XPATH,"//*[@text='通讯录']").click()
        self.driver.find_element(AppiumBy.XPATH, "//*[@text='添加成员']").click()
        self.driver.find_element(AppiumBy.XPATH, "//*[@text='手动输入添加']").click()
        self.driver.find_element(AppiumBy.ID,"com.tencent.wework:id/bsm").send_keys("alibaba")
        self.driver.find_element(AppiumBy.ID, "com.tencent.wework:id/hgi").send_keys("18888888888")
        self.driver.find_element(AppiumBy.ID, "com.tencent.wework:id/at6").click()
class TestDemo:
    def setup(self):
        caps = {}
        caps["platformName"] = "Android"
        caps["deviceName"] = "wework"
        caps["appPackage"] = "com.tencent.wework"
        caps["appActivity"] = "com.tencent.wework.launch.WwMainActivity"
        caps["noReset"] = "true"
        caps["dontStopAppOnReset"] = 'true'
        caps["automationName"] = "uiautomator2"
        # caps["ensureWebviewsHavePages"] = True
        self.driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
        self.driver.implicitly_wait(60)


    def test_wework(self):
        # 进入通讯录
        self.driver.find_element(AppiumBy.XPATH, "//*[@text='通讯录']").click()
        # 点击添加成员
        self.driver.find_element(AppiumBy.XPATH, "//*[@text='添加成员']").click()
        # 点击【手动输入添加】
        self.driver.find_element(AppiumBy.ID, "com.tencent.wework:id/dp3").click()
        # 输入姓名
        self.driver.find_element(AppiumBy.ID, "com.tencent.wework:id/bsm").send_keys("执风")
        # 输入手机号
        self.driver.find_element(AppiumBy.ID, "com.tencent.wework:id/hgi").send_keys("17777889900")
        self.driver.find_element(AppiumBy.ID, "com.tencent.wework:id/at6").click()
        print(self.driver.page_source)
        toast = self.driver.find_element(AppiumBy.XPATH, "//*[contains(@text,'添加成功')]").text
        assert '添加成功' in toast

建议:

  • id 如果是动态的,建议不要使用,尽量使用稳定的定位方式
  • 点击完【保存】之后,要添加一个验证

建议:

  • 动态id 需要改成稳定的定位方式,对应的不变的属性,找到这个输入框

企业微信打卡作业提交:

from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy
from faker import Faker
from selenium.common import NoSuchElementException


class TestDaka:
    '''
实现打卡功能
打开【企业微信】应用
进入【工作台】页面
点击【打卡】
选择【外出打卡】tab
点击【第 N 次打卡】
验证点:提示【外出打卡成功】
    '''

    IMPLICITLY_WAIT = 10
    def setup(self):
        caps = {}
        caps["platformName"] = "Android"
        caps["deviceName"] = "xiaolong"
        caps["appPackage"] = "com.tencent.wework"
        caps["appActivity"] = ".launch.LaunchSplashActivity"
        caps["noReset"] = "true"
        caps["ensureWebviewsHavePages"] = True

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

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

    def set_implicitly_wait(self, time=10):
        self.driver.implicitly_wait(time)

    def swipe_find(self, text, max_num=3):

        # self.driver.implicitly_wait(1)
        self.set_implicitly_wait()
        """滑动查找一个文本 text
        如果没有找元素,完成滑动操作
        如果找到了,则返回元素
        """
        for num in range(max_num):
            try:
                # find_element() 每次调用这个方法的时候,都会激活隐式等待,也就是在隐式等待的时长之内,动态的找元素
                element = self.driver.find_element(AppiumBy.XPATH, f"//*[@text='{text}']")
                # 找到了元素之后,再设置回全局的隐式等待时长 10秒
                # self.driver.implicitly_wait(self.IMPLICITLY_WAIT)
                self.set_implicitly_wait(self.IMPLICITLY_WAIT)
                return element
            except NoSuchElementException as e:
                print("未找到元素")
                # 滑动 从下向上
                size = self.driver.get_window_size()
                # 'width', 'height'
                width = size.get("width")
                height = size.get("height")

                startx = width / 2
                starty = height * 0.8

                endx = startx
                endy = height * 0.2

                duration = 2000
                self.driver.swipe(startx, starty, endx, endy, duration)

            if num == max_num - 1:
                # 没有找到的情况。在抛出异常之前,把这个隐式等待改回全局的等待时长 10秒
                # self.driver.implicitly_wait(self.IMPLICITLY_WAIT)
                self.set_implicitly_wait(self.IMPLICITLY_WAIT)
                # 执行到最大次数,仍然没有找到这个文本,则抛出异常
                raise NoSuchElementException(f"找了{num} 次,未找到{text}")

    def daka(self):
        try:
            self.driver.find_element(AppiumBy.XPATH, '//*[@text="上班打卡"]').click()
        except NoSuchElementException as e:
            print("未找到上班打卡,正在下班打卡..")
            self.driver.find_element(AppiumBy.XPATH, '//*[@text="下班打卡"]').click()


    def test_daka(self):
        #点击工作台
        self.driver.find_element(AppiumBy.XPATH,'//*[@text="工作台"]').click()
        #点击打卡
        self.swipe_find("打卡").click()
        #点击上班打卡或者下班打卡
        self.daka()
        #断言
        toast_tips = self.driver.find_element(AppiumBy.XPATH, "//*[contains(@text, '正常')]").text
        print(toast_tips)
        assert toast_tips == "上班·正常" or "下班·正常"```

from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy
from faker import Faker
from selenium.common import NoSuchElementException

class TestWechat:
IMPLICITYLY_WAIT = 10

def setup_class(self):
    # 实例化Faker,可以随机生成姓名和电话
    self.faker = Faker(locale="zh-CN")

def setup(self):
    caps = {}
    caps["platformName"] = "Android"
    caps["deviceName"] = "127.0.0.1:7555"
    caps["appPackage"] = "com.tencent.wework"
    caps["appActivity"] = ".launch.LaunchSplashActivity"
    # caps["noReset"] = "true"
    caps["autoGrantPermissions"] = "true"

    self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_capabilities=caps)

    # 添加隐式等待
    # self.driver.implicitly_wait(self.IMPLICITYLY_WAIT)
    self.set_impilicity_wait(self.IMPLICITYLY_WAIT)

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

def set_impilicity_wait(self, time=1):
    self.driver.implicitly_wait(time)

# 当通讯里中人数较多时,需要滑动才能看到底下
# 增加滑动机制
def swipe(self, num, text):

    # 滑动查找时已经进入了页面,降低隐式等待的时间
    # self.driver.implicitly_wait(3)
    self.set_impilicity_wait()

    for i in range(num):
        try:
            ele = self.driver.find_element(AppiumBy.XPATH, f"//*[@text='{text}']")
            # self.driver.implicitly_wait(self.IMPLICITYLY_WAIT)
            self.set_impilicity_wait(self.IMPLICITYLY_WAIT)

            return ele
        except NoSuchElementException as e:
            print("元素未查找到")
            # 获取滑动的距离
            size = self.driver.get_window_size()
            height = size.get('height')
            width = size.get('width')
            # 获取当前屏幕的尺寸

            start_x = 0.5 * width
            end_x = 0.5 * width
            start_y = 0.8 * height
            end_y = 0.2 * height

            duration = 2000
            self.driver.swipe(start_x, start_y, end_x, end_y, duration)

def test_punch_card(self):
    """
    1.打开企业微信应用
    2.进入工作台页面
    3.点击【打卡】
    4.选择【外出打卡】tab
    5.点击【第N次打卡】
    验证:提示,打卡成功
    :return:
    """
    # 1.进入工作台页面
    self.driver.find_element(AppiumBy.XPATH, "//*[@text='工作台']").click()

    # 点击【打卡】:进入工作台首先需要滑动页面找到元素,再进行滑动
    self.swipe(3, "打卡").click()

    #选择外出打卡
    self.driver.find_element(AppiumBy.XPATH,"//*[@text='外出打卡']").click()

    #点击【打卡】
    self.driver.find_element(AppiumBy.ID,"com.tencent.wework:id/bdv").click()

    #断言打卡成功
    act_punch_card_success=self.driver.find_element(AppiumBy.XPATH,"//*[@text='外出打卡成功']").text
    exc_punch_card_sucess="外出打卡成功"
    assert act_punch_card_success == exc_punch_card_sucess