*** Settings ***
Suite Setup
Library AppiumLibrary #导入appiumLibrary库
*** Variables ***
${num1} 8 #赋值变量num1=8
${num2} 6 #赋值变量num2=6
${expected_add_result} 14 #赋值变量加法期望结果为14
${expected_sub_result} 2 #赋值变量减法期望结果为2
${plus} com.miui.calculator:id/btn_plus_s #赋值‘+’的定位元素
${result} com.miui.calculator:id/btn_equal_s #赋值‘=’的定位元素
${display_result} xpath=//android.widget.TextView[@text='=']/following-sibling::android.widget.TextView #赋值计算结果的定位元素
${subt} com.miui.calculator:id/btn_minus_s #赋值‘-’的定位元素
*** Test Cases ***
加法
[Setup] 打开计算器
输入数字和运算符 ${num1} ${num2} ${plus}
计算结果 ${result}
验证计算结果 ${expected_add_result}
[Teardown] 关闭计算器
减法
[Setup] 打开计算器
输入数字和运算符 ${num1} ${num2} ${subt}
计算结果 ${result}
验证计算结果 ${expected_sub_result}
[Teardown] 关闭计算器
*** Keywords ***
打开计算器 #定义‘打开计算器’的函数
open application http://localhost:4723/wd/hub platformName=Android platformVersion=6.0.1 deviceName=1560ab67 appPackage=com.miui.calculator appActivity=.cal.CalculatorActivity
关闭计算器 #定义‘关闭计算器’的函数
close application
输入数字和运算符 #定义‘输入数字和运算符’的函数
[Arguments] ${test_num1} ${test_num2} ${operator} #定义三个形参用于接收传入参数
click element xpath=//*[contains(@text,'${test_num1}')] #第一个数 选取当前节点下text为${test_num1}的数
click element xpath=//*[contains(@resource-id,'${operator}')] #运算符号
click element xpath=//*[contains(@text,'${test_num2}')] #第二个数
计算结果
[Arguments] ${result}
click element xpath=//*[contains(@resource-id,'${result}')] #定义执行‘=’的函数
验证计算结果
[Arguments] ${expected_result}
${count_result} Get Element Attribute ${display_result} text #将计算结果赋值给${count_result}
Should Be Equal ${count_result} ${expected_result} #计算结果与期望结果比较
执行结果:
Starting test: 移动.小米计算器.加法
20180611 20:38:30.678 : INFO : Clicking element 'xpath=//*[contains(@text,'8')]'.
20180611 20:38:31.580 : INFO : Clicking element 'xpath=//*[contains(@resource-id,'com.miui.calculator:id/btn_plus_s')]'.
20180611 20:38:32.457 : INFO : Clicking element 'xpath=//*[contains(@text,'6')]'.
20180611 20:38:33.334 : INFO : Clicking element 'xpath=//*[contains(@resource-id,'com.miui.calculator:id/btn_equal_s')]'.
20180611 20:38:34.838 : INFO : Element 'xpath=//android.widget.TextView[@text='=']/following-sibling::android.widget.TextView' attribute 'text' value '14'
20180611 20:38:34.840 : INFO : 14
Ending test: 移动.小米计算器.加法
Starting test: 移动.小米计算器.减法
20180611 20:38:52.916 : INFO : Clicking element 'xpath=//*[contains(@text,'8')]'.
20180611 20:38:53.831 : INFO : Clicking element 'xpath=//*[contains(@resource-id,'com.miui.calculator:id/btn_minus_s')]'.
20180611 20:38:54.602 : INFO : Clicking element 'xpath=//*[contains(@text,'6')]'.
20180611 20:38:55.439 : INFO : Clicking element 'xpath=//*[contains(@resource-id,'com.miui.calculator:id/btn_equal_s')]'.
20180611 20:38:57.006 : INFO : Element 'xpath=//android.widget.TextView[@text='=']/following-sibling::android.widget.TextView' attribute 'text' value '2'
20180611 20:38:57.008 : INFO : 2
Ending test: 移动.小米计算器.减法