学习笔记——测试进阶之路 工作笔记:基于微信 minium 框架的小程序自动化初步实践(5)

优雅先森。
优雅先森。
优雅先森。
订阅者
265
文章
0
评论
测试交流评论161字数 2484阅读8分16秒阅读模式

等待内容更新。。。。。。。。。。。。。。。

学习过程中的经验总结

遇到的技术问题及解决过程(长期更新)

  • get_element 方法
  • inner_wxml

测试用例 DEMO

# !/usr/bin/python
# -*- coding: utf-8 -*-

"""
@Author  : Charles
@Contact : MyProject@MyProject.com
@File    : MyProject_test.py
@Create Time: 2022-06-30 09:26
@Description: XXXXXXXXXXXX
"""
import unittest
import time
import minium
from common import db_func
from common.db_func import get_current_time
from common.message import send_failmessage
execid = int(time.time())
print("execid: ", execid)
@minium.ddt_class
class MyProject(minium.MiniTest):
"""
MyProjectMyProjectMyProjectMyProjectMyProjectMyProject
"""
case_name = "MyProject"
case_field = "MyProject"
project_name = "MyProject"
show_livedata_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(0))
show_offline_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(0))
@minium.ddt_case("43338957","43338966", "43338976", "43338978", "44280929", "43338962", "43338960", "43338961",
"43338954", "43338956", "43338958", "43338955", "43338959", "43338950", "43338952", "43338951",
"43338953", "43338963", "43338965", "43338967", "43338964", "43338968", "43338969", "43338970",
"43338971", "43338972", "43338973", "43338974", "43338975", "43338977", "43338979", "44280927",
"44280930", "44280921", "44280922", "44280917", "44280918", "44280920", "44280923", "44280924",
"44280925", "44280926")
def test_device_livedata(self, args):
"""
MyProjectMyProjectMyProjectMyProjectMyProject
"""
self.page.wait_for(2)
# 用例开始时间
        case_starttime = get_current_time()
try:
if self.page.get_element("/page/view/equip//view/view[3]/view[1]/view[2]/view[2]",
text_contains="MyProject",
max_timeout=5).inner_text == "MyProject":
self.page.wait_for(2)
except:
if self.page.get_element("/page/view/equip//view/view[3]/view[2]",
text_contains="MyProject",
max_timeout=5).inner_text == "MyProject":
self.page.wait_for(2)
# 点击设备搜索框
        try:
elem = self.page.get_element("input[name='input'][placeholder='MyProject']",
inner_text="MyProject",
text_contains="MyProject",
value="MyProject",
max_timeout=5)
except:
elem = self.page.get_element("/page/view/equip//view/view[1]/view/view[2]/input", max_timeout=5)
elem.click()
self.page.wait_for(2)
# 设备搜索框中输入设备号
        device_number = args
try:
elem = self.page.get_element("input[name='input'][placeholder='MyProject']", max_timeout=5)
except:
elem = self.page.get_element("/page/view/view/view[1]/view/view[2]/input", max_timeout=5)
elem.trigger("input", {"value": device_number})
self.page.wait_for(2)
# 点击搜索图标进行搜索
        elem = self.page.get_element("/page/view/view/view[1]/view/view[1]/image", max_timeout=5)
elem.click()
self.page.wait_for(2)
# 点击设备,跳转到设备详情页
        try:
elem = self.page.get_element("text.text-black", max_timeout=5)
except:
elem = self.page.get_element("/page/view/view/view[2]/view/scroll-view/view",
inner_text="MyProject",
text_contains="MyProject",
value="MyProject",
max_timeout=5)
elem.click()
# 获取当前楼层数据
        trigger_data_time = get_current_time()
print("获取当前楼层数据 trigger_data_time:  ", trigger_data_time)
try:
# 判断状态是否为--
            start_time = time.time()
floor_number = '--'
while self.page.element_is_exists("/page/view/view[2]/view[2]/view/view[1]/view/view[2]/text"):
print("While True: 继续执行下面的代码")
if floor_number == '--':
end_time = time.time()
duration = end_time - start_time
print("数据加载时间: ", duration)
print("-------------------判断加载时间是否大于60秒-----------------")
if duraion > 60:
# 超过60秒默认数据加载失败,获取当前设备掉线时间
                        MyProject.show_offline_time = get_current_time()
MyProject.show_livedata_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(0))
print("超过60秒默认数据加载失败,数据加载失败的时间: ", MyProject.show_offline_time,
"实时数据设为默认时间", MyProject.show_livedata_time)
break
floor_marking = self.page.get_element(
"/page/view/view[2]/view[2]/view/view[1]/view/view[2]/text",
max_timeout=5).inner_text
floor_number = floor_marking.strip()
print("循环语句下,当前楼层数为:", floor_number)
if floor_number != "--" and floor_number != "----" and floor_number != "设备DTU掉线" and floor_number != "设备CPU掉线":
MyProject.show_livedata_time = get_current_time()
MyProject.show_offline_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(0))
print("循环语句下,实时数据成功展示时间: ", MyProject.show_livedata_time,
"设备掉线时间设为默认值", MyProject.show_offline_time)
break
elif floor_number == "----":
floor_marking = self.page.get_element(
"/page/view/view[2]/view[2]/view/view[1]/view/view[2]/text",
max_timeout=5).inner_text
floor_number = floor_marking.strip()
print("发现xpath路径,再次进行取值,当前楼层数为:", floor_number)
MyProject.show_livedata_time = get_current_time()
MyProject.show_offline_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(0))
print("循环语句下,实时数据成功展示时间: ", MyProject.show_livedata_time,
"设备掉线时间设为默认值", MyProject.show_offline_time)
break
else:
print("设备实时数据,存在异常。")
continue
except:
# 进入设备掉线的页面
            print("等待状态已改变,进入设备掉线的页面。。。。。。。。。。")
floor = self.page.get_element("/page/view/view[2]/view[2]/view/view[1]/text",
text_contains="掉线",
max_timeout=5)
floor_marking = floor.inner_text
floor_number = floor_marking.strip()
print("当前楼层数为: ", floor_number)
MyProject.show_offline_time = get_current_time()
MyProject.show_livedata_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(0))
print("进入设备掉线的页面,设备掉线的时间: ", MyProject.show_offline_time,
"设备掉线,实时数据设为默认时间", MyProject.show_livedata_time)
# 楼层数据校验
        if floor_number != "" and floor_number != "--" and floor_number != "设备XXX掉线" and floor_number != "设备YYY掉线":
floor_int_value = ["-", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
floor_str_value = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
"N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
if 1 < len(floor_number) < 7:
if floor_number[0] in floor_int_value and floor_number[1] in floor_int_value:
# 如果楼层数为正负数开头的,且第二位是数字,说明数据显示正常,进行类型转换
                    self.assertTrue(floor_number, "当前楼层显示正常,楼层为: {}".format(int(floor_number)))
assert_status = "pass"
elif floor_number[0] in floor_str_value and floor_number[1] in floor_int_value:
# 如果楼层数第一位是字母,第二位是数字,说明数据依然是显示正常,不需要进行类型转换
                    self.assertTrue(floor_number, "当前楼层显示正常,楼层为: {}".format(floor_number))
assert_status = "pass"
else:
# 电梯数据存在异常,直接推送报警
                    send_failmessage(self.case_name, device_number, self.project_name, self.case_field,
floor_number)
assert_status = "fail"
case_endtime = get_current_time()
# 写入数据库
                    db = db_func.connect_db()
sql = "insert INTO device (execid, device_number, project_name, case_field, case_content, triggertime, " 
"showtime, offlinetime, starttime, endtime, status) " 
"VALUES ('{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}');".format 
(execid, device_number, self.project_name, self.case_field, floor_number,
trigger_data_time,
MyProject.show_livedata_time, MyProject.show_offline_time, case_starttime, case_endtime,
assert_status)
db_func.db_insert(db, sql)
self.assertFalse(floor_number, "楼层信息显示异常,楼层为:{}".format(floor_number))
elif len(floor_number) == 1:
if floor_number[0] in floor_int_value:
# 如果楼层数为单个数字,说明数据显示正常,进行类型转换
                    self.assertTrue(floor_number, "当前楼层显示正常,楼层为: {}".format(int(floor_number)))
assert_status = "pass"
elif floor_number[0] in floor_str_value:
# 如果楼层数为单个字母,说明数据显示正常,进行类型转换
                    self.assertTrue(floor_number, "当前楼层显示正常,楼层为: {}".format(floor_number))
assert_status = "pass"
else:
# 如果楼层数不是单个数字或者单个字母,说明数据显示异常,直接推送报警
                    send_failmessage(self.case_name, device_number, self.project_name, self.case_field,
floor_number)
assert_status = "fail"
case_endtime = get_current_time()
# 写入数据库
                    db = db_func.connect_db()
sql = "insert INTO device (execid, device_number, project_name, case_field, case_content, triggertime, " 
"showtime, offlinetime, starttime, endtime, status) " 
"VALUES ('{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}');".format 
(execid, device_number, self.project_name, self.case_field, floor_number,
trigger_data_time,
MyProject.show_livedata_time, MyProject.show_offline_time, case_starttime, case_endtime,
assert_status)
db_func.db_insert(db, sql)
self.assertFalse(floor_number, "楼层信息显示异常,楼层为:{}".format(floor_number))
else:
# 如果获取到的数据长度不是1,2,7,说明是其他内容,直接推送报警
                send_failmessage(self.case_name, device_number, self.project_name, self.case_field,
floor_number)
assert_status = "fail"
case_endtime = get_current_time()
# 写入数据库
                db = db_func.connect_db()
sql = "insert INTO device (execid, device_number, project_name, case_field, case_content, triggertime, " 
"showtime, offlinetime, starttime, endtime, status) " 
"VALUES ('{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}');".format 
(execid, device_number, self.project_name, self.case_field, floor_number, trigger_data_time,
MyProject.show_livedata_time, MyProject.show_offline_time, case_starttime, case_endtime,
assert_status)
db_func.db_insert(db, sql)
self.assertFalse(floor_number, "楼层信息显示异常,楼层为:{}".format(floor_number))
elif floor_number == "设备XXX掉线":
# 设备掉线,直接推送报警
            send_failmessage(self.case_name, device_number, self.project_name, self.case_field, floor_number)
assert_status = "fail"
case_endtime = get_current_time()
# 写入数据库
            db = db_func.connect_db()
sql = "insert INTO device (execid, device_number, project_name, case_field, case_content, triggertime, " 
"showtime, offlinetime, starttime, endtime, status) " 
"VALUES ('{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}');".format 
(execid, device_number, self.project_name, self.case_field, floor_number, trigger_data_time,
MyProject.show_livedata_time, MyProject.show_offline_time, case_starttime, case_endtime,
assert_status)
db_func.db_insert(db, sql)
self.assertFalse(floor_number, "楼层信息显示异常,楼层为:{}".format(floor_number))
elif floor_number == "设备YYY掉线":
# 设备掉线,直接推送报警
            send_failmessage(self.case_name, device_number, self.project_name, self.case_field, floor_number)
assert_status = "fail"
case_endtime = get_current_time()
# 写入数据库
            db = db_func.connect_db()
sql = "insert INTO device (execid, device_number, project_name, case_field, case_content, triggertime, " 
"showtime, offlinetime, starttime, endtime, status) " 
"VALUES ('{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}');".format 
(execid, device_number, self.project_name, self.case_field, floor_number, trigger_data_time,
MyProject.show_livedata_time, MyProject.show_offline_time, case_starttime, case_endtime,
assert_status)
db_func.db_insert(db, sql)
self.assertFalse(floor_number, "楼层信息显示异常,楼层为:{}".format(floor_number))
elif floor_number == "--" or floor_number == "----":
# 实时数据没有加载出来,推送报警
            send_failmessage(self.case_name, device_number, self.project_name, self.case_field, floor_number)
assert_status = "fail"
case_endtime = get_current_time()
# 写入数据库
            db = db_func.connect_db()
sql = "insert INTO device (execid, device_number, project_name, case_field, case_content, triggertime, " 
"showtime, offlinetime, starttime, endtime, status) " 
"VALUES ('{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}');".format 
(execid, device_number, self.project_name, self.case_field, floor_number, trigger_data_time,
MyProject.show_livedata_time, MyProject.show_offline_time, case_starttime, case_endtime,
assert_status)
db_func.db_insert(db, sql)
self.assertFalse(floor_number, "楼层信息未成功显示,楼层为:{}".format(floor_number))
else:
# 楼层信息显示为其他字符,说明电梯存在异常,直接推送报警
            send_failmessage(self.case_name, device_number, self.project_name, self.case_field, floor_number)
assert_status = "fail"
case_endtime = get_current_time()
# 写入数据库
            db = db_func.connect_db()
sql = "insert INTO device (execid, device_number, project_name, case_field, case_content, triggertime, " 
"showtime, offlinetime, starttime, endtime, status) " 
"VALUES ('{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}');".format 
(execid, device_number, self.project_name, self.case_field, floor_number, trigger_data_time,
MyProject.show_livedata_time, MyProject.show_offline_time, case_starttime, case_endtime,
assert_status)
db_func.db_insert(db, sql)
self.assertFalse(floor_number, "楼层信息显示异常,楼层为:{}".format(floor_number))
# 返回上一级:搜索结果页
        self.page.wait_for(2)
self.app.navigate_back()
self.page.wait_for(2)
# 返回上一级:小程序首页
        self.app.navigate_back()
self.page.wait_for(2)
# 测试用例结束时间
        case_endtime = get_current_time()
self.page.wait_for(2)
# 数据写入数据库
        db = db_func.connect_db()
sql = "insert INTO device (execid, device_number, project_name, case_field, case_content, triggertime, " 
"showtime, offlinetime, starttime, endtime, status) " 
"VALUES ('{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}');".format 
(execid, device_number, self.project_name, self.case_field, floor_number, trigger_data_time,
MyProject.show_livedata_time, MyProject.show_offline_time, case_starttime, case_endtime,
assert_status)
db_func.db_insert(db, sql)
if __name__ == "__main__":
loaded_suite = unittest.TestLoader().loadTestsFromTestCase(KoneView)
result = unittest.TextTestRunner().run(loaded_suite)
文章源自玩技e族-https://www.playezu.com/181311.html
 
匿名

发表评论

匿名网友
确定

拖动滑块以完成验证