Input Text 关键字通常用来给输入框进行输入操做,该关键字接收两个参数[ locator | text ]。html
示例1:启动安卓手机上一个APP的MainActivity,在打开Activity,进入界面后,分别向两个EditText输入框中输入12,而且点击按钮“计算”来计算出输入的这两个数字的乘积。android
APP的界面以下,提供了两个输入框,还有一个计算的Button按钮。ios
在写这个自动化案例前,咱们可使用安卓SDK提供的Ui Automator Viewer工具来进行这个界面的资源定位。经过定位后,能够看到第一个EditText输入框的resource-id为com.example.calculator:id/factorone,class为android.widget.EditText,name为请输入数字,以下图所示。web
第二个EditText输入框的resource-id为com.example.calculator:id/factortwo,class为android.widget.EditText,name为请输入数字,以下图所示。数据库
Button按钮的resource-id为com.example.calculator:id/commit,class为android.widget.Button,name为计算,以下图所示。session
Click Button关键字用来模拟点击APP上的一个button按钮,该关键字接收一个参数[ index_or_name ]。app
Open Application http://localhost:4723/wd/hub platformName=Android platformVersion=22 deviceName=98YFBP522VSU app=C:/Users/yongqing/Desktop/app-debug.apk appPackage=com.example.calculator appActivity=MainActivity框架
Input Text id=com.example.calculator:id/factorone 12 函数
Input Text id=com.example.calculator:id/factortwo 12 工具
Click Button 计算
执行结果:
能够看到已经执行成功,上面是经过resource-id的方式来定位EditText输入框,而且经过name的方式来定位button按钮。
下面经过另外一种方式,用name的方式来定位EditText输入框,经过index的方式来点击button按钮
Open Application http://localhost:4723/wd/hub platformName=Android platformVersion=22 deviceName=98YFBP522VSU app=C:/Users/yongqing/Desktop/app-debug.apk appPackage=com.example.calculator
Input Text name=请输入数字 12
Input Text name=请输入数字 14
Click Button index=0
执行结果:
在经过index的方式来点击button按钮的时候,须要注意index的取值,不要和经过Ui Automator Viewer工具看到的index混淆,先看一段AppiumLibrary库的源码,在这里选取了源码中的三个函数。从以下三个函数中能够看到click_button关键字支持name和index两种方式来定位一个button,在使用index的时候,是根据class_name,即经过android.widget.Button这个class_name来找出当前界面中有几个button按钮(源码中经过elements = self._find_elements_by_class_name(class_name)来寻找有几个button按钮,就会返回几个element),而后每一个 button按钮按照index的方式来取出(源码中经过element = elements[index]来获得具体的一个button按钮)。
AppiumLibrary库函数1:
def click_button(self, index_or_name):
""" Click button """
_platform_class_dict = {'ios': 'UIAButton',
'android': 'android.widget.Button'}
if self._is_support_platform(_platform_class_dict):
class_name = self._get_class(_platform_class_dict)
self._click_element_by_class_name(class_name, index_or_name)
AppiumLibrary库函数2:
def _click_element_by_class_name(self, class_name, index_or_name):
element = self._find_element_by_class_name(class_name, index_or_name)
self._info("Clicking element '%s'." % element.text)
try:
element.click()
except Exception as e:
raise 'Cannot click the %s element "%s"' % (class_name, index_or_name)
AppiumLibrary库函数3:
def _find_element_by_class_name(self, class_name, index_or_name):
elements = self._find_elements_by_class_name(class_name)
print 'elements:"%s"' % elements
if self._is_index(index_or_name):
try:
index = int(index_or_name.split('=')[-1])
print 'index:"%s"' % index
element = elements[index]
print 'element:' , element
except (IndexError, TypeError):
raise 'Cannot find the element with index "%s"' % index_or_name
else:
found = False
for element in elements:
self._info("'%s'." % element.text)
if element.text == index_or_name:
found = True
break
if not found:
raise 'Cannot find the element with name "%s"' % index_or_name
下面这个界面中,放入了两个button按钮,一个button按钮是计算按钮,一个button按钮是取消按钮。
在执行时,经过elements = self._find_elements_by_class_name(class_name)获得全部的button按钮后,再用print 'elements:"%s"' % elements能够打印出获取到的elements,也就是全部的button,从以下的输出结果能够看到,elements会存放在一个list列表中,该list 列表中,共有两个元素,表明取到了两个button按钮。
打印输出结果:
elements:"[<appium.webdriver.webelement.WebElement (session="8e85c12f-2243-4b82-abfc-d091fddbed8b", element="4")>, <appium.webdriver.webelement.WebElement (session="8e85c12f-2243-4b82-abfc-d091fddbed8b", element="5")>]"
当index 为0时,会取到第一个按钮,也就是“计算”这个button按钮,当index为1时,会取到第二个按钮,也就是“取消”这个button按钮。当index超过1后,那么就会报错啦,此时源码中会经过raise 'Cannot find the element with index "%s"' % index_or_name来抛出一个异常,告诉使用者,不能经过当前的index获取到element(也就是此时没法获取到任何button按钮啦)。
示例2:经过xpath的方式定位元素,这里依旧用上面的APP界面为示例。
用xpath的方式定位第一个EditText输入框和第二个EditText输入框,示例以下:
Open Application http://localhost:4723/wd/hub platformName=Android platformVersion=22 deviceName=98YFBP522VSU app=C:/Users/yongqing/Desktop/app-debug.apk appPackage=com.example.calculator appActivity=MainActivity
Input Text xpath=//android.widget.EditText[1] 12
Input Text xpath=//android.widget.EditText[2] 14
Click Button 计算
执行结果:
Starting test: RobotFrameworkTest1.TestSuite5.TestCase005
20170510 13:45:07.381 : INFO : Typing text '12' into text field 'xpath=//android.widget.EditText[1]'
20170510 13:45:07.381 : INFO : msg:find xpath=//android.widget.EditText[1]
20170510 13:45:07.381 : INFO : prefix: xpath
20170510 13:45:07.397 : INFO : criteria: //android.widget.EditText[1]
20170510 13:45:10.462 : INFO : elements: [<appium.webdriver.webelement.WebElement (session="ec48b38a-9cbe-457d-94a0-dec662d3f9cb", element="1")>]
20170510 13:45:15.313 : INFO : Typing text '14' into text field 'xpath=//android.widget.EditText[2]'
20170510 13:45:15.313 : INFO : msg:find xpath=//android.widget.EditText[2]
20170510 13:45:15.313 : INFO : prefix: xpath
20170510 13:45:15.313 : INFO : criteria: //android.widget.EditText[2]
20170510 13:45:15.906 : INFO : elements: [<appium.webdriver.webelement.WebElement (session="ec48b38a-9cbe-457d-94a0-dec662d3f9cb", element="2")>]
20170510 13:45:21.307 : INFO : '计算'.
20170510 13:45:21.385 : INFO : Clicking element '计算'.
Ending test: RobotFrameworkTest1.TestSuite5.TestCase005
从上面的执行结果看,经过xpath=//android.widget.EditText[1] 定位到了第一个输入框,经过xpath=//android.widget.EditText[2] 定位到了第二个输入框。
示例3:经过accessibility_id的方式定位元素,accessibility_id对应到安卓APP后,其对应的属性为content-desc,这里依旧用上面的APP界面为示例,可是咱们对第一个EditText输入框加入了content-desc属性,以下图所示。
Open Application http://localhost:4723/wd/hub platformName=Android platformVersion=22 deviceName=98YFBP522VSU app=C:/Users/yongqing/Desktop/app-debug.apk appPackage=com.example.calculator appActivity=MainActivity
Input Text accessibility_id=输入框 23
Input Text id=com.example.calculator:id/factortwo 12
Click Button 计算
执行结果:
Starting test: RobotFrameworkTest1.TestSuite5.TestCase006
20170510 14:23:09.735 : INFO : Typing text '23' into text field 'accessibility_id=输入框'
20170510 14:23:09.735 : INFO : msg:find accessibility_id=输入框
20170510 14:23:16.573 : INFO : Typing text '12' into text field 'id=com.example.calculator:id/factortwo'
20170510 14:23:16.573 : INFO : msg:find id=com.example.calculator:id/factortwo
20170510 14:23:22.799 : INFO : '计算'.
20170510 14:23:22.901 : INFO : Clicking element '计算'.
Ending test: RobotFrameworkTest1.TestSuite5.TestCase006
从执行结果看,经过 accessibility_id=输入框 也能够定位到EditText输入框。
【原文归做者全部,欢迎转载,可是保留版权】
Robot Framework自动化测试框架核心指南电子版试读
相关博文汇总:
RobotFramework下的http接口自动化Create Http Context关键字的使用
RobotFramework下的http接口自动化Get关键字的使用
RobotFramework下的http接口自动化post关键字的使用
RobotFramework下的http接口自动化Get Response Body关键字的使用
RobotFramework下的http接口自动化Get Response Status 关键字的使用
RobotFramework下的http接口自动化Get Response header 关键字的使用
RobotFramework下的http接口自动化Set Request Header 关键字的使用
RobotFramework下HttpLibrary库其它关键字
RobotFramework下的http接口自动化Set Request Body 关键字的使用
RobotFramework下的http接口自动化Follow Response关键字的使用
RobotFramework自动化测试框架的基础关键字(一)
RobotFramework自动化测试框架的基础关键字(二)
RobotFramework自动化测试框架的基础关键字(三)
RobotFramework自动化测试框架的基础关键字(四)
RobotFramework自动化测试框架的基础关键字(五)
RobotFramework自动化测试框架-移动手机自动化测试AppiumLibrary介绍
RobotFramework自动化测试框架-移动手机自动化测试Open Application关键字的使用
RobotFramework自动化测试框架-经常使用断言关键字
RobotFramework自动化测试框架-移动手机自动化测试AppiumLibrary库其它的常见自动化关键字
RobotFramework自动化测试框架-移动手机自动化测试Input Text和Click Button关键字的使用
RobotFramework自动化测试框架-移动手机自动化测试Clear Text关键字的使用
RobotFramework自动化测试框架-移动手机自动化测试Click Element关键字的使用
RobotFramework自动化测试框架-移动手机自动化测试Click A Point关键字的使用
RobotFramework自动化测试框架-移动手机自动化测试Click Element At Coordinates关键字的使用
RobotFramework自动化测试框架-移动手机自动化测试Get Element Location关键字的使用
RobotFramework自动化测试框架-移动手机自动化测试Get Network Connection Status和Set Network Connection Status关键字的使用
RobotFramework自动化测试框架-移动手机自动化测试Element Attribute Should Match关键字的使用
RobotFramework自动化测试框架-DatabaseLibrary库的使用(对数据库的操做)
RobotFramework自动化测试框架-使用Python编写自定义的RobotFramework Lib
RobotFramework自动化测试框架-Selenium Web自动化(-)-Open Browser和Close Browser