【Appium】使用模拟器实现有道云App的业务功能-新增、搜索、修改、删除

  • 🍓学习目标:通过有道云项目的实践来熟悉appium的使用
  • 💘如有不足,还望指出
  • 💞若觉不错,希望不吝小手点赞关注支持一波

目录

一、创建公共类

二、创建新增笔记功能

三、创建查询笔记功能

四、创建修改笔记功能

五、创建删除笔记功能

六、调用方法


一、创建公共类

因为class类中不能只定义init方法,所以需要定义一个get_driver方法,在启动app后返回driver对象。

# 公共类:设置整个项目的启动参数并返回驱动引擎
# 导入Appium类库
from appium.webdriver.webdriver import WebDriver


class comm_class:
    # 设置启动参数
    def __init__(self):
        self.caps = {
            'automationName': 'UiAutomator2',
            'platformName': 'Android',
            'platformVersion': '6.0',
            'deviceName': '192.168.23.101:5555',
            'appPackage': 'com.youdao.note',
            'appActivity': '.activity2.MainActivity'}

    # get_driver方法
    def get_driver(self):
        self.driver = WebDriver('http://127.0.0.1:4723/wd/hub', self.caps)
        self.driver.implicitly_wait(10)
        return self.driver

二、创建新增笔记功能

新增笔记类前面已经完成,所以只需对其略作修改即可

# v1.0实现新增笔记测试
# 导入Appium类库
from appium.webdriver.webdriver import WebDriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
import time


class yd_addnote:
    # 使用初始化方法设置测试参数
    def __init__(self):
        pass

    # 进行新增笔记的测试
    def test_addnote_flow(self, driver):
        el = WebDriverWait(driver, 10).until(lambda x: x.find_element(By.ID, 'com.android.packageinstaller:id/permission_allow_button'))
        if el:
            # 点击同意
            driver.find_element(By.ID, 'com.android.packageinstaller:id/permission_allow_button').click()
            # 点击+号
            driver.find_element(By.ID, 'com.youdao.note:id/add_note').click()
            # 点击新建笔记
            driver.find_element(By.ID, 'com.youdao.note:id/add_note_floater_add_note').click()
            # 点击取消
            driver.find_element(By.ID, 'com.youdao.note:id/btn_cancel').click()
            # 输入内容
            driver.find_element(By.XPATH, '//*[@resource-id="com.youdao.note:id/note_content"]/android.widget.EditText').send_keys('testcontext1234')
            # 输入标题
            driver.find_element(By.ID, 'com.youdao.note:id/note_title').send_keys('testtitle')
            # 点击完成
            driver.find_element(By.ID, 'com.youdao.note:id/actionbar_complete_text').click()

三、创建查询笔记功能

为了确认查找的内容是正确的,引入了一个新的功能:截屏。使用方法很简单,一句话就好了:driver.get_screenshot_as_file('文件名.png'),文件后缀不止png。然后会在当前目录下生成捕获的截图。

代码如下:

# 进行查询功能的测试
# # 导入appium类库
from appium.webdriver.webdriver import WebDriver
import time
from selenium.webdriver.common.by import By


class yd_searchnote():
    def __init__(self):
        pass

    def test_searchnote_flow(self, driver):
        # 点击搜索按钮
        driver.find_element(By.ID, 'com.youdao.note:id/search').click()
        # 输入搜索关键字
        driver.find_element(By.ID, 'com.youdao.note:id/search_edit_view').send_keys("test")
        # 点击搜索按钮
        driver.find_element(By.ID, 'com.youdao.note:id/clear_search_text_btn').click()
        # 抓取查询结果对应的界面图
        driver.get_screenshot_as_file('searchnote.png')
        time.sleep(3)

四、创建修改笔记功能

修改之后通过抓取修改后的标题来判断是否修改成功。

代码如下:

# 进行修改功能的测试
from appium.webdriver.webdriver import WebDriver
from selenium.webdriver.common.by import By
import time


class yd_editnote:
    def __init__(self):
        pass

    def test_editnote_flow(self, driver):
        driver.find_element(By.ID, 'com.youdao.note:id/title').click()
        driver.find_element(By.ID, 'com.youdao.note:id/edit').click()
        # 修改内容
        driver.find_element(By.XPATH,
                            "//*[@resource-id='com.youdao.note:id/note_content']/android.widget.EditText").send_keys('editcon')
        # 输入标题
        driver.find_element(By.ID, 'com.youdao.note:id/note_title').send_keys('editt')
        # 点击完成
        driver.find_element(By.CLASS_NAME, 'android.support.v7.widget.LinearLayoutCompat').click()
        # 获取修改后的标题
        time.sleep(1)
        rtitle = driver.find_element(By.ID, 'com.youdao.note:id/note_title').text
        print(rtitle)
        if (rtitle == "editt"):
            print("修改成功")
        else:
            print("修改失败")

        time.sleep(3)

五、创建删除笔记功能

定位删除后的空白页面,使用find_elements方法,判断返回元素的个数,如果为0,说明删除成功。

代码如下:

# 删除笔记测试
from selenium.webdriver.common.by import By


class yd_deletenote:
    def __init__(self):
        pass

    def test_deletenote_flow(self, driver):
        driver.find_element(By.ID, 'com.youdao.note:id/menu_more').click()
        driver.find_element(By.ID, 'com.youdao.note:id/delete').click()
        driver.find_element(By.ID, 'com.youdao.note:id/btn_ok').click()
        num = driver.find_elements(By.CLASS_NAME, 'android.widget.LinearLayout')
        print(num)
        if len(num) > 0:
            print("删除不成功")
            driver.get_screenshot_as_file('deleteerror.png')
        else:
            print("删除成功")
            driver.get_screenshot_as_file('deletesucc.png')

六、调用方法

# 进行业务场景的测试(新增->查询->修改->删除)
# 导入Appium类库
from AppiumTest.YouDaoYun.work2_addnote import yd_addnote
from AppiumTest.YouDaoYun.work3_searchnote import yd_searchnote
from AppiumTest.YouDaoYun.common_class import comm_class
from AppiumTest.YouDaoYun.work4_editnote import yd_editnote
from AppiumTest.YouDaoYun.work5_deletenote import yd_deletenote

# 加入新增功能脚本测试
if __name__ == '__main__':
    # 实例化公共类,进行参数设置,返回driver
    com_obj = comm_class()
    driver = com_obj.get_driver()
    # 进行对象的实例化
    add_obj = yd_addnote()
    search_obj = yd_searchnote()
    edit_obj = yd_editnote()
    delete_obj = yd_deletenote()
    # 进行功能测试
    add_obj.test_addnote_flow(driver)
    search_obj.test_searchnote_flow(driver)
    edit_obj.test_editnote_flow(driver)
    delete_obj.test_deletenote_flow(driver)

总结:

  • 截图功能:driver.get_screenshot_as_file('文件名.png')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BetterFate!

爱你哦!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值