1.首先导入3个必要的库
2.修改配置
3.封装toat代码
4.全部的代码demo
import time
import unittest
from seleniumimport webdriver
from appiumimport webdriver
from selenium.webdriver.common.byimport By
from selenium.webdriver.supportimport expected_conditions
from selenium.webdriver.support.uiimport WebDriverWait
class ShouYe(unittest.TestCase):
# setUp()方法用于测试用例执行前的初始化工作。如测试用例需要访问数据库,可以在setUp中建立数据库链接
# 并进行初始化。如测试用例需要启动appium服务,则需要在该方法内启动appium服务
def setUp(self):
desired_caps = {}
desired_caps['platformName'] ='Android'
# desired_caps['platformVersion'] = '4.4.2'
# desired_caps['platformVersion'] = '6.0.1'
desired_caps['platformVersion'] ='7.0'
desired_caps['deviceName'] ='test' # 连接多个设备才有用
# desired_caps['app'] = r'e:\apk\toutiao.apk'#如果已经安装好软件乐意不需要,这边填写的是apk在电脑上的路径
desired_caps['appPackage'] ='xxxxxxxxxxxxxxxxxxxx' # app的包名,唯一标志app
desired_caps['appActivity'] ='Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' # 对应安卓应用的操作界面
desired_caps['unicodeKeyboard'] =True # 安装一个中文的输入法
desired_caps['resetKeyboard'] =True #
desired_caps['noReset'] =True # 清除元素数据,跳过初始页面
desired_caps['newCommandTimeout'] =6000
desired_caps['automationName'] ='Uiautomator2'
# 启动Remote RPC
self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)# 把上述参数传给appium services
# tearDown()方法用于测试用例之后的善后工作。如关闭数据库连接,退出应用。
# 无论这个方法写在哪里,都是最后执行
def tearDown(self):
self.driver.quit()
#获取toast text的封装,传入toast部分文本,返回全部文本
def toast_text(self, text):
try:
toast_loc = (By.XPATH, "//*[contains(@text,'" + text +"')]")
ele = WebDriverWait(self.driver, 10, 0.1).until(expected_conditions.presence_of_element_located(toast_loc))
print(ele.text)
return ele.text
except:
return None
# 具体的测试用例,必须要以test开头
# 点击延期任务卡片,完成任务
def test_task_colligate(self):
time.sleep(5)
self.driver.implicitly_wait(100)
# 点击完成任务
self.driver.find_element_by_id("com.xiniunet.xntalk:id/task_colligate_btn").click()
time.sleep(2)
self.driver.find_element_by_id("com.xiniunet.xntalk:id/edt").send_keys("测试完成任务进度啦!")
time.sleep(2)
self.driver.find_element_by_id("com.xiniunet.xntalk:id/tv_copmlete").click()
self.toast_text('完成')













网友评论