六、Appium-python-UI自動化之記一次使用隱式等待: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針對局部操作設置超時時間

 

完美解決!!!

 

發佈了49 篇原創文章 · 獲贊 3 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章