app自动化测试(十二) app&ui自动化融合框架

从理念上Appium旨在满足移动端自动化需求,遵循四个原则

  • 1.你没有必要为了自动化而重新编译你的应用或者以任何方式修改它。 (Android、IOS系统自带框架)
  • 2.你不应该被限制在特定的语言或框架上来编写运行测试。 (WebDriver API客户端-服务端协议(称为Json Wire Protocol))
  • 3.移动端自动化框架在自动化接口方面不应该重造轮子。(WebDriver-Web浏览器自动化的W3C标准附加可用于移动端自动化的API方法)
  • 4.移动端自动化框架应该开源,不但在名义上,而且在精神和实践上都要实至名归。(Appium开源)

所以可见,appium是基于selenium的拓展,其许多的方法都是继承selenium中的方法
基于实际使用,框架的设计与UI自动化设计理念一致,在其用例设计理念上也与UI自动化设计理念一致
有兴趣了解,ui自动化是如何设计的同学,可以看下噢https://www.jianshu.com/p/d4aa0db3d027
利用appium进行Hybrid 应用中的H5页面场景和微信小程序场景时,我们进入webview中也需要使用selenium进行操作
再结合实际中,app常与web结合测试,联系上下场景进行测试等
所以将app与ui进行融合是必要的。

例如:appium中的截图保存方法,便继承的就是selenium中的截图

    def save_screenshot(self, filename):
        """
        Saves a screenshot of the current window to a PNG image file. Returns
           False if there is any IOError, else returns True. Use full paths in
           your filename.

        :Args:
         - filename: The full path you wish to save your screenshot to. This
           should end with a `.png` extension.

        :Usage:
            driver.save_screenshot('/Screenshots/foo.png')
        """
        return self.get_screenshot_as_file(filename)

再例如 不管是appium还是selenium都会使用下面两个
selenium.webdriver.support.expected_conditions这个Module
selenium.webdriver.support.wait.WebDriverWait这个Class
用于显性等待

    def wait_visible(self, page_name, loc, timeout=30, polling=0.5):
        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):
        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

所以为了将appium和selenium融合,我们首先要挑出双方共用的部分,避免重复封装
依照appium的设计原则,appium中有部分是继承selenium的,所以我们从appium方向入手
review appium.webdriver.webdriver.WebDriver类
发现WebDriver里面例如find_element等方法全部return的是selenium.webdriver.remote.webdriver中的execute方法
不过除了find_element外都有指定command不能通用


app自动化测试(十二) app&ui自动化融合框架_第1张图片
image.png

app自动化测试(十二) app&ui自动化融合框架_第2张图片
image.png

而且WebDriver继承了selenium.webdriver.remote.webdriver这个Module


app自动化测试(十二) app&ui自动化融合框架_第3张图片
image.png

app自动化测试(十二) app&ui自动化融合框架_第4张图片
image.png

app自动化测试(十二) app&ui自动化融合框架_第5张图片
image.png

所以如果只出现在appium.webdriver.webdriver.WebDriver类中的才需要二次封装提取成基础操作(除find_element&find_elements方法)

日常我们需要的基本操作有以下几个:

操作
上下左右滑动 TouchAction
等待 expected_conditions&WebDriverWait
查找元素(单个、全部) remote.webdriver
输入内容 webelement
点击 webelement
获取toast&文本内容 webelement
zoom&pinch MultiAction&TouchAction
获得source remote.webdriver
获得属性 remote.webdriver
截图 remote.webdriver
分辨率大小 remote.webdriver
切换app Activities
进入h5&小程序 太复杂了解释不清
获取全部contexts context
获取当前context context

其中等待、查找元素(单个、全部)、输入内容、点击、获得source、获得属性、截图、分辨率大小、获取toast&文本内容都是继承selenium中的方法
可以与ui自动化共用
其余的是需要进行多种操作封装的
tips: 有一个地方要注意

所以我们只要封装:上下左右滑动、zoom&pinch、切换app、进入h5&小程序、获取全部contexts&context

    def full_screen_scroll(self, page_name, direction="up", duration=200, timeout=30, polling=0.5):
        size = self.get_size(page_name, timeout=timeout, polling=polling)
        direction.lower()
        if direction == "up":
            start_x, start_y, end_x, end_y = \
                size["width"] * 0.5, size["height"] * 0.8, size["width"] * 0.5, size["height"] * 0.2
        elif direction == "down":
            start_x, start_y, end_x, end_y = \
                size["width"] * 0.5, size["height"] * 0.2, size["width"] * 0.5, size["height"] * 0.8
        elif direction == "right":
            start_x, start_y, end_x, end_y = \
                size["width"] * 0.2, size["height"] * 0.5, size["width"] * 0.8, size["height"] * 0.5
        elif direction == "left":
            start_x, start_y, end_x, end_y = \
                size["width"] * 0.8, size["height"] * 0.8, size["width"] * 0.2, size["height"] * 0.2
        else:
            logger.error("Model Error value: {}".format(direction))
            raise TypeError("Model Error value: {}".format(direction))
        try:
            self.driver.swipe(start_x, start_y, end_x, end_y, duration)
        except:
            logger.error("")
            self.save_screen_shot(page_name)
        else:
            logger.info("")
            return self

    def zoom(self, page_name):
        size = self.get_size(page_name)
        ma = MultiAction(self.driver)
        ta1 = TouchAction(self.driver)
        ta2 = TouchAction(self.driver)
        ta1.press(x=size["width"] * 0.5 - 1, y=size["height"] * 0.5 - 1).wait(200). \
            move_to(x=size["width"] * 0.1, y=size["height"] * 0.1).wait(200).release()
        ta2.press(x=size["width"] * 0.5 + 1, y=size["height"] * 0.5 + 1).wait(200). \
            move_to(x=size["width"] * 0.9, y=size["height"] * 0.9).wait(200).release()
        ma.add(ta1, ta2)
        ma.perform()

    def pinch(self, page_name):
        size = self.get_size(page_name)
        ma = MultiAction(self.driver)
        ta1 = TouchAction(self.driver)
        ta2 = TouchAction(self.driver)
        ta1.press(x=size["width"] * 0.2, y=size["height"] * 0.1).wait(200). \
            move_to(x=size["width"] * 0.5 - 1, y=size["height"] * 0.5 - 1).wait(200).release()
        ta2.press(x=size["width"] * 0.8, y=size["height"] * 0.7).wait(200). \
            move_to(x=size["width"] * 0.5 + 1, y=size["height"] * 0.5 + 1).wait(200).release()
        ma.add(ta1, ta2)
        ma.perform()

    @property
    def get_contexts(self):
        contexts = self.driver.contexts
        logger.info("{}".format(contexts))
        return contexts

    @property
    def get_current_context(self):
        context = self.driver.context
        logger.info("{}".format(context))
        return context

    def switch_context(self, page_name, class_name='android.webkit.WebView', index=-1):
        wait_loc = (MobileBy.CLASS_NAME, class_name)
        self.wait_all_visible(page_name, wait_loc, timeout=10)
        time.sleep(3)  # Wait for H5 page to load
        current = self.get_current_context
        context = self.get_contexts.remove(current)[index]
        try:
            self.driver.switch_to.context(context)
            if current == self.get_current_context:
                raise NoSuchContextException("Failed to switch context {}".format(context))
        except NoSuchContextException as e:
            self.save_screen_shot(page_name)
            logger.error("{},{}".format(e, self.get_current_context))
        else:
            logger.info("{}".format(context))
            return self

    def switch_app(self, page_name, package, activity):
        try:
            self.driver.start_activity(package, activity)
        except:
            self.save_screen_shot(page_name)
            logger.error("")
        else:
            logger.info("")

你可能感兴趣的:(app自动化测试(十二) app&ui自动化融合框架)