Robot Framework And Appium
官方文档:
定位元素
比如Android界面上有一个TEST 按钮,在Appium中它显示的属性如下:
可以通过如下的方式来点击这个按钮
通过id
Click Element com.example.appiumlibrarytest:id/button1
通过class
Click Element
Click Element
或者通过resource-id,如
Click Element
Click Element
SeekBar拖动
可以参考:
主要代码如下,也很好理解,例子是上下滑动的,可以很方便的修改为左右滑动
Swipe Up
${element_size}= Get Element Size id=sample_content_fragment
${element_location}= Get Element Location id=sample_content_fragment
${start_x}= Evaluate ${element_location['x']} + (${element_size['width']} * 0.5)
${start_y}= Evaluate ${element_location['y']} + (${element_size['height']} * 0.7)
${end_x}= Evaluate ${element_location['x']} + (${element_size['width']} * 0.5)
${end_y}= Evaluate ${element_location['y']} + (${element_size['height']} * 0.3)
Swipe ${start_x} ${start_y} ${end_x} ${end_y} 500
Sleep 1
Swipe Down
${element_size}= Get Element Size id=sample_content_fragment
${element_location}= Get Element Location id=sample_content_fragment
${start_x}= Evaluate ${element_location['x']} + (${element_size['width']} * 0.5)
${start_y}= Evaluate ${element_location['y']} + (${element_size['height']} * 0.3)
${end_x}= Evaluate ${element_location['x']} + (${element_size['width']} * 0.5)
${end_y}= Evaluate ${element_location['y']} + (${element_size['height']} * 0.7)
Swipe ${start_x} ${start_y} ${end_x} ${end_y} 500
Sleep 1
获取Toast
AppiumLibrary没有获取Toast的keyword,需要自己定义 参考:
Mac电脑上,_element.py 文件的位置位于/usr/local/lib/python3.9/site-packages/AppiumLibrary/keywords/_element.py 位置可能各有区别,可以直接搜索AppiumLibrary
# coding=utf-8
# Author: Allan
# 导入三个库文件
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
# 获取 Toast 信息
def find_toast(self, text):
'''Find Android Toast
Arguments:
| text | Toast信息 |
Examples:
| Find Toast | ${text} |
'''
application = self._current_application()
try:
toast_locator = ("xpath", ".//*[contains(@text,'%s')]" %text)
WebDriverWait(application, 6, 0.5).until(expected_conditions.presence_of_element_located(toast_locator))
self._info("Toast has been found: %s." %text)
except:
raise AssertionError("Not found toast: %s." %text)
可以稍微修改下
# Public, element lookups
def find_toast(self, text=None, timeout=10, poll_frequency=0.01):
"""Find Android Toast
:param text: toast text
:param timeout: Number of seconds before timing out, By default, it is 10 second.
:param poll_frequency: sleep interval between calls, By default, it is 0.01 second.
:return: toast
"""
application = self._current_application()
if text:
toast_loc = ("//*[contains(@text, '%s')]" % text)
else:
toast_loc = "//*[@class='android.widget.Toast']"
try:
WebDriverWait(application, timeout, poll_frequency).until(
expected_conditions.presence_of_element_located(('xpath', toast_loc)))
toast_elm = application.find_element_by_xpath(toast_loc)
return toast_elm.text
except:
# raise AssertionError("Not found toast: %s." %text)
return 'NONE'
另一种方式,作为参考,我自己没有试验过:
|