1.首先導(dǎo)入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()方法用于測試用例執(zhí)行前的初始化工作臀玄。如測試用例需要訪問數(shù)據(jù)庫居砖,可以在setUp中建立數(shù)據(jù)庫鏈接
# 并進(jìn)行初始化。如測試用例需要啟動appium服務(wù)庆聘,則需要在該方法內(nèi)啟動appium服務(wù)
? ? 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'? # 連接多個設(shè)備才有用
# desired_caps['app'] = r'e:\apk\toutiao.apk'#如果已經(jīng)安裝好軟件樂意不需要胶惰,這邊填寫的是apk在電腦上的路徑
? ? ? ? desired_caps['appPackage'] ='xxxxxxxxxxxxxxxxxxxx'? # app的包名读处,唯一標(biāo)志app
? ? ? ? desired_caps['appActivity'] ='Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'? # 對應(yīng)安卓應(yīng)用的操作界面
? ? ? ? desired_caps['unicodeKeyboard'] =True? # 安裝一個中文的輸入法
? ? ? ? desired_caps['resetKeyboard'] =True? #
? ? ? ? desired_caps['noReset'] =True? # 清除元素數(shù)據(jù)搁宾,跳過初始頁面
? ? ? ? desired_caps['newCommandTimeout'] =6000
? ? ? ? desired_caps['automationName'] ='Uiautomator2'
? ? ? ? # 啟動Remote RPC
? ? ? ? self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)# 把上述參數(shù)傳給appium services
# tearDown()方法用于測試用例之后的善后工作猎唁。如關(guān)閉數(shù)據(jù)庫連接恒水,退出應(yīng)用会放。
# 無論這個方法寫在哪里,都是最后執(zhí)行
? ? def tearDown(self):
self.driver.quit()
#獲取toast text的封裝,傳入toast部分文本,返回全部文本
? ? def toast_text(self, text):
try:
toast_loc = (By.XPATH, "http://*[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開頭
# 點擊延期任務(wù)卡片咧最,完成任務(wù)
? ? def test_task_colligate(self):
time.sleep(5)
self.driver.implicitly_wait(100)
# 點擊完成任務(wù)
? ? ? ? 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("測試完成任務(wù)進(jìn)度啦!")
time.sleep(2)
self.driver.find_element_by_id("com.xiniunet.xntalk:id/tv_copmlete").click()
self.toast_text('完成')