Element Attribute Should Match 关键字用来判断元素的属性值是否和预期值匹配,该关键字接收四个参数[ locator | attr_name | match_pattern | regexp=False ],当通过locator识别到元素超过1个元素时,会默认选择第一个元素,attr_name参数代表所选元素的属性的名称,match_pattern参数代表预期匹配值。Regexp代表了匹配时是否通过正则表达式来进行匹配。
示例1:使用Element Attribute Should Match 关键字来判断通过locator为id=com.example.calculator:id/factorone和attr_name=name 获取到的值是否能和预期值匹配。在这里依旧采用上面使用的APP来做操作示例。
Open Application :4723/wd/hub platformName=Android platformVersion=22 deviceName=98YFBP522VSU app=C:/Users/yongqing/Desktop/app-debug.apk appPackage=com.example.calculator appActivity=MainActivity
Element Attribute Should Match
id=com.example.calculator:id/factorone name 输入框
执行结果:
Starting test: RobotFrameworkTest1.TestSuite5.TestCase017
20170513 15:48:54.838 : INFO : msg:find id=com.example.calculator:id/factorone
20170513 15:48:54.839 : INFO : prefix: id
20170513 15:48:54.840 : INFO : criteria: com.example.calculator:id/factorone
20170513 15:48:56.622 : INFO : elements: [<appium.webdriver.webelement.WebElement (session="6708d455-65b8-4af7-8d7c-ea86e7f039f3", element="1")>]
20170513 15:48:56.745 : INFO : Element 'id=com.example.calculator:id/factorone' attribute 'name' is '输入框'
Ending test: RobotFrameworkTest1.TestSuite5.TestCase017
从执行结果看,Element 'id=com.example.calculator:id/factorone' attribute 'name' is '输入框',正好和” 输入框”能匹配上。
示例2:改用locator为class=android.widget.Butto和attr_name=name来进行匹配验证,并且看一下,当locator匹配到多个元素时是如何进行处理。
Open Application :4723/wd/hub platformName=Android platformVersion=22 deviceName=98YFBP522VSU app=C:/Users/yongqing/Desktop/app-debug.apk appPackage=com.example.calculator appActivity=MainActivity
Element Attribute Should Match .widget.Button name 计* True
执行结果:
Starting test: RobotFrameworkTest1.TestSuite5.TestCase018
20170520 15:06:29.527 : INFO : msg:find class=android.widget.Button
20170520 15:06:29.527 : INFO : prefix: class
20170520 15:06:29.527 : INFO : criteria: android.widget.Button
20170520 15:06:31.214 : INFO : elements: [<appium.webdriver.webelement.WebElement (session="dce6f097-98d2-4632-8708-598e2f693721", element="1")>, <appium.webdriver.webelement.WebElement (session="dce6f097-98d2-4632-8708-598e2f693721", element="2")>]
20170520 15:06:31.214 : INFO : CAUTION: 'class=android.widget.Button' matched 2 elements - using the first element only
20170520 15:06:31.243 : INFO : Element 'class=android.widget.Button' attribute 'name' is '计*'
Ending test: RobotFrameworkTest1.TestSuite5.TestCase018
从执行结果看:当通过class=android.widget.Button获取到两个元素时,默认只会使用第一个元素,从结果看能和预期结果匹配上。