The id, name, class, relative location before the eight elements commonly used in Appium

Keywords: Android Mobile Selenium Attribute

In fact, the way of locating elements in APP automated testing is basically the same as that in Web automated testing. Whether it is APP or Web automated testing, the most important step is to locate elements. Only when elements are located accurately, can relevant elements be operated. Appium also provides a way to locate elements. Multivariate positioning:

The Way of Element Location
id name class List Location
Relative positioning Xpath location H5 page element location UIAutomator Location

Catalog

1. id location

Test scenarios

1.1 id positioning integrated practice

Test scenarios

requirement analysis

2. name and classname positioning:

2.1 name location:

2.2 className Location:

3. Relative positioning

Test cases:

1. id location

ID is familiar to everyone, just like our ID number, it is unique. In the element of APP interface, we can also use the ID value of the element to distinguish different elements, and then locate the element. In Appium, we can use the find_element_by_id() method to find the element. Location.

  • Test scenarios

  1. Install wandoujia.apk
  2. Start pea pod app
  3. Click the skip button to enter the main interface

wjd_test.py

from appium import webdriver
import time
 
xg_caps = {}
xg_caps['platformName'] = 'Android'
xg_caps['deviceName'] = '127.0.0.1:62001'
xg_caps['platformVersion'] = '4.4.2'
 
xg_caps['app'] = '/app Packet Storage Path/wandoujia.apk'
xg_caps['appPackage'] = 'com.wandoujia.phoenix2'
xg_caps['appActivity'] = 'com.pp.assistant.activity.PPMainActivity'
 
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', xg_caps)
driver.implicitly_wait(5)

jump = driver.find_element_by_id('com.wandoujia.phoenix2:id/aio')
jump.click()

Problem: Sometimes in the process of startup will encounter the situation of updated version, the way to deal with it is as follows:

update_test.py

from appium import webdriver
from selenium.common.exceptions import NoSuchElementException

xg_caps = {}
xg_caps['platformName'] = 'Android'
xg_caps['platformVersion'] = '4.4.2'
xg_caps['deviceName'] = '127.0.0.1:62001'

xg_caps['appPackage'] = 'com.wandoujia.phoenix2'
xg_caps['appActivity'] = 'com.pp.assistant.activity.PPMainActivity'

driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', xg_caps)
driver.implicitly_wait(5)

def check_jumpBtn():
    print('check_jumpBtn')
    try:
        jumpBtn = driver.find_element_by_id('com.wandoujia.phoenix2:id/aio')
    except NoSuchElementException:
        print('no jumpBtn')
    else:
        jumpBtn.click()
        print('Click and skip')

def check_cancelBtn():
    print('check_cancelBtn')
    try:
        cancelBtn = driver.find_element_by_id('com.wandoujia.phoenix2:id/s5')
    except NoSuchElementException:
        print('no cancelBtn')
    else:
        cancelBtn.click()

check_jumpBtn()
check_cancelBtn()

Execute the update script sample diagram:

1.1 id positioning integrated practice

Extractive reuse code: capability.py

// Reuse code
from appium import webdriver
from selenium.common.exceptions import NoSuchElementException

xg_caps = {}
xg_caps['platformName'] = 'Android'
xg_caps['platformVersion'] = '4.4.2'
xg_caps['deviceName'] = '127.0.0.1:62001'

xg_caps['appPackage'] = 'com.wandoujia.phoenix2'
xg_caps['appActivity'] = 'com.pp.assistant.activity.PPMainActivity'

xg_caps['noReset'] = True
// When sending_keys enters Chinese, you need to configure the following two items so that the input method guardian of Appium can perform the corresponding input operations.
xg_caps['unicodeKeyboard'] = True
xg_caps['resetKeyboard'] = True

driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', xg_caps)
driver.implicitly_wait(5)

def check_cancelBtn():
    print('check_cancelBtn')
    try:
        cancelBtn = driver.find_element_by_id('com.wandoujia.phoenix2:id/s5')
    except NoSuchElementException:
        print('no cancelBtn')
    else:
        cancelBtn.click()
        print('Click Cancel Update')

check_cancelBtn()
  • Test scenarios

  1. Start pea pod app
  2. Enter the login interface, enter the username and password, and then click login
  • requirement analysis

  1. The code that starts app to detect and upgrade the bullet window can be separated as a separate module to improve the code reuse rate.
  2. Click on the main menu button in the upper left corner to get the id of the main menu and enter the management interface.
  3. Click on the settings button in the upper right corner to get the id of the settings and enter the settings interface.
  4. Click on the login button in the settings interface to get the id of the login button and enter the mobile phone registration interface.
  5. Click the Immediate Logon button to get the id of the Immediate Logon button and enter the login interface.
  6. Get the id of the account, password entry box and login button, and the id of the consent check box
  7. Enter your account and password, select the Consent check box, and click the login button.

Reference code:

from capability import driver

def login():
    // Click on the main menu navigation button in the upper left corner
    driver.find_element_by_id('com.wandoujia.phoenix2:id/w4').click()
    driver.implicitly_wait(2)

    // Click the Setup button in the upper right corner
    driver.find_element_by_id('com.wandoujia.phoenix2:id/pp_item_setting').click()
    driver.implicitly_wait(2)

    // Click the login button to enter the login interface
    driver.find_element_by_id('com.wandoujia.phoenix2:id/ow').click()
    driver.implicitly_wait(2)

    // Clear the username and enter the username and password
    driver.find_element_by_id('com.wandoujia.phoenix2:id/l_').clear()
   driver.find_element_by_id('com.wandoujia.phoenix2:id/l_').send_keys('mailbox')
    driver.find_element_by_id('com.wandoujia.phoenix2:id/la').send_keys('Password')

    // Click the Consent check box
    driver.find_element_by_id('com.wandoujia.phoenix2:id/mw').click()
    driver.implicitly_wait(2)
    // Click the login button
    driver.find_element_by_id('com.wandoujia.phoenix2:id/mf').click()

login()

Problems encountered: When getting the navigation button of the main menu, the id of the parent control of the button is obtained by using the recording function of appium. The id of the parent control of the button has not been found for a long time, and then the id of the navigation button of the main menu is obtained by using UI Automator viewer.

Tip: When we use Appium's input operation, we may encounter the problem that our own input method can not arouse. We can set up the system - > language and input method - > current input method (replace the current input method with system input method or other input method).

2. name and classname positioning:

2.1 name location:

For Android, positioning by name is just a property of text.

Usage method:

from capability import *

driver.find_element_by_name('Cell-phone number/email').send_keys('mailbox')
driver.find_element_by_name('Sign in').click()

Tip: Because text's attributes are not very stable, this method was abandoned at Appium 1.5. (You can compare the contents of the red box in the picture below.)

2.2 className Location:

Using className to locate is actually based on the type of element, but in an APP there are many elements with the same classname.

Note: Contrasting the content in the green box in the picture above, we can find that the mobile phone number / email and the classname of the password box are both "android.widget.EditText", because if we use className to locate, we can only locate the mobile phone number / email.

Usage method:

from capability import *

driver.find_element_by_class_name('android.widget.EditText').send_keys('mailbox')
driver.find_element_by_class_name('android.widget.EditText').send_keys('Password')
driver.find_element_by_class_name('android.widget.TextView').click()

In fact, there is also a special way for thieves to locate the phone number/email input box with name and the password box with className. Although this can be achieved, it is also a cockroach, but if you use id to locate, you don't need to consider so much, so generally I use id to locate droplets.

3. Relative positioning

Relative positioning means to find the node of the parent element of the corresponding attribute of the element, and then locate the element based on the parent element.

Test cases:

Without using id element location, click the avatar button on the modification profile page.

relative.py

from capability import *

// The premise of using relative positioning is that there is only one ImageView element in the parent element node.
super_celement = driver.find_element_by_id('com.wandoujia.phoenix2:id/lk')
super_celement.find_element_by_class_name('android.widget.ImageView').click()

 

 

 

Posted by kubis on Sat, 10 Aug 2019 06:23:23 -0700