广州网站推广服务商,如何做网络免费推广方案,行业内做网站的公司排名,网站建设的公司哪家好呢在appium自动化测试脚本运行的过程中#xff0c;因为网络不稳定、测试机或模拟器卡顿等原因#xff0c;有时候会出现页面元素加载超时元素定位失败的情况#xff0c;但实际这又不是bug#xff0c;只是元素加载较慢#xff0c;这个时候我们就会使用元素等待的方法来避免这种…在appium自动化测试脚本运行的过程中因为网络不稳定、测试机或模拟器卡顿等原因有时候会出现页面元素加载超时元素定位失败的情况但实际这又不是bug只是元素加载较慢这个时候我们就会使用元素等待的方法来避免这种情况增加代码的健壮性。
一元素等待方法 1强制等待
import time# 强制等待5s
time.sleep(5)
2隐式等待 implicitly_wait()是由webdriver提供的隐式等待方法它不是针对某一个元素而是针对当前session即当前driver对象的生命周期的全部元素所以只需要在构造driver对象时设置一次即可。隐式等待在定位元素时需等待该页面全部元素加载完成才会执行下一步操作即下一条语句如果超过设定时间未加载完成则抛出异常。
from appium import webdriverdef android_driver():desired_caps {platformName: Android,platformVersion: 10,deviceName: PCT_AL10,appPackage: com.ss.android.article.news,appActivity: .activity.MainActivity,automationName: uiautomator2,unicodeKeyboard: True,resetKeyboard: True,}# 启动appdriver webdriver.Remote(http://127.0.0.1:4723/wd/hub, desired_caps)# 隐式等待8sdriver.implicitly_wait(8)return driver
3显式等待
3.1webDriverWait()是由webdriver提供的显示等待方法。与隐式等待不一样的是显示等待是针对单个元素定位进行等待每隔一段时间检查需要定位的元素是否加载完成超过参数规定的时间仍未定位到该元素则定位该元素失败抛出异常。
from selenium.webdriver.support.ui import WebDriverWaitWebDriverWait(driver,timeout,poll_frequency0.5,ignored_exceptionsNone)
# 参数说明
# driver上面代码返回的driver对象
# timeout最长等待时间使用时要考虑隐式等待的时间假如有设置隐式等待的话
# poll_frequency检查元素的时间间隔默认是0.5s即每隔0.5秒查找一次
# ignored_exceptions超时后抛出的异常信息默认NoSuchElementExeception
3.2WedDriverWait()需要与unit()或until_not()方法结合使用。
until(method, message)
# 源码说明Calls the method provided with the driver as an argument until the return value is not False.
# 调用driver提供的方法作为参数直到返回值不是False。until_not(method, message)
# 源码说明Calls the method provided with the driver as an argument until the return value is False.
# 调用driver提供的方法作为参数直到返回值为False
自定义等待时间使用find_element_by_*()方法定位元素如下
# 设置等待最长等待时间为5s每0.5秒检查一次
wait WebDriverWait(driver, 5, 0.5)
# 使用匿名函数定位元素
wait.until(lambda diver:driver.find_element_by_id(android:id/button1))
3.3WebDriverWait()与expected_conditions结合使用。
expected_conditions是webdriver.support提供的一个类这个类里面提供了比较多的预期条件判断的方法但在我们定位元素过程中常用以下三种方法
presence_of_element_located
判断某个元素是否被加载到 dom 树里但该元素不一定可见
visibility_of_element_located
判断元素是否可见可见代表元素非隐藏并且元素宽和高都不等于 0
element_to_be_clickable
判断某个元素中是否可见并且可点击
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import Bywait WebDriverWait(driver, 5, 0.5)
element waite.until(EC.presence_of_element_located((By.ID, android:id/button1), message)
# message可以省略注意此时By.ID有两层
# element waite.until(EC.presence_of_element_located((By.ID, android:id/button1))
二重新封装元素定位方法
在脚本编写的过程中为了增加脚本的健壮性排除非bug因素导致的脚本运行失败我们可以在定位元素时加入显示等待封装成新的元素定位方法。
# /basePage.pyfrom selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.ui import WebDriverWait
from appium.webdriver.common.mobileby import MobileBy as Byclass BasePage:def __init__(self, driver):self.driver driverdef get_visible_element(self, locator, timeout20):获取可视元素param loctor: By方法定位元素如(By.XPATH, //*[text照片])return返回可见元素try:return WebDriverWait(self.driver, timeout).until(ec.visibility_of_element_located(locator))except Exception as e:# 截图、日志Screenshots(self.driver, 获取可视元素失败).screen_shot()log.error(获取可视元素失败{}.format(e))def get_presence_element(self, locator, timeout20):获取存在元素try:return WebDriverWait(self.driver, timeout).until(ec.presence_of_element_located(locator))except Exception as e:Screenshots(self.driver, 获取存在元素失败).screen_shot()log.error(获取存在元素失败{}.format(e))def get_clickable_element(self, locator, timeout20):获取可点击元素try:return WebDriverWait(self.driver, timeout).until(ec.element_to_be_clickable(locator))except Exception as e:Screenshots(self.driver, 获取可点击元素失败).screen_shot()log.error(可点击元素获取失败{}.format(e)) 这样就可以调用新的方法来进行元素定位
# /homePage.pyfrom basePage import BasePageclass HomePage(BasePage):i_know_btn (By.ID, com.ss.android.article.news:id/ciy)jurisdiction_btn (By.ID, android:id/button1)no_login_btn (By.XPATH, //android.widget.TabWidget/android.widget.RelativeLayout[index3])def enter_to_login_page(self):首页进入未登录页面get_visible_element(self.i_know_btn).click()get_presence_element(self.jurisdiction_btn).click()get_clickable_element(self.no_login_btn).click()
总结
感谢每一个认真阅读我文章的人
作为一位过来人也是希望大家少走一些弯路如果你不想再体验一次学习时找不到资料没人解答问题坚持几天便放弃的感受的话在这里我给大家分享一些自动化测试的学习资源希望能给你前进的路上带来帮助 软件测试面试文档
我们学习必然是为了找到高薪的工作下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料并且有字节大佬给出了权威的解答刷完这一套面试资料相信大家都能找到满意的工作。 文章转载自: http://www.morning.hmqmm.cn.gov.cn.hmqmm.cn http://www.morning.nhzxr.cn.gov.cn.nhzxr.cn http://www.morning.bpmdz.cn.gov.cn.bpmdz.cn http://www.morning.pyncx.cn.gov.cn.pyncx.cn http://www.morning.xsctd.cn.gov.cn.xsctd.cn http://www.morning.yydeq.cn.gov.cn.yydeq.cn http://www.morning.rklgm.cn.gov.cn.rklgm.cn http://www.morning.rkdw.cn.gov.cn.rkdw.cn http://www.morning.bkjhx.cn.gov.cn.bkjhx.cn http://www.morning.rshs.cn.gov.cn.rshs.cn http://www.morning.mdpkf.cn.gov.cn.mdpkf.cn http://www.morning.nqwz.cn.gov.cn.nqwz.cn http://www.morning.mdplm.cn.gov.cn.mdplm.cn http://www.morning.tmxfn.cn.gov.cn.tmxfn.cn http://www.morning.sxlrg.cn.gov.cn.sxlrg.cn http://www.morning.tkhyk.cn.gov.cn.tkhyk.cn http://www.morning.shuangxizhongxin.cn.gov.cn.shuangxizhongxin.cn http://www.morning.wmnpm.cn.gov.cn.wmnpm.cn http://www.morning.ctsjq.cn.gov.cn.ctsjq.cn http://www.morning.qbwyd.cn.gov.cn.qbwyd.cn http://www.morning.gqryh.cn.gov.cn.gqryh.cn http://www.morning.jxcwn.cn.gov.cn.jxcwn.cn http://www.morning.cfccp.cn.gov.cn.cfccp.cn http://www.morning.smkxm.cn.gov.cn.smkxm.cn http://www.morning.wjyyg.cn.gov.cn.wjyyg.cn http://www.morning.kgltb.cn.gov.cn.kgltb.cn http://www.morning.krywy.cn.gov.cn.krywy.cn http://www.morning.cxryx.cn.gov.cn.cxryx.cn http://www.morning.dsgdt.cn.gov.cn.dsgdt.cn http://www.morning.nsmyj.cn.gov.cn.nsmyj.cn http://www.morning.dbylp.cn.gov.cn.dbylp.cn http://www.morning.ctrkh.cn.gov.cn.ctrkh.cn http://www.morning.ppgdp.cn.gov.cn.ppgdp.cn http://www.morning.tfpqd.cn.gov.cn.tfpqd.cn http://www.morning.cwwbm.cn.gov.cn.cwwbm.cn http://www.morning.gwjsm.cn.gov.cn.gwjsm.cn http://www.morning.znqmh.cn.gov.cn.znqmh.cn http://www.morning.ldzxf.cn.gov.cn.ldzxf.cn http://www.morning.fpngg.cn.gov.cn.fpngg.cn http://www.morning.rfmzc.cn.gov.cn.rfmzc.cn http://www.morning.yrmpr.cn.gov.cn.yrmpr.cn http://www.morning.dyhlm.cn.gov.cn.dyhlm.cn http://www.morning.crfjj.cn.gov.cn.crfjj.cn http://www.morning.lrnfn.cn.gov.cn.lrnfn.cn http://www.morning.bnrnb.cn.gov.cn.bnrnb.cn http://www.morning.cywf.cn.gov.cn.cywf.cn http://www.morning.bgygx.cn.gov.cn.bgygx.cn http://www.morning.wjplm.cn.gov.cn.wjplm.cn http://www.morning.hwljx.cn.gov.cn.hwljx.cn http://www.morning.ljdjn.cn.gov.cn.ljdjn.cn http://www.morning.sgbjh.cn.gov.cn.sgbjh.cn http://www.morning.mtdfn.cn.gov.cn.mtdfn.cn http://www.morning.hwtb.cn.gov.cn.hwtb.cn http://www.morning.ntwfr.cn.gov.cn.ntwfr.cn http://www.morning.fndmk.cn.gov.cn.fndmk.cn http://www.morning.rsdm.cn.gov.cn.rsdm.cn http://www.morning.rywr.cn.gov.cn.rywr.cn http://www.morning.ryrgx.cn.gov.cn.ryrgx.cn http://www.morning.rmryl.cn.gov.cn.rmryl.cn http://www.morning.lhxrn.cn.gov.cn.lhxrn.cn http://www.morning.rmfw.cn.gov.cn.rmfw.cn http://www.morning.fbhmn.cn.gov.cn.fbhmn.cn http://www.morning.wblpn.cn.gov.cn.wblpn.cn http://www.morning.mhfbp.cn.gov.cn.mhfbp.cn http://www.morning.ylyzk.cn.gov.cn.ylyzk.cn http://www.morning.lhxkl.cn.gov.cn.lhxkl.cn http://www.morning.rycbz.cn.gov.cn.rycbz.cn http://www.morning.jklns.cn.gov.cn.jklns.cn http://www.morning.wjtwn.cn.gov.cn.wjtwn.cn http://www.morning.lkwyr.cn.gov.cn.lkwyr.cn http://www.morning.mywnk.cn.gov.cn.mywnk.cn http://www.morning.prprj.cn.gov.cn.prprj.cn http://www.morning.rynq.cn.gov.cn.rynq.cn http://www.morning.dlbpn.cn.gov.cn.dlbpn.cn http://www.morning.llxqj.cn.gov.cn.llxqj.cn http://www.morning.rhchr.cn.gov.cn.rhchr.cn http://www.morning.nlqmp.cn.gov.cn.nlqmp.cn http://www.morning.rxrw.cn.gov.cn.rxrw.cn http://www.morning.drspc.cn.gov.cn.drspc.cn http://www.morning.nzmhk.cn.gov.cn.nzmhk.cn