【项目实战-有道云笔记APP】appium+python+unittest

目录

一、项目概况

1.项目背景:

2.测试任务:

3.测试脚本分析:

二、独立功能脚本

1.安装-卸载测试

V1.0:使用脚本在一个模拟器中进行安装-卸载

V2.0版本:适应多个模拟器,进行安装兼容性测试。

2.增删改查独立功能脚本编写:

三、业务场景脚本

1.保证独立功能脚本已调通

2.逐步调用

3.每个步骤都需要进行校验

 4.代码实现

四、测试框架设计

1.配置层CSV文件设计

 2.驱动脚本编写


一、项目概况

1.项目背景:

有道云笔记APP是一款公共APP,具有分类整理笔记,高效管理个人知识,快速搜索,分类查找,安全备份云端笔记等功能。

2.测试任务:

利用appium+python对有道云APP完成自动化测试脚本的编写,并利用unittest框架自动化测试框架,组织并执行用例,输出测试报告。

3.测试脚本分析:

具体测试脚本分为独立功能脚本及业务场景脚本

二、独立功能脚本

1.安装-卸载测试

V1.0:使用脚本在一个模拟器中进行安装-卸载

先对是否安装app进行判断-如果有卸载APP-再进行安装

    def install(self):
        if self.driver.is_app_installed("com.youdao.note"):
            self.driver.remove_app("com.youdao.note")
        self.driver.install_app("E:\youdaoyunbiji_84.apk")

检查点设置为安装后第一次启动弹出框的”deny”按钮

el = driver.find_element_by_id("com.android.packageinstaller:id/permission_deny_button").is_enabled()
print(el)
if el:
   print("安装成功")
else:
   print("安装不成功")

V2.0版本:适应多个模拟器,进行安装兼容性测试。

  1. 将多个模拟器的参数放入CSV文件
  2. 由脚本读取CSV文件,逐次进行卸载-安装测试
  3. 将测试结果写入另一个CSV文件中

 

import csv
import time

from appium.webdriver.common.touch_action import TouchAction
from appium.webdriver.webdriver import WebDriver

from youdao.anzhuang.test2 import yd_install_remove


class yd_install_removeV3():

    def setdevice(self,caps):
        caps["automationName"] = "UiAutomator2"
        caps["platformName"] = "Android"
        print(caps)
        self.driver = WebDriver("http://127.0.0.1:4723/wd/hub", caps)

    def install(self):
        if self.driver.is_app_installed("com.youdao.note"):
            self.driver.remove_app("com.youdao.note")
        self.driver.install_app("E:\youdaoyunbiji_84.apk")

    def check(self):
        caps["appPackage"] = "com.youdao.note"
        caps["appActivity"] = ".activity2.MainActivity t51"
        driver = WebDriver("http://127.0.0.1:4723/wd/hub", caps)
        time.sleep(3)
        el = driver.find_element_by_id("com.android.packageinstaller:id/permission_deny_button").is_enabled()
        print(el)
        if el:
            print("安装成功")
            return 1
        else:
            print("安装不成功")
            return 0

if __name__ == '__main__':
    file1=open("testdata.csv","r")
    file2=open("testresult.csv","w",newline='')
    table=csv.reader(file1)
    writers=csv.writer(file2)
    n=0
    for row in table:
        if n>0:
            obj=yd_install_removeV3()
            caps={}
            caps["deviceName"] = row[1]
            caps["appPackage"] = row[2]
            caps["appActivity"] = row[3]
            obj.setdevice(caps)
            obj.install()
            r=obj.check()
            if r==1:
                row.append("安装测试成功")
                writers.writerow(row)
            else:
                row.append("安装测试失败")
                writers.writerow(row)
        n=n+1
    file2.close()

2.增删改查独立功能脚本编写:

共有4个脚本,以新增笔记为例

1.先手工执行一遍测试用例

2.明确需要定位那些元素,做什么操作

检查点:新增后的标题与写入的标题一致

    def check(self,title):
        rtitle = self.driver.find_element_by_id("com.youdao.note:id/title").text
        if title==rtitle:
            print("成功")
            return 1
        else:
            print("失败")
            return 0

3.考虑测试数据的内容,从常量过渡到从文件读取

4.代码实现

最终版本:从文件读取测试数据,并在文件中写明测试结论

import csv
import time

from appium.webdriver.common.touch_action import TouchAction
from appium.webdriver.webdriver import WebDriver
from selenium.webdriver.support.wait import WebDriverWait

from youdao.anzhuang.test2 import yd_install_remove
from youdao.zengshangaicha.test_addV1 import add


class addV2(add):
    def kk1(self,title,content):
        el = WebDriverWait(self.driver, 10).until(
            lambda x: x.find_element_by_id("com.android.packageinstaller:id/permission_allow_button"))
        if el:
            self.driver.find_element_by_id("com.android.packageinstaller:id/permission_allow_button").click()
            self.driver.find_element_by_id("com.youdao.note:id/add_note").click()
            self.driver.find_element_by_id("com.youdao.note:id/add_note_floater_add_note").click()
            self.driver.find_element_by_id("com.youdao.note:id/btn_cancel").click()
            self.driver.find_elements_by_class_name("android.widget.EditText")[1].send_keys(content)
            self.driver.find_element_by_id("com.youdao.note:id/note_title").send_keys(title)
            self.driver.find_element_by_id("com.youdao.note:id/actionbar_complete_text").click()

    def check(self,title):
        rtitle = self.driver.find_element_by_id("com.youdao.note:id/title").text
        if title==rtitle:
            print("成功")
            return 1
        else:
            print("失败")
            return 0

if __name__ == '__main__':
    file1=open("testdata.csv","r")
    file2=open("testresult.csv","w",newline='')
    table=csv.reader(file1)
    writers=csv.writer(file2)
    for row in table:
        obj = addV2()
        obj.kk1(title=row[0],content=row[1])
        r=obj.check(title=row[0])
        if r == 1:
            row.append("添加笔记测试成功")
            writers.writerow(row)
        else:
            row.append("添加笔记测试失败")
            writers.writerow(row)
    file2.close()

三、业务场景脚本

把新增、修改、查询、删除四个功能合并在一起进行业务场景的测试

1.保证独立功能脚本已调通

2.逐步调用

3.每个步骤都需要进行校验

 4.代码实现

#使用测试框架完成业务场景测试(新增、搜索、修改、删除笔记)

import time
import warnings

from appium.webdriver.common.touch_action import TouchAction
from appium.webdriver.webdriver import WebDriver
from selenium.webdriver.support.wait import WebDriverWait
import unittest

class ydflow(unittest.TestCase):
    @classmethod
    def setUpClass(self):
        warnings.filterwarnings("ignore")
        self.caps = {}
        self.caps["automationName"] = "UiAutomator2"
        self.caps["platformName"] = "Android"
        self.caps["platformVersion"] = "6.0"
        self.caps["deviceName"] = "192.168.33.101:5555"
        self.caps["appPackage"] = "com.youdao.note"
        self.caps["appActivity"] = ".activity2.MainActivity t51"
        self.driver = WebDriver("http://127.0.0.1:4723/wd/hub", self.caps)
        self.driver.implicitly_wait(10)

    #新建
    def test_case1(self):
        el=WebDriverWait(self.driver,10).until(lambda x:x.find_element_by_id("com.android.packageinstaller:id/permission_allow_button"))
        if el:
            self.driver.find_element_by_id("com.android.packageinstaller:id/permission_allow_button").click()

            self.driver.find_element_by_id("com.youdao.note:id/add_note").click()
            self.driver.find_element_by_id("com.youdao.note:id/add_note_floater_add_note").click()
            self.driver.find_element_by_id("com.youdao.note:id/btn_cancel").click()
            self.driver.find_elements_by_class_name("android.widget.EditText")[1].send_keys("内容")
            self.driver.find_element_by_id("com.youdao.note:id/note_title").send_keys("标题")
            self.driver.find_element_by_id("com.youdao.note:id/actionbar_complete_text").click()
            rtitle=self.driver.find_element_by_id("com.youdao.note:id/title").text
            print(rtitle)
            self.assertEqual(rtitle,"标题","增加笔记测试失败")

    #查询
    def test_case2(self):
        self.driver.find_element_by_id("com.youdao.note:id/search").click()
        self.driver.find_element_by_id("com.youdao.note:id/search_edit_view").send_keys("标题")
        self.driver.find_element_by_id("com.youdao.note:id/search_button").click()
        self.driver.get_screenshot_as_file("search.png")
        time.sleep(3)

    #修改
    def test_case3(self):
        self.driver.find_element_by_id("com.youdao.note:id/title").click()
        self.driver.find_element_by_id("com.youdao.note:id/edit").click()
        self.driver.find_elements_by_class_name("android.widget.EditText")[1].send_keys("edition")
        self.driver.find_element_by_id("com.youdao.note:id/note_title").send_keys("edit")
        self.driver.find_element_by_id("com.youdao.note:id/actionbar_complete_text").click()
        rtitle = self.driver.find_element_by_id("com.youdao.note:id/note_title").text
        self.assertEqual("edit", rtitle, "修改笔记测试失败")

    #删除
    def test_case4(self):
        self.driver.find_element_by_id("com.youdao.note:id/menu_more").click()
        self.driver.find_element_by_id("com.youdao.note:id/delete").click()
        self.driver.find_element_by_id("com.youdao.note:id/btn_ok").click()
        num = self.driver.find_elements_by_class_name("android.widget.LinearLayout")
        self.assertEqual(len(num),0,"删除不成功")
        # if len(num) > 0:
        #     print("删除不成功")
        #     self.driver.get_screenshot_as_file('deleteerror.png')
        # else:
        #     print("删除成功")
        #     self.driver.get_screenshot_as_file('deletesucc.png')

    @classmethod
    def tearDownClass(self):
        self.driver.quit()

if __name__ == '__main__':
    #1.用unittest的main方法执行
    # unittest.main()
    print("1")

    #2.用suite测试套方法执行
    suiteobj=unittest.TestSuite()
    suiteobj.addTest(ydflow('test_case1'))
    suiteobj.addTest(ydflow('test_case2'))
    runner = unittest.TextTestRunner()
    runner.run(suiteobj)

四、测试框架设计

1.配置层CSV文件设计

第一列为各测试脚本的类名;第二列为运行状态,可自由设置:yes-运行,no-不运行;第三列为生成的测试报告名

 2.驱动脚本编写

  • 这次使用unittest的makeSuite()方法,括号名里是脚本的类名,对应配置层的第一列
  • 由于是从配置文件中读取类名,需要使用vars()函数将字符串转化为类
suite=unittest.makeSuite(vars()[row[0]])

利用HTMLTestRunner运行测试用例并生成测试报告

import csv
import  unittest

from youdao.ceshikuangjia.test_add import test_add
from youdao.ceshikuangjia.test_delete import test_delete
from youdao.ceshikuangjia.test_edit import test_edit
from youdao.ceshikuangjia.test_search import test_search

from youdao.ceshikuangjia.HTMLTestRunner import HTMLTestRunner

from youdao.ceshikuangjia.test_workflow import ydflow

if __name__ == '__main__':
    file=open("config.csv","r")
    table=csv.reader(file)
    for row in table:
        if row[1]=="YES":
            testname=row[0]
            suite=unittest.makeSuite(vars()[row[0]])
            reportname="F:\\appium\youdao\ceshikuangjia\\"+row[2]
            file=open(reportname,"wb")
            runner=HTMLTestRunner(stream=file,title="业务流程测试",description="新增、搜索、修改、删除",tester="111")
            runner.run(suite)
        else:
            continue

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Appium是一个用于自动化移动应用程序的开源测试框架,而Python是一种常用的编程语言。结合AppiumPython可以实现自动化测试的脚本编写和执行。 你可以使用Python编写Appium测试脚本,利用Appium提供的API来控制手机或模拟器上的应用程序。通过Python的丰富库和语法特性,你可以实现各种自动化测试任务,例如模拟用户输入、验证应用程序的功能、检查界面元素等。 要开始使用AppiumPython,首先需要安装AppiumPython的相关依赖。然后,你可以使用Python的测试框架(如unittest或pytest)编写测试用例,并使用Appium提供的API进行测试操作。 下面是一个简单的示例代码,演示了如何使用AppiumPython进行自动化测试: ```python from appium import webdriver # 设置Desired Capabilities desired_caps = { 'platformName': 'Android', 'platformVersion': '9.0', 'deviceName': 'Android Emulator', 'appPackage': 'com.example.app', 'appActivity': 'com.example.app.MainActivity' } # 连接Appium Server driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) # 执行测试操作 element = driver.find_element_by_id('com.example.app:id/button') element.click() # 断言结果 assert driver.find_element_by_id('com.example.app:id/text').text == 'Hello, World!' # 关闭连接 driver.quit() ``` 以上代码示例了一个安卓应用的测试过程,你可以根据实际情况修改Desired Capabilities和测试操作。通过编写类似的测试脚本,你可以实现更复杂的自动化测试任务。 希望以上信息对你有所帮助!如果有任何问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值