Appium-Python3--UI自動化-[-8-]-記一次使用隱式等待:implicitly_wait()的坑


情景描述:

  APP首次登錄時通常會有位置授權操作,APP-UI自動化時需要檢測該授權彈框是否存在,如果存在則需要授權,如果不存在則進行下一步

邏輯代碼如下:

        MyLog.logger().info("檢查位置授權彈框之前時間為:" + str(datetime.datetime.now()))
        
        # 檢查位置授權是否彈出
        is_show = self.login_page_auth_location_check_is_or_not_show()

        MyLog.logger().info("檢查位置授權彈框之后時間為::" + str(datetime.datetime.now()))

        MyLog.logger().info("位置授權是否存在:"+str(is_show))

        # app正常啟動,截圖保存
        common.take_screenShot(self.driver,u"啟動頁面")
        # MyLog.logger().info("現在時間為3:" + str(datetime.datetime.now()))

        if is_show is True:

            # 獲取位置授權
            self.login_page_auth_location()

全局的implicitly_wait()時間我設置成30秒

self.driver.implicitly_wait(30)

檢測授權彈框是否存在的方法:

# 獲取toast元素
def is_toast_exist(driver, text=None, timeout=5, poll_frequency=0.01):
    try:

        toast_loc = ("xpath", ".//*[contains(@text,'%s')]" % text)
        WebDriverWait(driver, timeout, poll_frequency).until(EC.presence_of_element_located(toast_loc))
        return True

    except Exception as e:

        return False

 

無需授權----結果日志顯示:

'''
2020-02-17 18:43:33,443 INFO test_sales_login_module.test_sales_Login_module Line:38 檢查位置授權彈框之前時間為:2020-02-17 18:43:33.443920
2020-02-17 18:44:05,150 INFO test_sales_login_module.test_sales_Login_module Line:42 檢查位置授權彈框之后時間為::2020-02-17 18:44:05.150074
2020-02-17 18:44:05,150 INFO test_sales_login_module.test_sales_Login_module Line:44 位置授權是否存在:False
2020-02-17 18:44:05,765 INFO test_sales_login_module.login_username Line:95 ============定位登錄員工號輸入框。。。。
2020-02-17 18:44:06,461 INFO test_sales_login_module.login_password Line:105 ============定位登錄密碼輸入框。。。。
'''

需進行授權----結果日志顯示:

'''
2020-02-17 18:55:00,960 INFO test_sales_login_module.test_sales_Login_module Line:38 檢查位置授權彈框之前時間為:2020-02-17 18:55:00.960350
2020-02-17 18:55:04,057 INFO test_sales_login_module.test_sales_Login_module Line:43 檢查位置授權彈框之后時間為::2020-02-17 18:55:04.057558
2020-02-17 18:55:04,057 INFO test_sales_login_module.test_sales_Login_module Line:45 位置授權是否存在:True
2020-02-17 18:55:04,502 INFO test_sales_login_module.login_page_auth_location Line:126 ============接受授權位置按鈕。。。。
2020-02-17 18:55:04,586 INFO test_sales_login_module.login_username Line:96 ============定位登錄員工號輸入框。。。。
2020-02-17 18:55:06,335 INFO test_sales_login_module.login_password Line:106 ============定位登錄密碼輸入框。。。。
'''

分析:

1.如果【需進行授權】,檢查彈框花費了不到4s的時間

2.如果 【無需授權】,檢查彈框花費了大約30s多的時間

因此得出,無需授權時,等待時間太長,全局的implicitly_wait()時間設置為30秒

 

改進:

全局的implicitly_wait()時間盡量縮短,否則會影響性能

1.我們可以將全局時間等待設置3-5s

2.檢測授權彈框是否存在的方法中可以單獨設置timeout針對局部操作設置超時時間

 

完美解決!!!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM