美文网首页
Robotframework-Appiumlibrary-通过索

Robotframework-Appiumlibrary-通过索

作者: 御剑逍遥 | 来源:发表于2017-07-15 00:52 被阅读0次
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扩展的相关文章,欢迎小伙伴们持续关注,谢谢。

相关文章

  • Robotframework-Appiumlibrary-通过索

    最近这段时间比较忙好久没跟朋友们一起分享技术话题了,今天接着上一篇的Robot继续跟大家分享Robotframew...

  • Robotframework-Appiumlibrary-长按扩

    Robot framework 降低了使用的门槛,同时也降低了代码的灵活性,扩展起来不那么方便,到底还能不能扩展,...

  • iOS系统键盘Window层注意事项

    划重点:UIRemoteKeyboardWindow是UITextEffectsWindow的子类 担心直接通过索...

  • K-9 Mail获取附件索引ID的解决方案

    以前的处理方法:优先通过X-Android-Attachment-StoreData获取,取不到再通过自己计算的索...

  • 十大最笨狗狗排名🐶

    波索尔名列第一。一般狗狗都是通过嗅觉来追捕猎物,但是波索尔追捕猎物依靠的是视觉。波索尔性格比较独特,除了主人,它们...

  • 安索夫矩阵及其变换

    1975年,策略管理之父安索夫(Igor Ansoff)博士提出安索夫矩阵。通过市场和产品两个维度,区分出:1)老...

  • 2021-03-31

    #HOSOER花索多肽酵素面膜 通过鱼胚胎测试的安全面膜

  • 买股票不要看价格

    《福布斯》富豪排行榜上的富豪都是通过经营或者投资企业成功成为富豪的,贝索斯通过创办亚马逊,比尔盖茨通过创办微软,巴...

  • 玩索而有得

    玩索而有得玩索而有得玩索而有得玩索而有得玩索而有得玩索而有得玩索而有得玩索而有得玩索而有得玩索而有得玩索而有得玩索...

  • 魔戒

    小说中的魔戒,是魔王索隆造出来的戒指,索隆可以通过它以邪恶统治世界。而这枚能使人隐形、长寿,获得超自然能力的戒指,...

网友评论

      本文标题:Robotframework-Appiumlibrary-通过索

      本文链接:https://www.haomeiwen.com/subject/ukqahxtx.html