App automated testing - app pop-up window exception handling

insert image description here



Pop-up frames from time to time during the running process (advertisement pop-up window, upgrade prompt box, new message prompt box, etc.) The
pop-up frame is not a BUG (UI interface prompt, warning function)

1. Abnormal pop-up window display

insert image description here

2. Blacklist processing process:

insert image description here

3. Blacklist processing code:

During the execution of automated testing, when searching for an element through the find method, if an exception pop-up window appears, there will be an exception that the element cannot be found. At this time, the positioning element that closes the pop-up window needs to be added to the blacklist black_list middle.
Through try...except, if the element to be located cannot be found, traverse the blacklist list to determine whether there is an abnormal element, if there is an abnormal element, close it; then continue to find the business element, if the business element is still not found, continue Traverse the elements in the blacklist and close them; if the business element is still not found after traversing the elements in the blacklist, it means that the element is not found, and a NoSuchElement error will be reported.

    def find(self,by,locator):
        try:
            return self.driver.find_element(by,locator)
        except Exception as e:
            print('没有找到元素,处理异常')
            for black in self.black_list:
                #查找黑名单中的每一个元素
                elements=self.driver.find_elements(*black)
                if len(elements)>0:
                    elements[0].click()
                    return self.find(by,locator)
            #todo 遍历完黑名单之后,如果仍然没有找到元素,就抛出异常
            raise e

Fourth, the advantages of decorators

Functional enhancements to the original functions
without changing the logic of the original functions
to make the code more concise and easy to maintain

class BasePage:

    def __init__(self,driver=None):    #加WebDriver代码就有提示了
        self.driver=driver

    @black_wrapper
    def find(self,by,locator):
        return self.driver.find_element(by, locator)

The find method is passed to the decorator black_wrapper(func), which means that the func in the decorator represents the find method.
The parameters self, by, and locator in the find method are passed to the run method in the decorator. The self in the run method can be obtained by self = args[0]*args

import time
import logging
import os
import allure
from appium.webdriver.common.appiumby import AppiumBy



# todo 黑名单列表
black_list = [(AppiumBy.ID, "com.xueqiu.android:id/ib_close")]


# todo func相当于find方法
def black_wrapper(func):
    def run(*args, **kwargs):
        from base.base_page import BasePage
        basepage:BasePage = args[0]
        try:
            logging.info(f"开始查找元素{args[2]}")
            return func(*args, **kwargs)
        except Exception as e:
            logging.warning("为找到元素,处理异常")
            # 以当前时间命名的截图
            curtime=basepage.get_time()
            tmp_name=curtime+'.png'
            #todo  获取当前文件black_handle.py所在的路径
            # os.path.dirname(__file__)
            # 上一级路径与当前路径进行拼接
            tmp_name=os.path.join(os.path.dirname(__file__),'..','images',tmp_name)
            logging.info(f"当前截图的路径为————————》:{tmp_name}")
            basepage.screenshot(tmp_name)
            #todo 截图添加到测试报告中
            allure.attach.file(tmp_name,
                               name="截图",
                               attachment_type=allure.attachment_type.PNG)

      
            for black in black_list:
                # 查找黑名单中的每一个元素
                logging.warning(f"处理黑名单:{black}")
                elements = basepage.driver.find_elements(*black)
                if len(elements) > 0:
                    elements[0].click()
                    return func(*args, **kwargs)
            # todo 遍历完黑名单之后,如果仍然没有找到元素,就抛出异常
            logging.error(f"遍历黑名单,仍然未找到元素信息————》{e}")
            raise e

    return run

Guess you like

Origin blog.csdn.net/YZL40514131/article/details/130543238