- 🍓学习目标:通过有道云项目的实践来熟悉appium的使用
- 💘如有不足,还望指出
- 💞若觉不错,希望不吝小手点赞、关注支持一波
目录
一、新增笔记-使用常量v1.0
在初始化方法中添加driver.implicitly_wait(10)智能等待,在元素没有刷新出来之前,最大等待10s。然后手动新增一次笔记,定位新增时点击的元素坐标,具体代码如下:
# v1.0实现新增笔记测试
# 导入Appium类库
from appium.webdriver.webdriver import WebDriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
import time
class yd_addnote:
# 使用初始化方法设置测试参数
def __init__(self):
self.caps = {
'automationName': 'UiAutomator2',
'platformName': 'Android',
'platformVersion': '6.0',
'deviceName': '192.168.23.101:5555',
'appPackage': 'com.youdao.note',
'appActivity': '.activity2.MainActivity'}
self.driver = WebDriver('http://127.0.0.1:4723/wd/hub', self.caps)
self.driver.implicitly_wait(10)
# 进行新增笔记的测试
def test_addnote(self):
el = WebDriverWait(self.driver, 10).until(lambda x: x.find_element(By.ID, 'com.android.packageinstaller:id/permission_allow_button'))
if el:
# 点击同意
self.driver.find_element(By.ID, 'com.android.packageinstaller:id/permission_allow_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()
# 输入内容
self.driver.find_element(By.XPATH, '//*[@resource-id="com.youdao.note:id/note_content"]/android.widget.EditText').send_keys('testcontext1234')
# 输入标题
self.driver.find_element(By.ID, 'com.youdao.note:id/note_title').send_keys('testtitle')
# 点击完成
self.driver.find_element(By.ID, 'com.youdao.note:id/actionbar_complete_text').click()
def check_addnote(self):
real_title = self.driver.find_element(By.ID, 'com.youdao.note:id/title').text
print(real_title)
if __name__ == '__main__':
yd_addnote_obj = yd_addnote()
yd_addnote_obj.test_addnote()
yd_addnote_obj.check_addnote()
二、新增笔记-文件读取v2.0
将需要新增的标题和内容写入csv文件,通过读取csv文件来新增笔记。重写父类的方法,加入参数title 和 content。
csv文件准备如下:
代码如下:
# v2.0从文件中读取测试数据进行新增笔记的测试
# 导入Appium类库
import csv
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from AppiumTest.YouDaoYun.work2_addnote import yd_addnote
from appium.webdriver.webdriver import WebDriver
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_allow_button'))
if el:
# 点击同意
self.driver.find_element(By.ID, 'com.android.packageinstaller:id/permission_allow_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()
# 输入内容
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)
# 点击完成
self.driver.find_element(By.ID, 'com.youdao.note:id/actionbar_complete_text').click()
def check_addnote(self, title):
real_title = self.driver.find_element(By.ID, 'com.youdao.note:id/title').text
if real_title == title:
return 1
else:
return 0
if __name__ == '__main__':
# 打开csv文件
file1 = open('addnotedata.csv', 'r')
file2 = open('addnoteresult.csv', 'w', newline='')
table = csv.reader(file1)
writer = csv.writer(file2)
for rows in table:
title = rows[0]
content = rows[1]
# 实例化对象
yd_addnote_obj2 = yd_addnoteV2()
# 调用新增笔记测试方法
yd_addnote_obj2.test_addnote(title, content)
# 检查新增是否正确
result = yd_addnote_obj2.check_addnote(title)
if result:
rows.append('测试通过')
writer.writerow(rows)
else:
rows.append('测试失败')
writer.writerow(rows)
file1.close()
file2.close()