appium学习总结12 - 强制、隐性、显性等待

11 篇文章 0 订阅

文章目录

1、处理异常

    # (3)if 来处理异常情况,比如 突然产生的弹框
    def test_002(self):
        ele = "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.view.ViewGroup/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.RelativeLayout[1]/android.widget.RelativeLayout/android.widget.ViewFlipper/android.widget.LinearLayout/android.widget.TextView"
        # 如果这里出现了 app升级框,我们通过if坐异常判断即可
        if self.driver.find_element(AppiumBy.XPATH, ele):
            self.driver.find_element(AppiumBy.XPATH, ele).click()
        self.driver.find_element(AppiumBy.ID, "com.xueqiu.android:id/search_input_text").send_keys("alibaba")

2、WebDriverWait 显性等待:lambda 表达式处理

    # (3)显性等待1:lambda表达式处理
    def test_004(self):
        ele = "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.view.ViewGroup/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.RelativeLayout[1]/android.widget.RelativeLayout/android.widget.ViewFlipper/android.widget.LinearLayout/android.widget.TextView"
        from selenium.webdriver.support.wait import WebDriverWait
        # 等待元素可见
        WebDriverWait(self.driver, 20).until(lambda x: self.driver.find_element(AppiumBy.XPATH, ele))
        self.driver.find_element(AppiumBy.XPATH, ele).click()
        self.driver.find_element(AppiumBy.ID, "com.xueqiu.android:id/search_input_text").send_keys("alibaba")

3、WebDriverWait 显性等待:expected_conditions 处理

    # (3)显性等待2:expected_conditions.visibility_of_element_located()
    def test_003(self):
        ele = "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.view.ViewGroup/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.RelativeLayout[1]/android.widget.RelativeLayout/android.widget.ViewFlipper/android.widget.LinearLayout/android.widget.TextView"

        from selenium.webdriver.support import expected_conditions as Ec
        from selenium.webdriver.support.wait import WebDriverWait
        # 等待元素可见
        WebDriverWait(self.driver, 20).until(Ec.visibility_of_element_located((AppiumBy.XPATH, ele)))
        self.driver.find_element(AppiumBy.XPATH, ele).click()
        self.driver.find_element(AppiumBy.ID, "com.xueqiu.android:id/search_input_text").send_keys("alibaba")

4、WebDriverWait 显性等待:自定义方法

    # (3)显性等待3:自定义函数
    def test_005(self):
        ele = "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.view.ViewGroup/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.RelativeLayout[1]/android.widget.RelativeLayout/android.widget.ViewFlipper/android.widget.LinearLayout/android.widget.TextView"

        from selenium.webdriver.support.wait import WebDriverWait

        # 等待元素可见
        def loaded(driver):
            print(datetime.now())
            if self.driver.find_element(AppiumBy.XPATH, ele):
                self.driver.find_element(AppiumBy.XPATH, ele).click()
                return True
            else:
                return False
        try:
            WebDriverWait(self.driver, 20, 0.2).until(loaded)
        except:
            print("no updates")
        self.driver.find_element(AppiumBy.ID, "com.xueqiu.android:id/search_input_text").send_keys("alibaba")

5、强制、隐性、显性等待完整代码示例

import time
from datetime import datetime
from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy


class TestDemo:

    def setup(self):
        caps = {
            "platformName": "android",
            "deviceName": "008640dd0804",
            "automationName": "uiautomator2",
            "appPackage": "com.xueqiu.android",
            "appActivity": ".view.WelcomeActivityAlias",
            "autoGrantPermissions": "true"
        }

        self.driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
        self.driver.find_element(AppiumBy.ID, "com.xueqiu.android:id/tv_agree").click()
        # (1) 隐形等待10秒:等待页面转圈完成
        self.driver.implicitly_wait(10)

    # (2)time.sleep() 死等
    def test_001(self):
        ele = "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.view.ViewGroup/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.RelativeLayout[1]/android.widget.RelativeLayout/android.widget.ViewFlipper/android.widget.LinearLayout/android.widget.TextView"
        self.driver.find_element(AppiumBy.XPATH, ele).click()
        time.sleep(20)
        el3 = self.driver.find_element(AppiumBy.ID, "com.xueqiu.android:id/search_input_text").send_keys("alibaba")

    # (3)if 来处理异常情况,比如 突然产生的弹框
    def test_002(self):
        ele = "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.view.ViewGroup/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.RelativeLayout[1]/android.widget.RelativeLayout/android.widget.ViewFlipper/android.widget.LinearLayout/android.widget.TextView"
        # 如果这里出现了 app升级框,我们通过if坐异常判断即可
        if self.driver.find_element(AppiumBy.XPATH, ele):
            self.driver.find_element(AppiumBy.XPATH, ele).click()
        self.driver.find_element(AppiumBy.ID, "com.xueqiu.android:id/search_input_text").send_keys("alibaba")

    # (3)显性等待1:lambda表达式处理
    def test_004(self):
        ele = "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.view.ViewGroup/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.RelativeLayout[1]/android.widget.RelativeLayout/android.widget.ViewFlipper/android.widget.LinearLayout/android.widget.TextView"
        from selenium.webdriver.support.wait import WebDriverWait
        # 等待元素可见
        WebDriverWait(self.driver, 20).until(lambda x: self.driver.find_element(AppiumBy.XPATH, ele))
        self.driver.find_element(AppiumBy.XPATH, ele).click()
        self.driver.find_element(AppiumBy.ID, "com.xueqiu.android:id/search_input_text").send_keys("alibaba")

    # (3)显性等待2:expected_conditions.visibility_of_element_located()
    def test_003(self):
        ele = "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.view.ViewGroup/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.RelativeLayout[1]/android.widget.RelativeLayout/android.widget.ViewFlipper/android.widget.LinearLayout/android.widget.TextView"

        from selenium.webdriver.support import expected_conditions as Ec
        from selenium.webdriver.support.wait import WebDriverWait
        # 等待元素可见
        WebDriverWait(self.driver, 20).until(Ec.visibility_of_element_located((AppiumBy.XPATH, ele)))
        self.driver.find_element(AppiumBy.XPATH, ele).click()
        self.driver.find_element(AppiumBy.ID, "com.xueqiu.android:id/search_input_text").send_keys("alibaba")

    # (3)显性等待3:自定义函数
    def test_005(self):
        ele = "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.view.ViewGroup/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.RelativeLayout[1]/android.widget.RelativeLayout/android.widget.ViewFlipper/android.widget.LinearLayout/android.widget.TextView"

        from selenium.webdriver.support.wait import WebDriverWait

        # 等待元素可见
        def loaded(driver):
            print(datetime.now())
            if self.driver.find_element(AppiumBy.XPATH, ele):
                self.driver.find_element(AppiumBy.XPATH, ele).click()
                return True
            else:
                return False
        try:
            WebDriverWait(self.driver, 20, 0.2).until(loaded)
        except:
            print("no updates")
        self.driver.find_element(AppiumBy.ID, "com.xueqiu.android:id/search_input_text").send_keys("alibaba")

    def teardown(self):
        self.driver.quit()

总结

写到这里也结束了,在文章最后放上一个小小的福利,以下为小编自己在学习过程中整理出的一个关于 java开发 的学习思路及方向。从事互联网开发,最主要的是要学好技术,而学习技术是一条慢长而艰苦的道路,不能靠一时激情,也不是熬几天几夜就能学好的,必须养成平时努力学习的习惯,更加需要准确的学习方向达到有效的学习效果。

由于内容较多就只放上一个大概的大纲,需要更及详细的学习思维导图的 点击我的Gitee获取
还有 高级java全套视频教程 java进阶架构师 视频+资料+代码+面试题!

全方面的java进阶实践技术资料,并且还有技术大牛一起讨论交流解决问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值