Robotframework-Appiumlibrary-通过索引定位元素扩展

image.png

最近这段时间比较忙好久没跟朋友们一起分享技术话题了,今天接着上一篇的Robot继续跟大家分享Robotframework-Appiumlibrary通过索引定位元素。

1.应用场景

做Android自动化测试的朋友肯定遇到过具有相同ID或Class的无从操作的时候,很多人会想到加个索引呀,没错索引确实能解决这个问题,但是Robotframework-Appiumlibrary是不支持索引的,这就需要我们进行扩展,比如下面的微信表情,全部是相同的ID。


image.png

2.定位代码

   #通常我们用这个方法来操作点击元素
    def click_element(self, locator):
        """Click element identified by `locator`.
        Key attributes for arbitrary elements are `index` and `name`. See
        `introduction` for details about locating elements.
        """
        self._info("Clicking element '%s'." % locator)
        self._element_find(locator, True, True).click()
  #通过这个方法来定位元素
   def _element_find(self, locator, first_only, required, tag=None):
        application = self._current_application()
        if isstr(locator):
            # Normalize any unicode as explained here, http://appium.io/slate/en/master/?javascript#multi-lingual-support
            if self._get_platform() == 'ios':
                _locator = normalize('NFD', locator)
            else:
                _locator = locator
            elements = self._element_finder.find(application, _locator, tag)
            if required and len(elements) == 0:
                raise ValueError("Element locator '" + locator + "' did not match any elements.")
            if first_only:
                if len(elements) == 0: return None
                return elements[0]
        elif isinstance(locator, WebElement):
            elements = locator
        # do some other stuff here like deal with list of webelements
        # ... or raise locator/element specific error if required
        return elements

通过这两个方法我们可以确定第二个定位元素的方法找到是一个数组elements,这说明这个方法是可以找到相同ID或Class的全部元素的,另外有个参数first_only表示默认取第一个元素,这下我们就清楚了,当我们传参数first_only为false的时候即可返回全部元素,然后我们通过数组下标来获取对应的元素。

3.增加通过索引点击元素的方法

   #新增一个方法,并且增加一个索引参数
    def click_elementsByIndex(self, locator,index):
        """Click element identified by `locator` and `index`.

        Key attributes for arbitrary elements are `index` and `name`.
        See`introduction` for details about locating elements.
        Args:
        - ``locator`` - find elements by locator
        - ``index`` - click the element with index
        """
        self._info("Clicking element '%s'." % locator)
        self._element_find(locator, False, True)[int(index)].click()

这段代码即可通过ID或Class+索引来操作元素

4.RIDE检验结果

重新安装之后,F5即可看到添加的最新关键字


image.png

5.实际应用

我们再去操作一下微信表情的点

1.分层实现的业务关键字
image.png
2.分层实现的基础关键字
image.png
后续我会陆续退出Robot framework扩展的相关文章,欢迎小伙伴们持续关注,谢谢。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,981评论 19 139
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,767评论 18 399
  • 最近朋友圈有一个很有意思的现象,很多人都爱发一些自己看过的书,喜欢的文章,最近在学什么课程,自己如何通过学习来提升...
    梁楽然阅读 801评论 0 0
  • 体验经济:人们正在从被动消费变为主动参与;其次,最佳体验不是在公司总部编写出来的,而是有服务提供者在现场提供给顾客...
    tiancijiaren阅读 355评论 0 0
  • iOS 10 以下系统: 1. 添加通知: 2. 移除通知: 3. 移除全部还未触发的通知: iOS 10以上版本...
    钢铁少侠阅读 1,040评论 0 1