app自动化测试(十一) 基础操作类封装

在用appium原生方法进行编写时,我们时常要进行这样的组合
等待+功能+日志
基于pageobject的思想,我们将常用到的方法进行二次封装
方法封装公式:前置条件(等待)+异常捕获+功能操作+日志
这样的封装法,将等待等前置条件都集成到一个操作中,减少编写用例时的代码量。
做到每个操作都会进行等待+异常捕获+日志

以下是部分功能的二次封装demo

import os
import time
from appium.webdriver.webdriver import WebDriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common import exceptions
from common.logger import logger
from common.constant import SCREEN_SHOT_PATH


class BaseOperation:

    def __init__(self, driver: WebDriver):
        self.driver = driver

    def save_screen_shot(self, file_name, path=None):
        """Save screenshot
        
        :param file_name: Screenshot name. Suggest:Page name
        :param path: 
        :return:
        """
        if not path :
            path = SCREEN_SHOT_PATH
        path = os.path.join(path , file_name)
        try:
            self.driver.save_screenshot(path)
        except:
            logger.error("{} Save screen shot failed".format(file_name))
            raise
        else:
            logger.info("{} Save screen shot successful".format(file_name))

    def wait_visible(self, page_name, loc, timeout=30, polling=0.5):
        """waiting element visible
        
        :param page_name: Page name
        :param path: locator (Mobile.XX, Keyword)
        :param timeout: timeout
        :param polling: poll_frequency
        :return: self
        """
        try:
            wait = WebDriverWait(self.driver, timeout, polling)
            wait.until(EC.visibility_of_element_located(loc))
        except:
            self.save_screen_shot(page_name)
            logger.error("Wait visibility of element location {} failed".format(loc))
        else:
            logger.info("Wait visibility of element location {} successful".format(loc))
            return self

    def wait_all_visible(self, page_name, loc, timeout=30, polling=0.5):
        """waiting all element visible
        
        :param page_name: Page name
        :param path: global locator (Mobile.XX, Keyword)
        :return: self
        """
        try:
            wait = WebDriverWait(self.driver, timeout, polling)
            wait.until(EC.visibility_of_all_elements_located(loc))
        except:
            self.save_screen_shot(page_name)
            logger.error("Wait visibility of all elements {} failed".format(page_name))
        else:
            logger.info("Wait visibility of all element location {} successful".format(loc))
            return self

    def wait_presence(self, page_name, loc, timeout=30, polling=0.5):
        try:
            wait = WebDriverWait(self.driver, timeout, polling)
            wait.until(EC.presence_of_element_located(loc))
        except:
            self.save_screen_shot(page_name)
            logger.error("")
        else:
            logger.info("")
            return self

    def wait_all_presence(self, page_name, loc, timeout=30, polling=0.5):
        try:
            wait = WebDriverWait(self.driver, timeout, polling)
            wait.until(EC.visibility_of_all_elements_located(loc))
        except:
            self.save_screen_shot(page_name)
            logger.error("")
        else:
            logger.info("")
            return self

    def lookup_element(self, page_name, loc, timeout=30, polling=0.5):
        self.wait_visible(page_name, loc, timeout, polling)

        try:
            ele = self.driver.find_element(*loc)
        except:
            logger.error("")
            self.save_screen_shot(page_name)
        else:
            logger.info("")
            return ele

    def click(self, page_name, loc, timeout=30, polling=0.5):
        ele = self.lookup_element(page_name, loc, timeout, polling)
        try:
            ele.click()
        except:
            logger.error("")
            self.save_screen_shot(page_name)
        else:
            logger.info("")
            return self

    def send_keys(self, page_name, loc, value, timeout=30, polling=0.5):
        ele = self.lookup_element(page_name, loc, timeout, polling)
        try:
            ele.send_keys(value)
        except:
            logger.error("")
            self.save_screen_shot(page_name)
        else:
            logger.info("")
            return self

你可能感兴趣的:(app自动化测试(十一) 基础操作类封装)