Foreplay
appium targeting element has: id, name, class, relative positioning, the Xpath positioning, List positioning, UIAutomator positioning
send_keys () needs to be configured in the following capability when the incoming Chinese
desired_caps['unicodeKeyboard']='True'
desired_caps['resetKeyboard']='True'
If you do not add these two lines, when he can not have Chinese input
Sometimes the addition of these two lines, garbled input Chinese, the reason is because the input method
After using appium do input operation, if the input method appears not to arouse, can be replaced in the system setup --- --- language and input current input to the system input method or other input methods
id Positioning
id positioning is the most commonly used positioning method to locate by find_element_by_id (), open Uiautomator, the value of resource-id is the id
Code combat
import pytest from appium import webdriver class Testcsca(): def setup(self): caps = {} caps["platformName"] = "Android" # caps["deviceName"] = "127.0.0.1:62001" caps["deviceName"] = "CLB0219314000452" caps["appPackage"] = "com.jgw.csca" caps["appActivity"] = " Com.jgw.csca.view.activity.LoginActivity " Caps [ " platfromVersion " ] = " 9.0.0 " Caps [ " autoGrantPermissions " ] = True # Set automatic licensing rights Caps [ ' unicodeKeyboard ' ] = True # input when the Chinese want to add, or else can not enter the Chinese Caps [ ' resetKeyboard ' ] = True # to add input Chinese, or else can not enter Chinese self.driver = webdriver.Remote ( " HTTP: // localhost: 4723 / WD / Hub " ,caps) self.driver.implicitly_wait(20) def test_login(self): self.driver.find_element_by_id('com.jgw.csca:id/et_name').send_keys('zouzou') self.driver.find_element_by_id('com.jgw.csca:id/et_pwd').send_keys('666')
Positioning name
Positioning name
The name is positioned, for android, the text property is
driver.find_element_by_name ( 'login') .click ()
self.driver.find_element_by_name ( ' login ' ) .click () # this will complain
Since text is not very good stability, so the method appium1.5 started discarded, although this method is not available, but we can be positioned by xpath
self.driver.find_element_by_xpath ( " // * [the contains (@Text, 'login')] " ) .click () # This will normally click on
class positioning
classname position is positioned according to the type of element, but in reality classname many elements are the same, only a plurality of first target input box
import pytest from appium import webdriver class Testcsca(): def setup(self): caps = {} caps["platformName"] = "Android" # caps["deviceName"] = "127.0.0.1:62001" caps["deviceName"] = "CLB0219314000452" caps["appPackage"] = "com.jgw.csca" caps["appActivity"] = " Com.jgw.csca.view.activity.LoginActivity " Caps [ " platfromVersion " ] = " 9.0.0 " Caps [ " autoGrantPermissions " ] = True # Set automatic licensing rights Caps [ ' unicodeKeyboard ' ] = True # input when the Chinese want to add, or else can not enter the Chinese Caps [ ' resetKeyboard ' ] = True # to add input Chinese, or else can not enter Chinese self.driver = webdriver.Remote ( " HTTP: // localhost: 4723 / WD / Hub " ,caps) self.driver.implicitly_wait ( 20 is ) DEF test_login (Self): # positioned such that the input box self.driver.find_element_by_class_name ( ' android.widget.EditText ' ) .send_keys ( ' 111 ' ) # find_elements positioning a plurality of elements, [1] obtaining a second element, from 0 self.driver.find_elements_by_class_name ( ' android.widget.EditText ' ) [1] .send_keys ( ' 222 ' )
Relative positioning
Relative positioning of the elements is to find the parent attribute has a corresponding element node, then the parent element based on the positioning element
Xpath positioning
A path is positioned xpath targeting, mainly dependent on the absolute or element to locate the relevant attributes, but the absolute path xpath efficiency is relatively low, less commonly used, is generally used xpath properties relative path and positioning
http://www.w3school.com.cn/xpath/xpath_syntax.asp
xpath path expression
contains (@ resource-id, 'user') on the part of the line
/ | Choose from the root node |
// | Select the current node in the document matches the selected node from, regardless of their location |
nodename | Select all the child nodes of this node |
. | Select the current node |
.. | Select the parent of the current node |
@ | Select Properties |
* | Matches any element node |
@* | Matches any attribute node |
node() | Matches any type of node |
import pytest from appium import webdriver class Testcsca(): def setup(self): caps = {} caps["platformName"] = "Android" # caps["deviceName"] = "127.0.0.1:62001" caps["deviceName"] = "CLB0219314000452" caps["appPackage"] = "com.jgw.csca" caps["appActivity"] = " Com.jgw.csca.view.activity.LoginActivity " Caps [ " platfromVersion " ] = " 9.0.0 " Caps [ " autoGrantPermissions " ] = True # Set automatic licensing rights Caps [ ' unicodeKeyboard ' ] = True # input when the Chinese want to add, or else can not enter the Chinese Caps [ ' resetKeyboard ' ] = True # to add input Chinese, or else can not enter Chinese self.driver = webdriver.Remote ( " HTTP: // localhost: 4723 / WD / Hub " ,caps) self.driver.implicitly_wait ( 20 is ) DEF test_login (Self): # no contains, between the attribute and the value = (self.driver.find_element_by_xpath ' // * [@ text = "Please enter the username'] ' ). send_keys ( ' balabala ' ) self.driver.find_element_by_xpath ( ' //*[@resource-id="com.jgw.csca:id/et_pwd "] ' ) .send_keys ( ' 123456 ' ) # when contains, attributes and a value between, the intermediate may be used and self.driver.find_element_by_xpath ( ' // * [the contains (@Text, "login") and the contains (@index, ". 5")] ').click()
List positioning
List first positioning element is used find_elements_by_XX obtain a same attribute group, and then use the array subscript numerals to distinguish between different elements related operations. Subscript starts at 0
self.driver.find_element_by_id('com.tal.csca:id/login_register_text').click() self.driver.find_element_by_id('com.tal.csca:id/activity_register_userheader').click() image = self.driver.find_elements_by_id('com.tal.csca:id/item_image') image[7].click() self.driver.find_element_by_id("com.tal.kaoyan:id/save").click()
Click picture here is a picture id, so first navigate to the id, stored in a list inside, and then determined according to the index
Uiautomator positioning
Uiautomator positioning element Android system supported by native targeting methods, XPath although similar, but with better than it, and the positioning element supports all the attributes. Positioning works by Android comes with Android Uiautomator library to find elements. Appium element positioning method is actually performed based on the package Uiautomator
Commonly used targeting are:
id: the corresponding property resource_id Android
text: text corresponding to Andrews
classname: class Android attributes
import pytest from appium import webdriver class Testcsca(): def setup(self): caps = {} caps["platformName"] = "Android" # caps["deviceName"] = "127.0.0.1:62001" caps["deviceName"] = "CLB0219314000452" caps["appPackage"] = "com.jgw.csca" caps["appActivity"] = " Com.jgw.csca.view.activity.LoginActivity " Caps [ " platfromVersion " ] = " 9.0.0 " Caps [ " autoGrantPermissions " ] = True # Set automatic licensing rights Caps [ ' unicodeKeyboard ' ] = True # input when the Chinese want to add, or else can not enter the Chinese Caps [ ' resetKeyboard ' ] = True # to add input Chinese, or else can not enter Chinese self.driver = webdriver.Remote ( " HTTP: // localhost: 4723 / WD / Hub " ,caps) self.driver.implicitly_wait ( 20 is ) DEF test_login (Self): # The text positioning self.driver.find_element_by_android_uiautomator ( ' . UiSelector new new () text ( "Please enter a user name") ' ) .send_keys ( ' 666 ' ) # The id positioning self.driver.find_element_by_android_uiautomator ( ' new new UiSelector () The resourceId (. "com.jgw.csca: id / et_pwd") ' ) .send_keys ( ' balabala ' ) # The class positioning self.driver.find_element_by_android_uiautomator ( 'new UiSelector().className("android.widget.Button")').click()