https://github.com/606keng/weeds_study/blob/master/appium_study/test_member.py
这样删除很容易出现一个问题,就是for循环里的任何一个步骤出错,整条用例都出错了。
需要优化一下 ,改成参数化的形式。
好的,已修改,代码已更新
1 个赞
1. 添加联系人
https://github.com/ZHHAYO/TestingDemo/blob/master/test_appium_wechatwork/test_addcontact.py
2. 删除联系人
https://github.com/ZHHAYO/TestingDemo/blob/master/test_appium_wechatwork/test_deletecontact.py
建议把 获取删除列表的长度 放在while循环里面。每次删除之后都重新查看一下长度。
建议部分代码添加注释,便于维护。
提交课后作业
作业1
from time import sleep
import pytest
from appium import webdriver
from appium.webdriver.common.mobileby import MobileBy
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
class TestAdd:
"""
课后作业1:
将课上的添加联系人,自己编写一条用例
使用参数化,添加多条用例
注意:添加联系人的姓名以固定字符开头,例如:测试name1,测试name2,测试name3
"""
def setup_class(self):
desired_caps = {
'platformName': 'android',
'deviceName': 'emulator-5554',
'appPackage': 'com.tencent.wework',
'appActivity': '.launch.WwMainActivity',
'noReset': True,
'autoGrantPermissions': True
}
self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
self.driver.implicitly_wait(6)
# 进入通讯录
self.driver.find_element(MobileBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("通讯录")').click()
# 滑动查找添加成员并点击
self.driver.find_element(MobileBy.ANDROID_UIAUTOMATOR,'new UiScrollable('
'new UiSelector().scrollable(true)'
'.instance(0)).scrollIntoView(new UiSelector()'
'.text("添加成员").instance(0));').click()
@pytest.mark.parametrize('name, sex, phone', [
('测试成员1', '女', '13500000001'),
('测试成员2', '男', '13500000002'),
('测试成员3', '女', '13500000003')
])
def test_add(self, name, sex, phone):
# 点击手动输入添加
self.driver.find_element(MobileBy.ID, 'com.tencent.wework:id/c7g').click()
# 输入姓名
locator_name = (MobileBy.XPATH, '//*[@resource-id="com.tencent.wework:id/b2k"]/*[@text="必填"]')
WebDriverWait(self.driver, 10).until(expected_conditions.element_to_be_clickable(locator_name))
self.driver.find_element(*locator_name).send_keys(name)
# 选择性别
self.driver.find_element(MobileBy.XPATH, '//*[@text="性别"]/..//*[@resource-id="com.tencent.wework:id/av2"]').click()
locator_sex = (MobileBy.XPATH, f'//*[@resource-id="com.tencent.wework:id/dqn" and @text="{sex}"]')
WebDriverWait(self.driver, 5).until(expected_conditions.element_to_be_clickable(locator_sex))
self.driver.find_element(*locator_sex).click()
# 输入手机号
self.driver.find_element(MobileBy.ID, 'com.tencent.wework:id/eq7').send_keys(phone)
# 点击保存
self.driver.find_element(MobileBy.ID, 'com.tencent.wework:id/gur').click()
# 添加需要时间,等待2秒
sleep(2)
# 断言添加成功
assert "添加成功" in self.driver.find_element(MobileBy.XPATH, "//*[@class='android.widget.Toast']").text
def teardown_class(self):
self.driver.quit()
作业2
from time import sleep
import pytest
from appium import webdriver
from appium.webdriver.common.mobileby import MobileBy
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
class TestDelete:
"""
课后作业2:
将名称为 “测试name”开头的所有联系人删除
循环删除所有 "测试name"开头的联系人
删除联系人之后,判断删除成功
"""
def setup_class(self):
desired_caps = {
'platformName': 'android',
'deviceName': 'emulator-5554',
'appPackage': 'com.tencent.wework',
'appActivity': '.launch.WwMainActivity',
'noReset': True,
'autoGrantPermissions': True
}
self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
self.driver.implicitly_wait(6)
# 进入通讯录
self.driver.find_element(MobileBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("通讯录")').click()
# 点击管理图标
self.driver.find_element(MobileBy.ID, 'com.tencent.wework:id/gup').click()
def test_delete(self):
sleep(3)
elements = self.driver.find_elements(MobileBy.ANDROID_UIAUTOMATOR, 'new UiSelector().textContains("测试成员")')
if len(elements) > 0:
# 直接循环删除elements,测试成员2总是找不到,所以先把名称保存到数组
names = []
for element in elements:
text = element.text
print(f"获取到的名字为:{text}")
names.append(text)
# 循环要删除的成员名称数组,进行删除操作
for name in names:
print(f"执行删除的名字为:{text}")
# 点击要删除成员所在行的编辑图标
locator_edit = (MobileBy.XPATH, f'//*[@text="{name}"]/../../../..//*[@resource-id="com.tencent.wework:id/fcq"]')
self.driver.find_element(*locator_edit).click()
# 点击删除按钮
self.driver.find_element(MobileBy.ID, 'com.tencent.wework:id/duq').click()
# 删除弹窗,点击确定删除
locator_delete = (MobileBy.ID, 'com.tencent.wework:id/b_4')
WebDriverWait(self.driver, 3).until(expected_conditions.element_to_be_clickable(locator_delete))
self.driver.find_element(*locator_delete).click()
# 删除需要时间,等待3秒
sleep(3)
# 判断是否删除成功
if len(elements) == 0:
print("删除成功")
def teardown_class(self):
self.driver.quit()
作业1
https://github.com/whiteParachute/PythonTest/blob/master/appium/record/appium_2.py
作业2
https://github.com/whiteParachute/PythonTest/blob/master/appium/record/appium_2_homework.py
from time import sleep
import pytest
from appium import webdriver
from appium.webdriver.common.mobileby import MobileBy
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
class Test_Appium:
def setup(self):
desired_caps = {}
desired_caps['platformName'] = 'android'
desired_caps['deviceName'] = 'emulator-5554'
desired_caps['appPackage'] = 'com.tencent.wework'
desired_caps['appActivity'] = 'com.tencent.wework.launch.WwMainActivity'
desired_caps['noReset'] = 'true'
desired_caps['autoGrantPermissions'] = 'true'
desired_caps['skipDeviceInitialization'] = 'true'
self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
self.driver.implicitly_wait(5)
def teardown(self):
self.driver.quit()
@pytest.mark.parametrize('name,phone,gender'
, [('test1', '13425472811', 'M'), ('test2', '13425472812', 'F'), ('test3', '13425472813', 'F')])
def test_add(self, name, phone, gender):
self.driver.find_element(MobileBy.XPATH,
"//*[@resource-id='com.tencent.wework:id/dnj' and @text='通讯录']").click()
# 滑动查找添加成员并点击
self.driver.find_element(MobileBy.ANDROID_UIAUTOMATOR, 'new UiScrollable('
'new UiSelector().scrollable(true)'
'.instance(0)).scrollIntoView(new UiSelector()'
'.text("添加成员").instance(0));').click()
# 手动输入添加
self.driver.find_element(MobileBy.XPATH, "//*[@resource-id='com.tencent.wework:id/c56']").click()
# 输入name
name_locator = "//*[@resource-id='com.tencent.wework:id/asf' and contains(@text,'姓名')]/../*[@resource-id='com.tencent.wework:id/ase']"
WebDriverWait(self.driver, 5).until(expected_conditions.element_to_be_clickable((MobileBy.XPATH, name_locator)))
self.driver.find_element(MobileBy.XPATH, name_locator).click()
self.driver.find_element(MobileBy.XPATH, name_locator).send_keys(name)
# 输入phone
self.driver.find_element(MobileBy.XPATH, "//*[@resource-id='com.tencent.wework:id/emh']").send_keys(phone)
# 设置部门
self.driver.find_element(MobileBy.XPATH, "//*[@resource-id='com.tencent.wework:id/drm']").click()
self.driver.find_element(MobileBy.XPATH, "//*[@resource-id='com.tencent.wework:id/fik']").click()
# 设置性别
self.driver.find_element(MobileBy.XPATH, "//*[@resource-id='com.tencent.wework:id/drr']").click()
if gender == 'F':
self.driver.find_element(MobileBy.XPATH, "//*[@text='女']").click()
else:
self.driver.find_element(MobileBy.XPATH, "//*[@text='男']").click()
# 点击保存
self.driver.find_element(MobileBy.XPATH, "//*[@resource-id='com.tencent.wework:id/gq7']").click()
# 验证添加成功
sleep(2)
self.driver.find_element(MobileBy.XPATH, "//*[@class='android.widget.Toast']")
@pytest.mark.parametrize('name', ['test1', 'test2', 'test3'])
def test_del(self, name):
self.driver.find_element(MobileBy.XPATH,
"//*[@resource-id='com.tencent.wework:id/dnj' and @text='通讯录']").click()
if len(self.driver.find_elements(MobileBy.XPATH,
f"//*[@resource-id='com.tencent.wework:id/gwb']/*[@class='android.widget.TextView' and @text='{name}']")) > 0:
self.driver.find_element(MobileBy.XPATH,
f"//*[@resource-id='com.tencent.wework:id/gwb']/*[@class='android.widget.TextView' and @text='{name}']").click()
self.driver.find_element(MobileBy.XPATH, "//*[@resource-id='com.tencent.wework:id/gq0']").click()
self.driver.find_element(MobileBy.XPATH, "//*[@resource-id='com.tencent.wework:id/axr']").click()
self.driver.find_element(MobileBy.XPATH, "//*[@resource-id='com.tencent.wework:id/drk']").click()
self.driver.find_element(MobileBy.XPATH, "//*[@resource-id='com.tencent.wework:id/b89']").click()
sleep(5)
assert len(self.driver.find_elements(MobileBy.XPATH,
f"//*[@resource-id='com.tencent.wework:id/gwb']/*[@class='android.widget.TextView' and @text='{name}']")) == 0