多端自动化运行:pc+android+ios+小程序 均可

1.先来看下整体结

 

 

 

 

 

目的:配置pc端,验证移动端,通过自动化实现 

已完成:

1.通过airtest 完成android 端脚本

2.通过selenium 完成 pc 脚本

3.通过unittest 来组织

4.通过yagmail 发送

未处理:

1.报告中增加log日志

2.case 封装-基于业务情况来定,因脚本实现简单,通过airtest即可维护

3.cicd

 

最后效果:

 

 

 

 

 

#code

test01.py

# -*- encoding=utf8 -*-
__author__ = "qa"
import time,os
import pytest,unittest
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.keys import Keys
from airtest_selenium.proxy import WebChrome
from selenium.webdriver.remote import switch_to
from selenium.webdriver.support.wait import WebDriverWait
from BeautifulReport import BeautifulReport

from airtest.cli.parser import cli_setup
from airtest.core.api import connect_device, init_device, set_current, device, start_app, snapshot

driver = WebChrome()#需加入 /usr/local/bin
# driver=webdriver.Chrome()
driver.implicitly_wait(20)

'''
pc code
1。登陆工作台
2。客服模式为智能+机器人
3。开启工作台
'''
class PcTaseCase(unittest.TestCase):
# @pytest.mark.run(order=-1)
def save_img(self, img_name):

driver.get_screenshot_as_file('{}/{}.png'.format(os.path.abspath('./img'), img_name))

@BeautifulReport.add_test_img('pc登陆')
def test01(self):
"""登陆pc"""
driver.get("https://xxxxx/login")
driver.find_element("id","userEmail").send_keys("xxxx.com")
driver.find_element("id","userPassword").send_keys("xxxx")
time.sleep(2)
driver.find_element_by_class_name("reg_submit").click()
# snapshot(msg="test.png")
driver.implicitly_wait(20)
self.save_img('pc登陆')
# @pytest.mark.run(order=1)
@BeautifulReport.add_test_img('pc模式为智能+机器人')
def test02(self):
"""app渠道,客服模式为智能模式+机器人"""
#点击设置
driver.find_element("xpath",
"//div[contains(@class,'zc-sidebar')]/div[8]/span[1]").click()
#点击app
driver.find_element("xpath","//*[contains(text(),'APP')]").click()
#点击客服设置
driver.find_element('xpath','//*[contains(text(),"客服设置")]').click()
driver.maximize_window()
#点击设置
driver.find_element('xpath',
'//*[@id="channelPc-cont-box"]/div[2]/table/tbody/tr[1]/td[5]/a').click()
time.sleep(5)
#客服模式默认机器人模式+机器人并保存
# driver.find_element_by_xpath("//*").send_keys(Keys.DOWN)
driver.find_element_by_tag_name('body').send_keys(Keys.END) #通过空格置底
driver.find_element('xpath','//*[@value="保存"]').click()
driver.implicitly_wait(10)
self.save_img('pc模式为智能+机器人')

@BeautifulReport.add_test_img('pc开启工作台')
def test03(self):
"""开启在线工作台"""
driver.find_element('xpath','//*[contains(text(), "客服工作台")]').click()
driver.find_element('xpath','//*[contains(text(), "在线工作台")]').click()
driver.implicitly_wait(30)

#selenium 获取具柄
ops_windows = driver.current_window_handle
# 获取所有具柄
all_windows = driver.window_handles
print(all_windows)
# 切换在线工作台
for handle in all_windows:
if handle != ops_windows:
driver.switch_to.window(handle)

self.save_img('pc开启工作台')

if __name__=="__main__":
unittest.main()
#通过TestSuite 执行指定的case
# suite=unittest.TestSuite()
# suite.addTest(Tasecase("test02"))
#执行
# runner=unittest.TextTestRunner()
# runner.run(suite)





test02.py

#!/user/bin/python3
#coding=utf-8
from airtest.core.api import *
import unittest
from BeautifulReport import BeautifulReport
import os

from poco.drivers.android.uiautomation import AndroidUiautomationPoco

auto_setup(__file__)
'''
h5 code
1。开启灰度/线上h5,用于验证小程序
2。点击启动,机器人,服务评价,转人工,重新会话
'''
class H5TestCase(unittest.TestCase):
@classmethod
def setUpClass(cls):

# init_device("Android", "6e953063") #e+phone

# init_device("Android", "XWN0220C09020939") #华为meta 40 鸿蒙os
# init_device("Android", "959722783700475") #vivo s9
init_device("Android", "7HX0219923014524") #华为meta 30 pro


def setUp(self):
print('ui测试开始')

start_app("com.xxxx.demo")

def test_01(self):
"""触发机器人"""

from poco.drivers.android.uiautomation import AndroidUiautomationPoco

poco = AndroidUiautomationPoco(use_airtest_input=True, screenshot_each_action=False)
poco(name="com.xxxx/sobot_btn").click()
#触发机器人
poco(text="2. 采访").click()
poco("android.widget.Button").click()
poco(text="4. 测试").click()
snapshot(filename="./img/h5-触发机器人.png", quality=90, max_size=800)
# 转人工
poco("android.widget.RelativeLayout").child("android.webkit.WebView").child("android.webkit.WebView").offspring(
"root").offspring("sendarea").child("android.view.View").child("android.view.View").child(
"android.view.View")[1].click()
snapshot(filename="./img/h5-转人工.png", quality=90, max_size=800)
# 滑至底部
poco("scroll-container").swipe("down")
# 触发文本框
poco("textarea").click()
text("QQ") # 输入内容
poco(text="发送").click() # 发送
snapshot(filename="./img/h5-发送文本.png", quality=90, max_size=800)
sleep(2)
#?点击两次展示,待确认点2次,可以显示,有卡顿
poco("moreTools").click() # 加号
sleep(1)
poco("moreTools").click() # 加号
sleep(3)
# snapshot(filename="./img/h5-机器人评价弹窗.png", quality=90, max_size=800)

def test_02(self):
"""服务评价"""
touch(Template(r"服务评价.png", record_pos=(-0.337, 0.956), resolution=(1440, 3120)))
sleep(2)
touch(Template(r"提交.png", record_pos=(0.048, 0.974), resolution=(1440, 3120)))
snapshot(filename="./img/h5-机器人服务评价弹窗.png", quality=90, max_size=800)
sleep(2)


def test_03(self):
"""留言"""
from poco.drivers.android.uiautomation import AndroidUiautomationPoco
poco = AndroidUiautomationPoco(use_airtest_input=True, screenshot_each_action=False)
poco(text="留言").click()
sleep(2)
poco("android.widget.EditText").click()#唤醒文本框
sleep(2)
text("msg-test") # 输入内容
snapshot(filename="./img/h5-留言.png", quality=90, max_size=800)
sleep(2)
poco(text="提交留言").click()

def test_04(self):
"""重新会话"""
touch(Template(r"重新会话.png", record_pos=(0.338, 1.008), resolution=(1440, 3120)))
snapshot(filename="./img/h5-重新会话.png", quality=90, max_size=800)
sleep(2)


# touch(Template(r"tpl1634029541490.png", record_pos=(-0.168, 0.729), resolution=(1440, 3120))) # 已解决
# sleep(2)
# #提交
# touch(Template(r"tpl1634030989021.png", record_pos=(0.048, 0.974), resolution=(1440, 3120)))
# sleep(2)

# @classmethod
# def tearDownClass(cls):
# print('ui测试结束')
def tearDown(self):
print('ui测试结束')
# stop_app("com.sobot.h5.demo")


if __name__ == '__main__':
unittest.main()

test03.py
test04.py
...

#自定义脚本执行顺序
customer.py

import unittest,yagmail
# from copy import copy
from datetime import time

from BeautifulReport import BeautifulReport as bf, BeautifulReport
import time

# from HTMLTestRunner import HTMLTestRunner

from test01 import PcTaseCase #导入登陆
from test02 import H5TestCase#导入h5
from test03 import SwitchTestCase#转人工
if __name__ == "__main__":

# 自定义测试场景
suite = unittest.TestSuite()
suite.addTest(H5TestCase('test_01')) # 启动h5
suite.addTest(H5TestCase('test_02'))
suite.addTest(H5TestCase('test_03'))
suite.addTest(H5TestCase('test_04'))


suite.addTest(PcTaseCase('test01'))#登陆pc
suite.addTest(PcTaseCase('test02'))#app渠道,客服模式为智能模式+机器人
suite.addTest(PcTaseCase('test03'))#开启工作台

#转人工
suite.addTest(SwitchTestCase('test001'))


#实例化bf
run = bf(suite)
run.report(filename="CustomerTestReport", description="多端自定义场景测试")


# now = time.strftime("%Y-%m-%d %H_%M_%S")
# filename = "../report/" + now + " result.html"
#
#
# with open(filename, "wb") as f:
# runner = HTMLTestRunner(
# stream=f,
# title="测试报告",
# description="用例执行情况"
# )
# runner.run(suite)

# 执行测试

# 邮件
yag = yagmail.SMTP(user='xxx@qq.com', password='xxx', host='smtp.qq.com', port='465')
body = "以下是sobot多端自定义测试场景,请查收..."



yag.send("liuyh@sobot.com", attachment)
print("发送成功")

yag.send(to=['liuyh@sobot.com'], subject='sobot 自动化测试报告', contents=[body, './CustomerTestReport.html'])
print("已发送邮件")
runner = unittest.TextTestRunner()
runner.run(suite)



posted @ 2021-09-01 20:41  yanhuidj  阅读(322)  评论(0编辑  收藏  举报