总述 :
巧妙处理有时出现有时 不 出现的元素。
selenium4,智能等待,获得消息弹出框的属性值并断言
前提条件:
环境搭建完成
1、连接手机,通过远程连接,给出手机或 模拟器参数
下面 写 法 支持 要 测试app已经安装
adb devices
结果写在appium:deviceName
adb shell dumpsys activity activities | grep mFocusedActivity
结果分别写在appium:appPackage,appium:appActivity,
com.youdao.note/.activity2.MainActivity
导入 的依赖和手机连接参数如下:
from appium import webdriver
import time
from appium.webdriver.common.appiumby import AppiumBy
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
des_cap = {
"platformName": "android",
"appium:deviceName": "emulator-5554",
"appium:appPackage": "com.youdao.note",
"appium:appActivity": ".activity2.MainActivity",
"appium:automationName": "UiAutomator2",
"appium:unicodeKeyboard": True,
"appium:resetKeyboard": True,
"appium:autoGrantPermissions": True
}
# remote远程连接,appium_server:http://127.0.0.1:4723/wd/hub ,,模拟器手机连接参数desired_capabilities
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_capabilities=des_cap)
driver.implicitly_wait(10)
2、进入app开始时授权同意,但下一次可以不出现。通过find_elements定位判断如果元素出现就点击,如果不 出现返回是空的[]列表,但如果是 find_element定位元素,元素找不到会报错并停止执行。
driver.find_element(AppiumBy.ID,"com.youdao.note:id/btn_ok").click()
if driver.find_elements(AppiumBy.ID,"com.youdao.note:id/btn_cancel"):
driver.find_element(AppiumBy.ID, "com.youdao.note:id/btn_cancel").click()
3、适配所有 机型从右往左滑动,可以 单独封装为 一 个公共方法
通过获得手机宽高,滑动百分比实现。
size = driver.get_window_size()
width = size['width']
height = size['height']
driver.swipe(width / 2, height * 9 / 10, width / 2, height / 10, 500)
4、 有条件智能等待, 获得消息弹出框的属性值并断言
# 用户满意度调查
driver.find_element(AppiumBy.ID,"com.youdao.note:id/satisfaction_survey").click()
time.sleep(1)
# 选择8分
driver.find_element(AppiumBy.ID,"com.youdao.note:id/rate8").click()
time.sleep(1)
# 提交反馈
driver.find_element(AppiumBy.ID,"com.youdao.note:id/actionbar_menu_layout").click()
time.sleep(1)
# 加个有条件智能等待
wait=WebDriverWait(driver,10)
wait.until(EC.presence_of_element_located(driver.find_element(AppiumBy.ID, "com.youdao.note:id/text_title")))
# 获得消息弹出框值,获得属性值,断言
assert "谢谢参与调查!" in driver.find_element(AppiumBy.ID, "com.youdao.note:id/text_title").get_attribute("text")
整体代码如下:
from appium import webdriver
import time
# adb shell dumpsys activity activities | grep mFocusedActivity
# com.youdao.note/.activity2.MainActivity
from appium.webdriver.common.appiumby import AppiumBy
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
des_cap = {
"platformName": "android",
"appium:deviceName": "emulator-5554",
"appium:appPackage": "com.youdao.note",
"appium:appActivity": ".activity2.MainActivity",
"appium:automationName": "UiAutomator2",
"appium:unicodeKeyboard": True,
"appium:resetKeyboard": True,
"appium:autoGrantPermissions": True
}
# remote远程连接,appium_server:http://127.0.0.1:4723/wd/hub ,,模拟器手机连接参数desired_capabilities
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_capabilities=des_cap)
driver.implicitly_wait(10)
# id com.youdao.note:id/btn_ok 同意
driver.find_element(AppiumBy.ID,"com.youdao.note:id/btn_ok").click()
if driver.find_elements(AppiumBy.ID,"com.youdao.note:id/btn_cancel"):
driver.find_element(AppiumBy.ID, "com.youdao.note:id/btn_cancel").click()
# 我的
driver.find_element(AppiumBy.ID,"com.youdao.note:id/tab_mine").click()
time.sleep(2)
size = driver.get_window_size()
width = size['width']
height = size['height']
driver.swipe(width / 2, height * 9 / 10, width / 2, height / 10, 500)
time.sleep(1)
# 用户满意度调查
driver.find_element(AppiumBy.ID,"com.youdao.note:id/satisfaction_survey").click()
time.sleep(1)
# 选择8分
driver.find_element(AppiumBy.ID,"com.youdao.note:id/rate8").click()
time.sleep(1)
# 提交反馈
driver.find_element(AppiumBy.ID,"com.youdao.note:id/actionbar_menu_layout").click()
time.sleep(1)
# 加个有条件智能等待
wait=WebDriverWait(driver,10)
wait.until(EC.presence_of_element_located(driver.find_element(AppiumBy.ID, "com.youdao.note:id/text_title")))
# 获得消息弹出框值,
print(driver.find_element(AppiumBy.ID, "com.youdao.note:id/text_title").get_attribute("text"))
assert "谢谢参与调查!" in driver.find_element(AppiumBy.ID, "com.youdao.note:id/text_title").get_attribute("text")
time.sleep(1)
# 关闭提建议
driver.find_element(AppiumBy.ID,"com.youdao.note:id/btn_cancel").click()
time.sleep(2)
# driver.swipe(350,400,350,770,500)
size = driver.get_window_size()
width = size['width']
height = size['height']
driver.swipe(width / 2, height / 5, width / 2, height* 4 / 5, 500)
# 登录
driver.find_element(AppiumBy.ID,"com.youdao.note:id/head_image").click()
time.sleep(1)
# 手机验证码登陆
# /hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.ScrollView/android.widget.LinearLayout/android.widget.Button[3]
driver.find_element(AppiumBy.XPATH,"//android.widget.Button[3]").click()
time.sleep(5)
driver.quit()