Input text and click Button
The input Text keyword is typically used to enter an input box that receives two parameters [locator | Text].
Example 1: Launch the mainactivity of an app on an Android phone, enter 12 in two EditText input boxes after opening activity, enter the interface, and click the button "calculate" to calculate the product of the two numbers entered.
The app's interface is as follows, with two input boxes and a calculated button button.
Before writing this automation case, we can use the UI Automator Viewer tool provided by the Android SDK to locate the resource on this interface. After positioning, you can see that the first EditText input box Resource-id is Com.example.calculator:id/factorone, Class for Android.widget.edittext,name Enter a number as shown in.
The second EditText input box has a resource-id of com.example.calculator:id/factortwo,class for the android.widget.edittext,name, enter the number as shown in.
The button Resource-id is calculated as Com.example.calculator:id/commit,class for Android.widget.button,name, as shown in.
The click button keyword is used to simulate clicking a button on the app, which receives a parameter [Index_or_name].
Open application http://localhost:4723/wd/hub platformname=android platformversion=22 devicename=98yfbp 522VSU 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 calculation
Execution Result:
You can see that the success has been performed by locating the EditText input box by Resource-id and locating the button by name.
Here's another way to locate the EditText input box by name, and click the button by index
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= Please enter the number 12
Input Text name= Please enter the number 14
Click Button index=0
Execution Result:
In the way to click the button through the index, you need to pay attention to the index value, do not and through the UI Automator Viewer tool see the index confusion, first look at a appiumlibrary library source code, here selected the source of the three functions. From the following three functions you can see the Click_button keyword supports name and index two ways to locate a button, when using index, is based on Class_name, That is, by Android.widget.Button this class_name to find a few button buttons in the current interface (source through elements = self. _find_elements_by_class_ Name (class_name) to look for a few button buttons, it will return several element), and then each button buttons are removed by index (via element = Elements[index in the source code) To get a specific button).
Appiumlibrary Library function 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 Library function 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 Library function 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
In the following interface, two button buttons are placed, a button is a calculation button, and a button is a Cancel button.
At execution time, all button buttons are obtained via elements = Self._find_elements_by_class_name (class_name) and then print ' Elements: '%s '% Elements can print out the obtained elements, that is, all the button, from the following output can be seen, elements will be stored in a list, the list, a total of two elements, representing the Fetch two button buttons.
Print out the results:
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 ") ] "
When index is 0 o'clock, it takes the first button, the button "calculate", and when index is 1 o'clock, it takes the second button, the button "Cancel". When index more than 1, then will be error, at this time the source code through raise ' cannot find the element with index "%s" ' % index_or_name to throw an exception, tell the user, You cannot get to element with the current index (that is, you cannot get any button buttons at this time).
Example 2: Positioning an element with XPath, still using the app interface above as an example.
Use XPath to locate the first EditText input box and the second EditText input box, as shown in the following example:
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 Calculation
Execution Result:
Starting test:RobotFrameworkTest1.TestSuite5.TestCase005
20170510 13:45:07.381:info:typing text ' 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 ' 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: ' Calculation '.
20170510 13:45:21.385:info:clicking element ' calculation '.
Ending test:RobotFrameworkTest1.TestSuite5.TestCase005
From the above results, the first input box was positioned through xpath=//android.widget.edittext[1], and the second input box was positioned through xpath=//android.widget.edittext[2].
Example 3: Positioning elements by accessibility_id, accessibility_id corresponding to the Android app, its corresponding property is Content-desc, here is still using the above app interface as an example, But we added the Content-desc property to the first EditText input box, as shown in.
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= Entry box 23
Input Text Id=com.example.calculator:id/factortwo 12
Click Button Calculation
Execution Result:
Starting test:RobotFrameworkTest1.TestSuite5.TestCase006
20170510 14:23:09.735:info:typing text ' into text field ' accessibility_id= input box '
20170510 14:23:09.735:info:msg:find accessibility_id= Input Box
20170510 14:23:16.573:info:typing text ' 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: ' Calculation '.
20170510 14:23:22.901:info:clicking element ' calculation '.
Ending test:RobotFrameworkTest1.TestSuite5.TestCase006
From the execution results, you can also navigate to the EditText input box via the accessibility_id= input box.
Robotframework Automated test framework-Mobile phone automation test input text and click button keyword usage