机器人框架 - 将Appium驱动程序传递给Python脚本

2024-05-18 11:41:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Python将Robot框架与Appium集成。但是我不知道如何将在Robot框架中创建的Appium驱动程序传递给定制的python脚本。

我的环境:

  • Mac OS-小牛
  • Appium 1.2(通过自制程序安装)
  • 最新的机器人框架(通过pip安装)
  • Robot框架的最新Appium库(通过pip安装)

我在Python中有一个正在工作的Appium脚本,但是我想开始使用Robot框架来处理实际的测试。

使用python脚本的部分代码:

wd = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
wd.find_element_by_name("Start").click()
wd.find_element_by_xpath("//UIAApplication[1]/UIAWindow[1]/UIATableView[1]/UIATableCell[1]").click()
wd.execute_script("var vkeyboard = target.frontMostApp().keyboard(); vKeyboard.typeString(\"Test Text\");")
wd.execute_script("var vKeyboard = target.frontMostApp().keyboard(); vKeyboard.buttons()['Return'].tap();")

如您所见,由于应用程序是如何工作的,我不得不使用execute_脚本作为脚本的一部分。

机器人框架的Appium库不公开execute_脚本,因此我需要在python库中编写自己的脚本。

以下是我的robot测试脚本的开始部分,直到需要执行脚本时为止:

TestStart
    Open Application   ${REMOTE_URL}    ${PLATFORM_NAME}    ${PLATFORM_VERSION}    ${DEVICE_NAME}    ${APP}
    Click Element    name=Start
    Click Element    xpath=//UIAApplication[1]/UIAWindow[1]/UIATableView[1]/UIATableCell[1]

我的问题是如何获取在开放应用程序中创建的驱动程序实例并将其传递给Python脚本?

我有一个python脚本,它包含以下内容:

def KeyboardType(driver):
    driver.execute_script("var vKeyboard = target.frontMostApp().keyboard(); vKeyboard.typeString(\"hi there\");")

但是,我似乎无法将驱动程序从Robot框架脚本传递给这个python脚本。

我尝试通过以下方式将打开的应用程序设置为变量:

${Driver}  Open Application   ${REMOTE_URL}    ${PLATFORM_NAME}    ${PLATFORM_VERSION}    ${DEVICE_NAME}    ${APP}
KeyboardType  ${Driver}

但我收到了一个错误:

AttributeError:“str”对象没有“execute\u script”属性

我还尝试将Get Current Context的结果传递到python脚本中,但随后得到:

AttributeError:“unicode”对象没有“execute\u script”属性

如何将Robot框架创建的驱动程序传递到python脚本中?


Tags: name脚本框架targetexecutevar驱动程序script