Appium test framework was built

Github address:github.com/…

Because most of the information on Appium is basic, including environment building, single interface test, single control test, etc. The article about Appium testing framework was not found, so this article is mainly about the construction of Appium framework.

The construction process of Appium environment will not be introduced. You can check relevant articles online or refer to my project.

Start Android emulator video:

Appium test video:

First, apPIUM_Demo project overall idea introduction

To start a project, you only need to run thetest.pyCan.

Project initiation analysis

1.multi_appium.start_service()

This method kills the started Appium process before starting the Appium service.

2.multi_appium.con_device()

This method performs the following method to create the WebDriver object of the Appium package with two parameters, the Appium service and the device parameter object. And assign to the driver property of the base AppElement class.

` AppElement. Driver `. = webdriver.Remote(AppConfig.service_path, desired_caps)Copy the code

3. App = app () and app.test()

This method creates App objects and performs interface test methods.

Ii. Appium_demo project construction process

1. Create the base class

View->ViewGroup->Activity Create the control base AppElement class from which all viewGroups and activities inherit.

Add webDriver to AppElement

This allows all interfaces or layouts to find or manipulate Element elements using WebDriver. Similar to the basic attribute of BaseActivity in APP, it can be called in all interfaces.

Since the process of using Appium is to create the WebDriver first, we assign the value to AppElement.driver after creating the WebDriver object. We then create the MainActivity object to use the WebDriver method and test it out.

1.2 Add basic Element lookup methods to AppElement :findElementId, findElementText, findElementTextWait.

With the findElementId method, we can delay finding Element elements and catch logic such as exceptions. And other commonly used swiping in a certain direction, such as swipeUp, swipeDown, etc.

1.3 Create the title bar TitleLayou class on the interface

Initialize the public property title text control: title, return button: back, and right function button: iv_right in the TitleLayout class. The basic method for compiling the title bar of the interface: click return button method: Finish, get the title text control text method: getTitle, click the right function button method: rightClick.

1.4 Creating the base class Activity for the interface

Initialize the interface’s title bar in the Activity base class: title. According to the class_name of the incoming interface, search for the full path of the corresponding class from the activity_list of the globally configured class name and package name. Use the driver.wait_activity method when initializing the interface to wait for the interface to appear to prevent exceptions caused by searching for controls before the interface is loaded.

1.5 Add the Activity class name and package name to the activity_list in the Module package for easy saving and reading:

2. Interface creation

2.1 Create a feedback interface FeedbackActivity class.

This class inherits from the Activity base class and overwrites the Activity’s __init__ method using the Activity.__init__(self, self.__class__.__name__) method. (Unlike Java, you must override the Activity’s __init__ method.)

Passing the __name__ of the current class into Acitivity using the __init__ method performs the wait_activity method described above and initializes the interface title.

2.2 Write operations and test control methods and logic in FeedbackActivity interface.

As shown in the suggestion feedback interface above, there is a text box for content entry and a button to add an image below.

So in the suggestion feedback interface, we write the method of [prompting users to input feedback content] – [inputContent], and the following method of [add picture] – [selectPicture], the method of [please input mobile phone number] – [inputPhone], [click submit button] method – [submitFeedBack] and so on. You can execute all the methods when you initialize the FeedbackActivity interface, or you can call the methods individually after you create the FeedbackActivity instance.

In the FeedbackActivity screen, we execute methods in the following order: InputContent ->selectPicture->inputPhone->submitFeedBack-> Finish

Such a test interface is written.

2.3 After writing the test interface code, we also need to write the logic of **APP entering the test interface FeedbackActivity**

In the startup process of Appium program, when we create webDriver object to start APP, we need to add startup interface information in APP configuration information parameters:

Appelement. driver = webdriver.Remote(appconfig.service_path, Desired_caps) # appActivity corresponds to the information on the startup screen desired_caps = {..... 'appPackage': 'com.ennova.dreamlf', 'appActivity': 'com.ennova.dreamlf.module.main.splash.SplashActivity', }Copy the code

After Appium to start the APP will automatically jump to the home page of the APP. The module. The main. MainActivity. Therefore, we need to write the main interface of APP MainActivity class and the corresponding jump method.

2.3.1 Write the code of MainActivity on the main interface

As shown in the figure above, the main interface of MainActivity consists of the home page and my two modules, so we first create the required MainActivity and HomeFragment classes in the mainac.py file. Create a homeTest method in the MainActivity class to test the home page module logic.

def __init__(self):
	...
def homeTest(self, vp_test=True, grid_test=True):
    home_fragment = HomeFragment()
    if vp_test: home_fragment.viewPagerTest()
    if grid_test: home_fragment.gridClick(3)
Copy the code

In the homeTest method, create a HomeFragment object and perform either a click-test on the top of the wheel chart layout or a click-test on the middle grid button.

After entering the MainActivity interface, execute the method homeTest(vp_test=False, grid_test=True) to test the nine grid click logic.

2.3.2 Write the code of home Page HomeFragment

def viewPagerTest(self): ..... def gridClick(self, module_num=-1): module_rv = self.findElementId("module_rv") ..... if module_num == 3 or module_num == -1: Module_rv. find_elements(by= by.class_name, value="android.widget.LinearLayout")[3].click() feedbackactivity = FeedbackActivity()Copy the code

In the gridClick method, when module_num is 3, the APP enters the FeedbackActivity interface by looking for the fourth child element in the module_RV list layout, the suggestion feedback control, and clicking on it.

After clicking on the suggestion feedback control on the home page, test the suggestion feedback screen by creating FeedBackActivity = FeedBackActivity ().

Run the test program to test the interface function

The testing framework is now complete.

In addition to the Appium test framework, the appium_demo project also includes the Android emulator startup program and the AppiumServie startup program.

The location where the Android emulator is launched

android_avd.py

start_avd()

The location where the AppiumServie program is started

multi_appium.py

start_service()