Appium is a cross-platform mobile test automation tool that makes it easy to create automated test cases for iOS and Android. It can simulate various operations inside the App, such as clicking, sliding, text input, etc., as long as we manually operate the action Appium can complete. Earlier, we looked at Selenium, a web-based testing automation tool. Appium actually inherits Selenium, and Appium also uses WebDriver to automate App testing. For iOS devices, Appium uses UIAutomation to implement drivers. For Android, it uses UiAutomator and Selendroid to implement the driver.

Appium is like a server. We can send some operation instructions to Appium, and Appium will drive the mobile device according to different instructions to complete different actions.

For crawlers, we use Selenium to grab JavaScript rendered pages that are visible and can be crawled. Appium is also a good choice for App crawlers.

Let’s take a look at the basic use of Appium.

I. Objectives of this section

We take wechat of Android platform as an example to demonstrate the method of launching and operating App of Appium. The main purpose is to understand the process of automated testing with Appium and the usage of related APIS.

Two, preparation work

Ensure that Appium, the Android development environment, and the Appium API of Python have been installed on the PC. In addition, Android phone installed wechat App.

3. Start the App

There are two ways to start an App with Appium: one is to use Appium’s built-in drive to open the App, and the other is to use a Python program to do so. We will explain each of them below.

First, open Appium. The startup interface is as shown in the figure below.

Click the Start Server button to Start the Appium service, which is equivalent to starting an Appium Server. We can use Appium’s built-in driver or Python code to send a series of operation instructions to Appium’s server, and Appium will drive the mobile device to perform different actions according to the different instructions. After startup, the running interface is as shown in the following figure.

Appium is listening on port 4723 after running. We can send the operation command to the service interface corresponding to this port, and this page will display the operation log of this process.

Connect the Android phone to the PC running Appium through the data cable, and turn on the USB debugging function to ensure that the PC can connect to the phone.

You can test the connection by entering the adb command, as shown below:

adb devices -lCopy the code

If a result similar to the following occurs, it indicates that the PC is properly connected to the phone.

List of devices attached
2da42ac0 device usb:336592896X product:leo model:MI_NOTE_Pro device:leoCopy the code

Model is the name of the device, which is the deviceName variable you’ll use later in this article. I am using the Mi Note top edition, so the name here is MI_NOTE_Pro.

If the adb command cannot be found, check whether the Android development environment and environment variables have been configured successfully. If you can successfully invoke the ADB command without displaying device information, check the connection between your phone and PC.

Next, open the App with the built-in drive of Appium and click the “Start New Session” button in Appium, as shown in the figure below.

A configuration page appears, as shown in the figure below.

You need to set the Desired Capabilities parameters when starting the App. They are platformName, deviceName, appPackage, and appActivity.

  • PlatformName: It is the name of the platform. You need to distinguish between Android and iOS.

  • DeviceName: This is the name of the device, in this case the specific type of the phone.

  • AppPackage: This is the name of the App package.

  • AppActivity: This is the name of the entry Activity. At the beginning.

In the lower left corner of the current configuration page also has a configuration parameter relevant specification, the link is https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/caps.md.

We added the above four configurations to Appium, as shown in the figure below.

Click the Save button, save this, we can continue to use this configuration in the future.

Click the Start Session button in the lower right corner to Start the wechat App on Android phone and enter the launch page. At the same time, a debugging window will pop up on the PC, from which we can preview the current mobile phone page and view the source code of the page, as shown in the figure below.

Clicking on an element of the screen in the left bar, such as the login button, will highlight it. At this time, the middle bar displays the source code corresponding to the currently selected button, and the right bar displays the basic information of the element, such as the id, class, text, etc. of the element, as well as the operations that can be performed, such as Tap, Send Keys, Clear, as shown in the figure below.

Click the third record button at the top of the middle bar, Appium will start to record the operation action, at this time we operate the App in the window will be recorded, the Recorder can automatically generate the code of the corresponding language. For example, we click the record button, select the login button in the App, and click Tap, which simulates the button clicking function. At this time, the phone and the App in the window will jump to the login page, and the code corresponding to this action will be displayed in the middle bar, as shown in the figure below.

Next, select the phone number text box on the left, click Send Keys, and the dialog box will pop up. Enter the mobile phone number and click Send Keys to complete the text input, as shown in the picture below.

We can click different action buttons on this page, you can realize the control of the App, and the Recorder part can also generate the corresponding Python code.

Let’s take a look at how to drive an App using Python code. Appium Server: Appium Server: Appium Server: Appium Server: Appium Server: Appium Server: Appium Server: Appium Server: Appium Server

server = 'http://localhost:4723/wd/hub'Copy the code

Use the dictionary to configure the Desired Capabilities parameter as follows:

desired_caps = {
    'platformName': 'Android'.'deviceName': 'MI_NOTE_Pro'.'appPackage': 'com.tencent.mm'.'appActivity': '.ui.LauncherUI'
}Copy the code

Create a new Session. This is similar to clicking the Start Session button in the Appium built-in driver. The code implementation is as follows:

from appium import webdriver
from selenium.webdriver.support.ui import WebDriverWait

driver = webdriver.Remote(server, desired_caps)Copy the code

After the configuration is complete, you can start the wechat App. But now I can just launch the App, I haven’t done anything yet.

Then use the code to simulate the two actions just demonstrated: one is to click the “login” button, the other is to enter the phone number.

Looking at the Python code just generated by the Recorder recording in the Appium built-in drive, the automatically generated code is very cumbersome, such as clicking on the “login” button code like this:

el1 = driver.find_element_by_xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.View/android. widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.R elativeLayout/android.widget.Button[1]")
el1.click()Copy the code

The XPath selector path in this code is too long, the selection method is not very scientific, and there is no wait when retrieving the element, which is likely to have a timeout exception. So let’s change it to find the element by ID, set the delay wait, and rewrite the code for the two operations as follows:

wait = WebDriverWait(driver, 30)
login = wait.until(EC.presence_of_element_located((By.ID, 'com.tencent.mm:id/cjk')))
login.click()
phone = wait.until(EC.presence_of_element_located((By.ID, 'com.tencent.mm:id/h2')))
phone.set_text('18888888888')Copy the code

To sum up, the complete code looks like this:

from appium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

server = 'http://localhost:4723/wd/hub'
desired_caps = {
    'platformName': 'Android'.'deviceName': 'MI_NOTE_Pro'.'appPackage': 'com.tencent.mm'.'appActivity': '.ui.LauncherUI'
}
driver = webdriver.Remote(server, desired_caps)
wait = WebDriverWait(driver, 30)
login = wait.until(EC.presence_of_element_located((By.ID, 'com.tencent.mm:id/cjk')))
login.click()
phone = wait.until(EC.presence_of_element_located((By.ID, 'com.tencent.mm:id/h2')))
phone.set_text('18888888888')Copy the code

Be sure to reconnect the phone, and then run this code, then you can observe the mobile phone first popup the wechat welcome page, and then simulate click the login button, enter the phone number, the operation is complete. We have successfully implemented the operation of the App using Python code.

Four, API

Let’s take a look at how the App operates in code and summarize the usage of the related apis. Here use Python library for AppiumPythonClient, its making address for https://github.com/appium/python-client, this library inherited from the Selenium, using methods and Selenium has much in common.

1. Initialization

You need to set the Desired Capabilities parameter, Complete configuration instructions can refer to https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/caps.md. In general, you can configure a few basic parameters, as follows:

from appium import webdriver

server = 'http://localhost:4723/wd/hub'
desired_caps = {
    'platformName': 'Android'.'deviceName': 'MI_NOTE_Pro'.'appPackage': 'com.tencent.mm'.'appActivity': '.ui.LauncherUI'
}
driver = webdriver.Remote(server, desired_caps)Copy the code

The Desired Capabilities of starting the wechat App are configured here, so that Appnium will automatically look up the package name and entry class on the phone, and then start it. The package name and the entry class name are available in the Androidmanifest.xml file in the installation package.

If the App to be opened has not been installed on the mobile phone, we can directly specify the App parameters as the path of the installation package, so that the App will be automatically installed and started on the mobile phone when the program is started, as shown below:

from appium import webdriver

server = 'http://localhost:4723/wd/hub'
desired_caps = {
    'platformName': 'Android'.'deviceName': 'MI_NOTE_Pro'.'app': './weixin.apk'
}
driver = webdriver.Remote(server, desired_caps)Copy the code

When the program starts, it will find the APK installation package in the current path of the PC, and then install it into the phone and start.

2. Find the element

We can use the common lookup methods in Selenium to find elements, as shown below:

el = driver.find_element_by_id('com.tencent.mm:id/cjk')Copy the code

In Selenium, other methods for finding elements also apply and will not be covered here.

On Android, we can also use UIAutomator for element selection, as shown below:

el = self.driver.find_element_by_android_uiautomator('new UiSelector().description("Animation")')
els = self.driver.find_elements_by_android_uiautomator('new UiSelector().clickable(true)')Copy the code

On iOS, we can use UIAutomation for element selection, as shown below:

el = self.driver.find_element_by_ios_uiautomation('.elements()[0]')
els = self.driver.find_elements_by_ios_uiautomation('.elements()')Copy the code

You can also use iOS Predicates for element selection, as shown below:

el = self.driver.find_element_by_ios_predicate('wdName == "Buttons"')
els = self.driver.find_elements_by_ios_predicate('wdValue == "SearchBar" AND isWDDivisible == 1')Copy the code

You can also use iOS Class Chain to make the selection, as shown below:

el = self.driver.find_element_by_ios_class_chain('XCUIElementTypeWindow/XCUIElementTypeButton[3]')
els = self.driver.find_elements_by_ios_class_chain('XCUIElementTypeWindow/XCUIElementTypeButton')Copy the code

But this method is only applicable to XCUITest drive, specific can refer to: https://github.com/appium/appium-xcuitest-driver.

3. Click on the

Tap to use the tap() method, which can simulate finger clicks (up to five fingers), and set the time length (milliseconds), as follows:

tap(self, positions, duration=None)Copy the code

The last two parameters are as follows.

  • Positions: This is a list of clicked positions.

  • Duration: This is the click duration.

An example is shown below:

driver.tap([(100, 20), (100, 60), (100, 100)], 500)Copy the code

This will simulate clicking on certain points on the screen.

For an element such as a button, we can simulate a click by calling the cilck() method directly, as shown in the following example:

button = find_element_by_id('com.tencent.mm:id/btn')
button.click()Copy the code

4. Drag the screen

You can simulate screen scrolling using the Scroll () method as follows:

scroll(self, origin_el, destination_el)Copy the code

You can scroll from element origin_EL to element destination_el.

Its last two arguments are as follows.

  • Original_el: This is the element being operated on.

  • Destination_el: This is the target element.

An example is shown below:

driver.scroll(el1,el2)Copy the code

Swipe () can be used to simulate sliding from point A to point B, as follows:

swipe(self, start_x, start_y, end_x, end_y, duration=None)Copy the code

The following parameters are described.

  • Start_x: This is the abscissa of the starting position.

  • Start_y: This is the ordinate of the starting position.

  • End_x: This is the abscissa of the end position.

  • End_y: This is the ordinate of the end position.

  • Duration: This is the duration, in milliseconds.

An example is shown below:

driver.swipe(100, 100, 100, 400, 5000)Copy the code

In this way, we can slide from (100, 100) to (100, 400) in 5s time.

You can simulate A quick slide from point A to point B using the flick() method as follows:

flick(self, start_x, start_y, end_x, end_y)Copy the code

The parameters are described below.

  • Start_x: This is the abscissa of the starting position.

  • Start_y: This is the ordinate of the starting position.

  • End_x: This is the abscissa of the end position.

  • End_y: This is the ordinate of the end position.

An example is shown below:

driver.flick(100, 100, 100, 400)Copy the code

5. Drag

You can drag an element to another target element using drag_and_drop() as follows:

drag_and_drop(self, origin_el, destination_el)Copy the code

You can drag the origin_EL element to the destination_el element.

The two parameters are described as follows.

  • Original_el: This is the element being dragged.

  • Destination_el: This is the target element.

An example is shown below:

driver.drag_and_drop(el1, el2)Copy the code

6. Enter a text

You can use the set_text() method for text input, as shown below:

el = find_element_by_id('com.tencent.mm:id/cjk')
el.set_text('Hello')Copy the code

7. Action chain

Similar to ActionChains in Selenium, TouchAction in Appium can support tap(), press(), long_press(), release(), move_to(), wait(), cancel() and so on. An example is shown below:

el = self.driver.find_element_by_accessibility_id('Animation')
action = TouchAction(self.driver)
action.tap(el).perform()Copy the code

First, select an element, and then use TouchAction to perform the click action.

If you want to drag, you can do it as follows:

els = self.driver.find_elements_by_class_name('listView')
a1 = TouchAction()
a1.press(els[0]).move_to(x=10, y=0).move_to(x=10, y=-75).move_to(x=10, y=-600).release()
a2 = TouchAction()
a2.press(els[1]).move_to(x=10, y=10).move_to(x=10, y=-300).move_to(x=10, y=-600).release()Copy the code

With the API above, we can do most of the things. More API operation can refer to: https://testerhome.com/topics/3711.

Five, the conclusion

In this section, we mainly understand the basic usage of the operation App of Appium, as well as the usage of common apis.


This resource starting in Cui Qingcai personal blog still find: Python3 tutorial | static find web crawler development practical experience

For more information about crawlers, please follow my wechat official account: Coder

Weixin.qq.com/r/5zsjOyvEZ… (Qr code automatic identification)