简体   繁体   中英

Appium | Robot Framework | Unable to run a keyword to find an element using a custom locator strategy

I am using Robot Framework and Appium to automate an Android native app. We have moved to espresso driver for running these tests and the elements need to be identified using the view-tag locator. However, Appium Robot library does not have support for this locator strategy. I have written the custom keyword below

from robot.libraries.BuiltIn import BuiltIn
from robot.api.deco import keyword


@keyword(name='Find by ViewTag')
def by_viewtag(tagname):
    """Provides support to find elements using view tag for Espresso driver on Android"""
    appiumlib = BuiltIn().get_library_instance('AppiumLibrary')
    driver = appiumlib._current_application()
    el = driver.find_element_by_android_viewtag(tagname)
    print(el)
    return el

and I am using it in my page file to find the object like this:

*** Settings ***
Library  ../../../../Resources/Utils/find_elements_utils.py
Library  BuiltIn
Library  AppiumLibrary
Resource  ../../../../Resources/Utils/helpers.robot

*** Variables ***
${loginBtn} =               id=btn_sign_in
${signUpEmail}=  Call Method  Find by ViewTag

However, running this throws the following error

Element locator 'Call Method Find by ViewTag' did not match any elements after 20 seconds

Robot framework thinks I am passing an element locator when I am trying to call a keyword to find the locator. Can someone please help me with this? Is there any additional function I need to write to make this happen?

Please help!

The *** Variables *** table can only define static strings, it can't call other keywords. You are defining ${signUpEmail} as the literal string "Call Method Find by ViewTag" (minus the quotes). Anywhere you use it, that full string will be what is passed to a keyword.

If you want to call your Find by ViewTag keyword, you don't need to use Call Method . It's a normal keyword so you can call it in a normal way. For example:

${result}=  Find By ViewTag  a_view_tag

I solved this problem by extending the Appium library to support the view-tag strategy. Along with the function I also needed to update the strategies object in the constructor of Appium Library.

 def __init__(self):
        """Initialize extended locators."""
        ElementFinder.__init__(self)
        strategies = {
            'viewtag': self._by_viewtag,
        }
        self._strategies.update(strategies)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM