#隐式等待、智能等待、重载父类、适应业务场景调用做的改动
#父类
from appium.webdriver.webdriver import WebDriver
from selenium.webdriver.support.wait import WebDriverWait
class yd_addnote:
def __init__(self):
self.caps={}
self.caps['automationName']='UIAutomator2'
self.caps['platformName']='Android'
self.caps['platformVersion']='6.0'
self.caps['deviceName']='192.168.1410101:5555'
self.caps['appPackage']='com.youdao.note'
self.caps['appActivity']='.activity2.MainActivity t362'
self.driver=WebDriver('http://127.0.0.1:4723/wd/hub',self.caps)
#隐式等待
self.driver.implicitly_wait(10)
#业务场景下,初始化类pass掉
#pass
def test_addnote(self):
#智能等待
el = WebDriverWait(self.driver, 10).until(lambda x: x.find_element_by_id(
'com..android.packageinstaller:id/permission_a;;ow_button'))
if el:
# 点击允许按钮
self.driver.find_element_by_id('com.android.packageinstaller:id/permission_a;;ow_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()
# 输入内容
# xpath定位
self.driver.find_element_by_xpath(
"//*[@resource-id='com.youdao.note:id/note_content']/android.widget.EditText").send_keys('testcontent')
# 输入标题
self.driver.find_element_by_id('com.youdao.note:id/note_title').send_keys('testtitle')
# 点击完成按钮
# class_name定位
self.driver.find_element_by_class_name('android.support.done').click()
#业务场景下掉用,传入公共类的driver
def test_addnote_flow(self,driver):
# 智能等待
el = WebDriverWait(driver, 10).until(lambda x: x.find_element_by_id(
'com..android.packageinstaller:id/permission_a;;ow_button'))
if el:
# 点击允许按钮
driver.find_element_by_id('com.android.packageinstaller:id/permission_a;;ow_button').click()
# 点击新建按钮
driver.find_element_by_id('com.youdao.note:id/add_note').click()
# 点击新建笔记
driver.find_element_by_id('com.youdao.note:id/add_note_floater_add_note').click()
# 点击取消按钮
driver.find_element_by_id('com.youdao.note:id/btn_cancel').click()
# 输入内容
# xpath定位
driver.find_element_by_xpath(
"//*[@resource-id='com.youdao.note:id/note_content']/android.widget.EditText").send_keys('testcontent')
# 输入标题
driver.find_element_by_id('com.youdao.note:id/note_title').send_keys('testtitle')
# 点击完成按钮
# class_name定位
driver.find_element_by_class_name('android.support.done').click()
def check_addnote(self):
title=self.driver.find_element_by_id('com.youdao.note:id/title').text
print(title)
#子类,重载父类
import csv
from youdaoproject.work2_addnote import yd_addnote
from selenium.webdriver.support.wait import WebDriverWait
class yd_addnoteV2(yd_addnote):
#重载父类
def test_addnote(self,title,content):
#智能等待
el=WebDriverWait(self.driver,10).until(lambda x: x.find_element_by_id(
'com..android.packageinstaller:id/permission_a;;ow_button'))
if el:
#点击允许按钮
self.driver.find_element_by_id('com.android.packageinstaller:id/permission_a;;ow_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()
#输入内容
#xpath定位
self.driver.find_element_by_xpath(
"//*[@resource-id='com.youdao.note:id/note_content']/android.widget.EditText").send_keys(content)
#输入标题
self.driver.find_element_by_id('com.youdao.note:id/note_title').send_keys(title)
#点击完成按钮
#class_name定位
self.driver.find_element_by_class_name('android.support.done').click()
def check_addnote(self,title):
rtitle=self.driver.find_element_by_id('com.youdao.note:id/title').text
if(rtitle==title):
return 1
else:
return 0
if __name__ == '__main__':
file1=open('addnotedat.csv','r')
table=csv.reader(file1)
file2=open('addnotereport.csv','w',newline='')
writer=csv.writer(file2)
#类实例化放在循环外,类初始化只执行一次出错
#yd_addnote_obj2 = yd_addnoteV2()
for row in table:
title=row[0]
content=row[1]
#每次循环内实例化一次!!!
yd_addnote_obj2 = yd_addnoteV2()
yd_addnote_obj2.test_addnote(title,content)
result=yd_addnote_obj2.check_addnote(title)
if result:
row.append('测试通过')
writer.writerow(row)
else:
row.append('测试失败')
writer.writerow(row)
file2.close()