【自动化训练营】appium 企业微信实战(一)作业贴

课后作业

  • 使用 Appium Inspector 录制企业微信搜索功能的测试用例,搜索一个存在的联系人。

  • 用例步骤:

    • 打开企业微信(提前登录)
    • 进入通讯录
    • 点击搜索按钮
    • 输入 已存在的联系人姓名, 例如“aa”,
    • 点击联系人,进入聊天页面
    • 输入“测试code”
    • 点击发送
    • 退出应用
注意:

提前登录,绕过登录功能( 加 noReset 参数 设置为 True)
使用录制功能完成上面的功能,
进行简单的重构,使用 pytest 测试框架。
可以加入参数化,实现多条搜索功能的测试用例。

作业–20200528

改良前

前置条件:需要手动登录进入到如下页面后,下面的步骤才可以自动运行

  • 进入通讯录
  • 点击搜索按钮
  • 输入 已存在的联系人姓名, 例如“aa”,
  • 点击联系人,进入聊天页面
  • 输入“测试code”
  • 点击发送
  • 退出应用

image

from time import sleep

from appium import webdriver

class TestQiYeWeiXin:
    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'
        self.driver=webdriver.Remote('http://localhost:4723/wd/hub',desired_caps)
        self.driver.implicitly_wait(30)
    def teardown(self):
        self.driver.quit()
    def test_qi_ye_wei_xin(self):
        sleep(1)
        el1 = self.driver.find_element_by_xpath("/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.LinearLayout/android.view.ViewGroup/android.widget.RelativeLayout[2]/android.widget.TextView")
        sleep(1)
        el1.click()
        sleep(1)
        el2 = self.driver.find_element_by_id("com.tencent.wework:id/gw1")
        sleep(1)
        el2.click()
        sleep(1)
        el3 = self.driver.find_element_by_id("com.tencent.wework:id/fl3")
        sleep(1)
        el3.send_keys("川建国2")
        sleep(1)
        el3.click()
        sleep(1)
        el4 = self.driver.find_element_by_xpath("/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.RelativeLayout/android.widget.FrameLayout/android.widget.ListView/android.widget.RelativeLayout[2]/android.widget.RelativeLayout/android.widget.RelativeLayout/android.widget.RelativeLayout")
        sleep(1)
        el4.click()
        sleep(1)
        el5 = self.driver.find_element_by_id("com.tencent.wework:id/ac1")
        sleep(1)
        el5.click()
        sleep(1)
        el6 = self.driver.find_element_by_id("com.tencent.wework:id/dxy")
        sleep(1)
        el6.send_keys("测试code")
        sleep(1)
        el6.click()
        sleep(1)
        el7 = self.driver.find_element_by_id("com.tencent.wework:id/dxu")
        sleep(1)
        el7.click()
        sleep(5)

改良后(按照老师的方法增加一行代码)

  • desired_caps[‘noReset’]=True
from time import sleep

from appium import webdriver

class TestQiYeWeiXin:
    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
        self.driver=webdriver.Remote('http://localhost:4723/wd/hub',desired_caps)
        self.driver.implicitly_wait(30)
    def teardown(self):
        self.driver.quit()
    def test_qi_ye_wei_xin(self):
        sleep(1)
        el1 = self.driver.find_element_by_xpath("/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.LinearLayout/android.view.ViewGroup/android.widget.RelativeLayout[2]/android.widget.TextView")
        sleep(1)
        el1.click()
        sleep(1)
        el2 = self.driver.find_element_by_id("com.tencent.wework:id/gw1")
        sleep(1)
        el2.click()
        sleep(1)
        el3 = self.driver.find_element_by_id("com.tencent.wework:id/fl3")
        sleep(1)
        el3.send_keys("川建国2")
        sleep(1)
        el3.click()
        sleep(1)
        el4 = self.driver.find_element_by_xpath("/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.RelativeLayout/android.widget.FrameLayout/android.widget.ListView/android.widget.RelativeLayout[2]/android.widget.RelativeLayout/android.widget.RelativeLayout/android.widget.RelativeLayout")
        sleep(1)
        el4.click()
        sleep(1)
        el5 = self.driver.find_element_by_id("com.tencent.wework:id/ac1")
        sleep(1)
        el5.click()
        sleep(1)
        el6 = self.driver.find_element_by_id("com.tencent.wework:id/dxy")
        sleep(1)
        el6.send_keys("测试code")
        sleep(1)
        el6.click()
        sleep(1)
        el7 = self.driver.find_element_by_id("com.tencent.wework:id/dxu")
        sleep(1)
        el7.click()
        sleep(5)

import pytest
from appium import webdriver

class TestWechat:
    def setup(self):
        cap = {}
        cap["platformName"] = "Android"
        cap["deviceName"] = "127.0.0.1:7555"
        cap["appActivity"] = ".launch.WwMainActivity"
        cap["appPackage"] = "com.tencent.wework"
        cap["platformVersion"] = "6.0"
        cap["noReset"] = True
        cap['unicodeKeyboard'] = True
        cap['resetKeyboard'] = True
        cap['autoGrantPermissions'] = True


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

    @pytest.mark.parametrize("name,news",[
        ('张三', 'testcode1'),
        ('李四', 'testcode2'),
        ('王五', 'testcode3'),
        ('小红', 'testcode4'),
    ])

    def test_sendnews(self, name, news):
        self.driver.find_element_by_xpath('//*[contains(@text,"通讯录")]').click()
        self.driver.find_element_by_id("com.tencent.wework:id/gw1").click()
        self.driver.find_element_by_id("com.tencent.wework:id/fl3").send_keys(name)
        self.driver.find_element_by_id("com.tencent.wework:id/d37").click()
        self.driver.find_element_by_id("com.tencent.wework:id/ac1").click()
        self.driver.find_element_by_id("com.tencent.wework:id/dxy").send_keys(news)
        self.driver.find_element_by_id("com.tencent.wework:id/dxu").click()

    def teardown(self):
        self.driver.quit()
if __name__=='__main__':
    pytest.main

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

class TestWechat:
    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'] = 'com.tencent.wework.launch.WwMainActivity'  # 打开APP的第一个activity
        desired_caps['noReset'] = True  # 记住之前的动作,不会清除之前的操作的一些换成信息

        self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)  # 打开APP,即跟server创建一个连接
        self.driver.implicitly_wait(5)

    def teardown(self):
        self.driver.quit()  # 退出应用
        # self.driver.back()  # 返回到上一个页面

    @pytest.mark.parametrize('search, type, send', [('李四', '李四', '测试code'),('色橙1', '色橙1', '色橙1,你好'),('阿白', '阿白', '阿白,你好!')])
    def test_search(self, search, type, send):
        self.driver.find_element(MobileBy.XPATH, "//*[@text='通讯录']").click()  # 进入通讯录
        self.driver.find_element(MobileBy.ID, "com.tencent.wework:id/gw1").click()  # 点击搜索按钮
        self.driver.find_element(MobileBy.ID, "com.tencent.wework:id/fl3").send_keys(search)  # 输入搜索词
        self.driver.find_element(MobileBy.XPATH, f"//*[@resource-id='com.tencent.wework:id/dhx']//*[@text='{type}']").click()  # 点击搜索出来的信息
        self.driver.find_element(MobileBy.XPATH, "//*[@text='发消息']").click()  # 点击发消息按钮
        el1 = self.driver.find_element(MobileBy.ID, "com.tencent.wework:id/dxy")  # 定位输入框
        el1.click()  # 点击输入框
        el1.send_keys(send)  # 在该输入框输入需发送的内容
        # print(el1.text)  # 打印输入框中的内容
        actual_send = el1.text  # 把输入框中的实际内容复制给actual_send
        print(f"实际输入的内容:{actual_send}")
        print(f"期望输入的内容:{send}")
        self.driver.find_element(MobileBy.ID, "com.tencent.wework:id/dxu").click()  # 点击发送按钮
        assert actual_send == send  # 断言实际的内容与输入的内容一致

运行截图:
image

  • 使用 Appium Inspector 录制企业微信搜索功能的测试用例,搜索一个存在的联系人。
  • 用例步骤:
    • 打开企业微信(提前登录)
    • 进入通讯录
    • 点击搜索按钮
    • 输入 已存在的联系人姓名, 例如“aa”,
    • 点击联系人,进入聊天页面
    • 输入“测试code”
    • 点击发送
    • 退出应用
注意:

提前登录,绕过登录功能( 加 noReset 参数 设置为 True)
使用录制功能完成上面的功能,
进行简单的重构,使用 pytest 测试框架。
可以加入参数化,实现多条搜索功能的测试用例。
# 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
import pytest
from appium import webdriver
from appium.webdriver.common.mobileby import MobileBy

class TestSendMassage:
    def setup(self):
        caps = {}
        caps["platformName"] = "android"  # 测试设备类型
        caps["deviceName"] = "mumu0"  # 测试设备名
        caps["appPackage"] = "com.tencent.wework"  # 初始登录APP
        caps["appActivity"] = "launch.WwMainActivity"  # 初始登录界面
        caps["noReset"] = "True"  # 不清空登录信息

        self.driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)  # 打开APP
        self.driver.implicitly_wait(120)  # 隐式等待

    def teardown(self):
        #  关闭应用回收资源
        self.driver.quit()

    @pytest.mark.parametrize("name , news", [
        ('a1', '测试code1'),
        ('a2', '测试code2'),
        ('a3', '测试code3'),
        ('a4', '测试code4'),
    ])
    def test_sendmassage(self, name, news):
        #  点击“通讯录”
        self.driver.find_element(MobileBy.XPATH, "//*[@text='通讯录']").click()
        #  点击查找放大镜
        self.driver.find_element(MobileBy.ID, "com.tencent.wework:id/gw1").click()
        #  搜索框输入查找内容“a1”
        el4 = self.driver.find_element(MobileBy.XPATH, "//*[@text='搜索']")
        el4.send_keys(name)
        #  点击查找出的成员
        self.driver.find_element(MobileBy.ID, "com.tencent.wework:id/d37").click()
        #  点击发送消息按钮
        self.driver.find_element(MobileBy.XPATH, "//*[@text='发消息']").click()
        #  发送消息宽内输入要发送的内容
        el8 = self.driver.find_element(MobileBy.ID, "com.tencent.wework:id/dxy")
        el8.send_keys(news)
        #  点击发送按钮
        self.driver.find_element(MobileBy.XPATH, "//*[@text='发送']").click()
        #  点击返回按钮退出发送消息界面
        self.driver.find_element(MobileBy.ID, "com.tencent.wework:id/gvg").click()

代码

from time import sleep
import pytest
from selenium import webdriver
class Testwechet:
    def setup(self):
        desc_tentwecht = {
            "platformName": "Android",
            "deviceName": "127.0.0.1:7555",
            "appPackage": "com.tencent.wework",
            "appActivity": ".launch.LaunchSplashActivity",
            "uiautomationName": "uiautomator2",
            "noReset" : True,
        }
        self.driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desc_tentwecht)
        self.driver.implicitly_wait(20)
    def teardown(self):
        self.driver.quit()
    @pytest.mark.parametrize("name,news",[('张三',"测试code"),("美女",'测试code'),])
    def test_wechet(self,name,news):
        sleep(5)
        self.driver.find_element_by_xpath("//*[@text='通讯录']").click()
        self.driver.find_element_by_xpath("//*[@resource-id='com.tencent.wework:id/gw1']").click()
        self.driver.find_element_by_xpath("//*[@resource-id='com.tencent.wework:id/fl3']").send_keys(name)
        self.driver.find_element_by_xpath("//*[@resource-id='com.tencent.wework:id/d37']").click()
        self.driver.find_element_by_xpath("//*[@resource-id='com.tencent.wework:id/ac1']").click()
        self.driver.find_element_by_xpath("//*[@resource-id='com.tencent.wework:id/dxy']").send_keys(news)
        self.driver.find_element_by_xpath("//*[@resource-id='com.tencent.wework:id/dxu']").click()
        sleep(3)

image

关闭