This is the 15th day of my participation in the Challenge. For details, see:More article challenges

3. Configure the environment (baidu installation – basic operation)

There is already JDK,Android Studio, Python environment, and Pythoncharm by default

Appium_Python_Client (calls client libraries to communicate with Appium Server)

pip install Appium-Python-Client

Test whether the connection is successful

adb devices -l

Check the app’s package name and appActivity

The adb shell dumpsys activity recents | find "intent = {"

Screen capture

1.adb shell screencap /sdcard/screen.png

2. Adb pull /sdcard/screen.png C:\Users\Administrator\Desktop\

4. Introduction to Appium demo

Steps:

1. Open the simulator

2. Open the Appium

3. Find the need to test the package name and the interface name (adb shell dumpsys Windows Windows | findstr searches mFocusedApp)

3. Write code in Python to start the application

Effect: It automatically starts Bilibili

# import webdriver
import time

from appium import webdriver
# Initialize parameters to start the uniform code of the app
desired_caps = {
    'platformName': 'Android'.# Tested phone is Android
    'platformVersion': '7'.# Mobile Android version
    'deviceName': 'xxx'.# Device name, android phone can fill in freely
    'appPackage': 'tv.danmaku.bili'.# Start APP Package name
    'appActivity': '.ui.splash.SplashActivity'.# Name of the Activity to start
    'unicodeKeyboard': True.# Use the built-in input method. Enter True when entering Chinese
    'resetKeyboard': True.Restore the original input method after executing the program
    'noReset': True.Do not reset the App. If it is False, the App data will be cleared after the script is executed. For example, if you logged in originally, you logged out after executing the script
    'newCommandTimeout': 6000.'automationName': 'UiAutomator2'
}
Connect to Appium Server and initialize the automation environment
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
time.sleep(5)
driver.quit()
Copy the code

5. Obtain app element information

5.1 Application of small tools

Two ways to find information about app elements

(1) Use appium (2) Use UIAutomatorViewer (Android SDK built-in element positioning tool)

Method 2:

5.2 Locate the element API

5.2.1 Locate an element

find_element_by_id() find_element_by_class_name() find_element_by_xpath() find_element_by_css_selector()

5.2.2 Locate a set of elements

find_elements_by_id() find_elements_by_class_name() find_elements_by_xpath() find_elements_by_css_selector()

5.2.3 requires waiting

Implicit wait (global element valid)

After the timeout is set, all subsequent methods that locate the element will wait for the element to occur within that time. If you go out and continue, NoSuchElementException will be reported if it does not occur
driver.implicitly_wait(200)
Copy the code

Explicit wait (valid for a single element)

Key class: WebDriverWait

Key method: the until method in the WebDriverWait object

Function: After the display wait is set, you can wait for a timeout period and search within this timeout period. By default, search every 0.5 seconds

Wait for the element to appear within this time, and if you go out to continue, a TimeOutException is reported if it does not

6. Element manipulation API

6.1 click

Note: If you want to enter Chinese, remember to enter the pre-code

'unicodeKeyboard': True, # Use the built-in input method, enter True 'resetKeyboard': True, # Restore the original input method after executing the programCopy the code

` `

element.click()
Copy the code

6.2 Enter text in the input box

Element. Send_keys (" content ")

6.3 Clearing The Input Field

element.clear()

6.4 Obtaining the element text content

element.text
Copy the code

6.5 Obtaining element Size and Position (both dictionary types)

` `

print("Get element position and size")
print(button.size)
print(button.location)
print(button.location["x"])
Copy the code

Test the demo

# import webdriver
from appium import webdriver
# Initialize parameters
desired_caps = {
        'platformName': 'Android'.# Tested phone is Android
    'platformVersion': '7'.# Mobile Android version
    'deviceName': 'xxx'.# Device name, android phone can fill in freely
    'appPackage': 'tv.danmaku.bili'.# Start APP Package name
    'appActivity': '.ui.splash.SplashActivity'.# Name of the Activity to start
    'unicodeKeyboard': True.# Use the built-in input method. Enter True when entering Chinese
    'resetKeyboard': True.Restore the original input method after executing the program
    'noReset': True.Do not reset the App. If it is False, the App data will be cleared after the script is executed. For example, if you logged in originally, you logged out after executing the script
    'newCommandTimeout': 6000.'automationName': 'UiAutomator2'
}
Connect to Appium Server and initialize the automation environment
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
If you do not give time, you may not find the element
driver.implicitly_wait(5)
Click the search box
driver.find_element_by_id("expand_search").click()
# Type "Titanic"
driver.find_element_by_id("search_src_text").send_keys("Software Testing")
# Keyboard enter
driver.keyevent(66)
# Because he just quit the app after searching and can't see the search results page, SO I gave him a way to stop
input('* * * * * * * * * *')




driver.quit()
Copy the code

The screenshots

Check the equipment

Enter the parameters in Appium

Find the element by id

Error solution

When I click on this, I get an error

Error while obtaining the UI hierarchy XML file: com. Android. Ddmlib. SyncException: Remote object doesn ‘t exist! Error while obtaining the UI hierarchy XML file: com. Android. Ddmlib. SyncException: Remote object doesn ‘t exist!

Solution: Turn off/stop the Appium