PPT
代码地址
课后调查表
学习路线
2022_App自动化测试学习大纲.xmind.zip (214.5 KB)
from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy
class TestWeChat:
def setup(self):
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['deviceName'] = 'Android Emulator'
desired_caps['appPackage'] = 'com.tencent.wework'
desired_caps['appActivity'] = '.launch.LaunchSplashActivity'
desired_caps['noReset'] = True
self.driver = webdriver.Remote("http://localhost:4723/wd/hub", desired_caps)
self.driver.implicitly_wait(10)
def teardown(self):
self.driver.quit()
def test_add_number(self):
name = "hahaha"
iphone = '13191243667'
self.driver.find_element(AppiumBy.XPATH,"//*[@text='通讯录']").click()
self.driver.find_element(AppiumBy.XPATH,"//*[@text='添加成员']").click()
self.driver.find_element(AppiumBy.XPATH,"//*[@text='手动输入添加']").click()
self.driver.find_element(AppiumBy.ID,"com.tencent.wework:id/bwp").send_keys(name)
self.driver.find_element(AppiumBy.ID,"com.tencent.wework:id/hyw").send_keys(iphone)
self.driver.find_element(AppiumBy.XPATH,"//*[@text='保存']").click()
toast_text = self.driver.find_element(AppiumBy.XPATH,"//*[@text='添加成功']").text
assert toast_text == '添加成功'
优点:
建议:
from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy
from faker import Faker
class TestWenXinApp:
def setup_class(self):
# faker构造数据
self.fake = Faker(locale="zh-CN")
self.name = self.fake.name()
self.phone = self.fake.phone_number()
self.account = self.fake.ssn()
self.email = self.fake.email()
caps = {}
caps["platformName"] = "Android"
# andriod下deviceName可以随便指定,当多个设备是,需要增加uuid来唯一标识某个设备
caps["deviceName"] = "emulator-5554"
caps["udid"] = "127.0.0.1:7555"
# Android包名和页面名,获取命令
# mac/linux:adb logcat ActivityManager:I |grep "cmp"
# windows: adb logcat ActivityManager:I |findstr "cmp"
caps["appPackage"] = "com.tencent.wework"
caps["appActivity"] = ".launch.LaunchSplashActivity"
# 在当前 session 下不会重置应用的状态。默认值为 false
caps["noReset"] = "true"
# 首次启动的时候,不停止 app
# caps["dontStopAppOnReset"] = "true"
# 启用 Unicode 输入,默认为 false,如果需要输入中文时,需要设置为true
caps["unicodeKeyboard"] = "true"
caps["resetKeyboard"] = "true"
# 等待下一个命令超时时长,默认是60s
caps["newCommandTimeout"] = 300
caps["skipServerInstallation"] = "true"
# driver 变成self.driver 由局部变量变成实例变量,就可以在其他方法中引用这个实例变量了
self.driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
# 隐式等待,全局生效
self.driver.implicitly_wait(10)
def setup_down(self):
pass
def test_add_person(self):
# 点击通讯录
self.driver.find_element(AppiumBy.XPATH, "//*[@text='通讯录']").click()
# 点击添加成员
self.driver.find_element(AppiumBy.XPATH, "//*[@text='添加成员']").click()
# 选择手动输入添加
self.driver.find_element(AppiumBy.XPATH, "//*[@text='手动输入添加']").click()
# 输入姓名
self.driver.find_element(AppiumBy.ID, "com.tencent.wework:id/bsm").send_keys(self.name)
# 输入手机号
self.driver.find_element(AppiumBy.ID, "com.tencent.wework:id/hgi").send_keys(self.phone)
# 保存后不自动发送邀请通知
self.driver.find_element(AppiumBy.ID, "com.tencent.wework:id/h2n").click()
# 保存
self.driver.find_element(AppiumBy.XPATH, "//*[@text='保存']").click()
# 获取toast类的文本
toast_text = self.driver.find_element(AppiumBy.XPATH, "//*[@class='android.widget.Toast']").text
assert toast_text == "添加成功"
from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy
class TestSearch:
def setup(self):
caps = {}
caps[“platformName”] = “Android”
# appPackage、appActivity是安卓端特有的
caps[“appPackage”] = “com.tencent.wework”
caps[“appActivity”] = “.launch.LaunchSplashActivity”
caps[“deviceName”] = “127.0.0.1:7555”
caps[“noReset”] = True
# 创建driver,与appium server创建链接,发送请求信息,返回一个session对象
self.driver = webdriver.Remote(“http://localhost:4723/wd/hub”, caps)
self.driver.implicitly_wait(10)
def teardown(self):
self.driver.quit()
def test_addpeople(self):
name = "xsw"
phonenumber = 18888888888
self.driver.find_element(AppiumBy.XPATH, '//*[@text="通讯录"]').click()
self.driver.find_element(AppiumBy.ID, "com.tencent.wework:id/kcc").click()
self.driver.find_element(AppiumBy.ID, "com.tencent.wework:id/fg9").click()
self.driver.find_element(AppiumBy.ID, "com.tencent.wework:id/dp3").click()
self.driver.find_element(AppiumBy.ID, "com.tencent.wework:id/bsm").send_keys(name)
self.driver.find_element(AppiumBy.ID, "com.tencent.wework:id/hgi").send_keys(phonenumber)
self.driver.find_element(AppiumBy.ID, "com.tencent.wework:id/at6").click()
# 获取页面toast
toast_text = self.driver.find_element(AppiumBy.XPATH, "//android.widget.Toast").text
assert toast_text == "添加成功"
from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy
from selenium.common import NoSuchElementException
class TestWeChat:
def setup(self):
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['deviceName'] = 'Android Emulator'
desired_caps['appPackage'] = 'com.tencent.wework'
desired_caps['appActivity'] = '.launch.LaunchSplashActivity'
desired_caps['noReset'] = True
self.driver = webdriver.Remote("http://localhost:4723/wd/hub", desired_caps)
self.driver.implicitly_wait(10)
def teardown(self):
self.driver.quit()
def swipe_find(self, text, swipe_num=3):
"""滑动找某个文字"""
# 找到元素,返回这个元素
# 如果没有找到,就滑动
# 滑动次数,最多为n次
for i in range(swipe_num):
try:
return self.driver.find_element(AppiumBy.XPATH, f"//*[@text='{text}']")
except:
# 手机屏幕尺寸
size = self.driver.get_window_size()
# 'width', 'height'
width = size.get('width')
height = size.get('height')
print(width)
print(height)
# x 宽的一半
# y 屏幕的8/10 处 滑动到 3/10
start_x = width / 2
start_y = height * 0.8
end_x = start_x
end_y = height * 0.3
# duration = 2000
# 滑动
self.driver.swipe(start_x, start_y, end_x, end_y)
if i == swipe_num - 1:
raise NoSuchElementException(f"找了{swipe_num}次,未找到")
def test_punch_clock(self):
swipe_text = '打卡'
self.driver.find_element(AppiumBy.XPATH,"//*[@text='工作台']").click()
self.swipe_find(swipe_text)
self.driver.find_element(AppiumBy.XPATH,"//*[@text='打卡']").click()
self.driver.find_element(AppiumBy.XPATH,"//*[@text='外出打卡']").click()
self.driver.find_element(AppiumBy.XPATH,"//*[contains(@text, '次外出')]").click()
content = self.driver.find_element(AppiumBy.ID,"com.tencent.wework:id/sr").text
assert content == "外出打卡成功"
课外作业
@pytest.mark.parametrize("text", ["程霞"])
def test_del_person(self, text):
# 点击通讯录
self.driver.find_element(AppiumBy.XPATH, "//*[@text='通讯录']").click()
# 点击查找按钮
self.driver.find_element(AppiumBy.XPATH, "//*[@text='自动化科技']/../../../../android.widget.LinearLayout[last()]/android.widget.RelativeLayout[1]").click()
# 搜索输入框,输入成员名称
self.driver.find_element(AppiumBy.XPATH, "//*[@text='搜索']").send_keys(text)
try:
# 如果有该联系人,页面会有”联系人“文本的元素出现,说明用户找到了,否则用户不存在抛出异常
self.driver.find_element(AppiumBy.XPATH, "//*[@text='联系人']")
# 点击该联系人
self.driver.find_element(AppiumBy.XPATH, f"//*[@text='{text}' and @class='android.widget.TextView']").click()
# 点击右上侧的...按钮,进入“个人信息”页面
self.driver.find_element(AppiumBy.XPATH, "//*[@text='个人信息']/../../../../../android.widget.LinearLayout[last()]").click()
# 点击“编辑成员”
self.driver.find_element(AppiumBy.XPATH, "//*[@text='编辑成员']").click()
# 往下滚动页面,找“删除成员”元素
self.swipe_find("删除成员")
# 点击删除成员按钮
self.driver.find_element(AppiumBy.XPATH, "//*[@text='删除成员']").click()
# 点击确定
self.driver.find_element(AppiumBy.XPATH, "//*[@text='确定']").click()
# 如果删除成功,则会在搜索页显示"无搜索结果",找到该元素,表示删除成功
self.driver.find_element(AppiumBy.XPATH, "//*[@text='无搜索结果']")
except:
raise NoSuchElementException(f"该成员{text}不存在,无法删除")
def test_delecontact(self):
self.driver.find_element(AppiumBy.XPATH, "//*[@text='通讯录']").click()
self.driver.find_element(AppiumBy.XPATH, "//*[contains(@text,'何家榜信息')]/../../../following-sibling::android.widget.LinearLayout/*[1]").click()
self.driver.find_element(AppiumBy.XPATH, "//android.widget.EditText[@text = '搜索']").send_keys("李明")
WebDriverWait(self.driver, 5).until(expected_conditions.visibility_of_element_located((AppiumBy.XPATH, "//*[@text = '联系人']")))
self.driver.find_element(AppiumBy.XPATH, "//*[contains(@text, '李明') and @class='android.widget.TextView']").click()
WebDriverWait(self.driver, 5).until(expected_conditions.visibility_of_element_located((AppiumBy.XPATH, "//*[@text = '个人信息']")))
self.driver.find_element(AppiumBy.XPATH, "//*[contains(@text, '个人信息')]/../../../../following-sibling::android.widget.LinearLayout").click()
self.driver.find_element(AppiumBy.XPATH, "//*[@text='编辑成员']").click()
self.swipe_find('删除成员')
self.driver.find_element(AppiumBy.XPATH, "//*[@text='删除成员']").click()
self.driver.find_element(AppiumBy.XPATH, "//*[@text='确定']").click()
assert WebDriverWait(self.driver, 5).until_not(expected_conditions.visibility_of_element_located((AppiumBy.XPATH, "//*[@text = '联系人']")))
def test_delete_member(self):
self.driver.find_element(AppiumBy.XPATH, '//*[@text="通讯录"]').click()
self.driver.find_element(AppiumBy.XPATH, '//*[@text="张雪梅"]').click()
self.driver.find_elements(AppiumBy.XPATH,
'//*[@text="个人信息"]/../../../../..//*[@class="android.widget.TextView"]')[2].click()
self.driver.find_element(AppiumBy.XPATH, "//*[@text='编辑成员']").click()
self.swipe_find_element(3, AppiumBy.XPATH, '//*[@text="删除成员"]').click()
self.driver.find_element(AppiumBy.XPATH, "//*[@text='确定']").click()
assert not WebDriverWait(self.driver, 10).until_not(
expected_conditions.invisibility_of_element_located((AppiumBy.XPATH, '//*[@text="张雪梅"]')))`预先格式化的文本`
最后判断需要优化一下。
可以判断 已经删除的联系人不存在 ,
很不错,思路很清晰
优化:
建议: