Appium+python automation (seven) - first met Pipa female Appium - on (super detailed)

 "After a thousand calls, I came out, still holding the pipa and half covering my face." After all the previous preparations, the pipa girl Appium was finally invited out. Then let me introduce this beauty (handsome guy) to all the judges, friends and children's shoes. This article is mainly to make a small summary of the previous content. We can't just bury our heads in the cart, and don't remember to look up and see the road!

Explanation: Appium--the tuba of the slave family, mentioning the tuba of the slave family is known to everyone in the world, and everyone knows it. Servant == Appium;

1. Self-introduction    

       Nujia is an open source automated testing tool. Nujia can support native, mobile browser-based, hybrid applications (APP) on iOS and Android platforms.

1. The benefits of inviting slaves to play (the benefits of using appium for automated testing)

       Nujia uses standard automation APIs on different platforms, so there is no need to recompile or modify your own applications when cross-platform.

       Naojia supports all languages ​​supported by Selenium WebDriver, such as Java, Object-C, JavaScript, Php, Python, Ruby, C#, Clojure, or Perl language, and you can use the API of Selenium WebDriver. Appium supports any kind of testing framework. Appium realizes the real cross-platform automated testing. (This article mainly introduces the usage of Python)

2. The internal organs of the slave family (Appium architecture)

      Nujia is an HTTP server written in Node.js, which creates and manages multiple WebDriver sessions to interact with different platforms, such as iOS, Android, etc. 

      After Nujia starts a test (playing a song), it will start a server on the device under test - Pipa (mobile phone), and listen to instructions from the Appium server (viewer, audience). Each platform like iOS and Android There are different ways of running and interacting. Therefore, Nujia will use a stub program to "hack" the platform and accept instructions to complete the running of the test case (playing the score).

Start the topic directly, and conduct the actual combat of automated testing on the mobile terminal (pipa playing - ambush on all sides)

2. Nujia's actual combat skills (taking the real machine <Pipa> as an example)

Ladies and gentlemen, here are some fingerings (commands) for pipa girls listed for you.

       aapt dump badging D:\XXX.apk Get all the information of the installation package

       adb devices (check if the phone is connected to the computer)

       adb shell pm list packages: List all package names and find the package name of the viewed package.

       adb shell dumpsys package com.android.XXX: View the specific information of a package

       other:

       adb devices: Check whether the Android device is connected to the computer.

       adb shell dumpsys activity: check which activity is currently running, some running processes, etc.

       adb shell dumpsys activity activities

       adb shell pm list packages: List all package names.

       adb shell dumpsys package: List all installed application information

       adb shell dumpsys package com.android.XXX: View the specific information of a package

       adb shell dumpsys activity | grep mFocusedActivity: check which activity is currently resumed

       adb logcat | grep ActivityManager: View the currently running Activity

       adb logcat | grep Displayed: View the currently running Activity

1. Connect to the real device or the simulator -- here is how the female Pipa finger connects with the Pipa

1) When connecting the mobile phone, pay attention to select the developer mode of the mobile phone, open the command line window with cmd, and enter adb devices to get the deviceName of the device.

2) Start the emulator, connect the emulator AVD (Google comes with it), open the command line window with cmd, and enter adb devices to get the deviceName of the device

3) Start the emulator and connect to the Yeghen emulator, pay attention to input nox_adb.exe connect 127.0.0.1:62001 (equivalent to inserting the data cable), and then continue, enter adb devices

2. Get the Activity (Get the tune in the music score, which is similar to "Sweeping Trash in the Secret Method of Rui" in our current music)

1) Open the command line window with cmd, and enter aapt dump badging D:\XXX.apk (xxx refers to the name of the installation package, and D:\ refers to the path) to directly view the specific information of the app installation package to be tested.

2) The marked is the package name, continue to drag down, you can find the activity information.

       The red circle here can see the activity information. These two values ​​need to be obtained in advance in the appium script, so this acquisition method needs to be introduced in advance. If you think it looks ugly in cmd, you can export the cmd information to a txt file, for example aapt dump badging shoujibaidu.apk > 123.txt After pressing Enter, a 123.txt file will be generated on the desktop, you can open it by Query to find this information. Through the above method, as long as we have the android SDK environment, then we can get the package name and Activity information of any package.

3. Start Appium 

     1) Double-click the appium icon on the desktop to open it, and click Android Settings to set it.

2) Fill in the deviceName of the real device obtained above, and select platformName and platformVersion

 3) Configure the session of the service to be rewritable (non-essential configuration)

4) After configuration, click the start button in the upper right corner to start.

5) The startup is successful, the next step is to write the code.

4. Write the device and installation package information obtained above into the script

 1 # coding=utf-8
 2 # 1.先设置编码,utf-8可支持中英文,如上,一般放在第一行
 3 
 4 # 2.注释:包括记录创建时间,创建人,项目名称。
 5 '''
 6 Created on 2019-6-19
 7 @author: 北京-宏哥   QQ交流群:707699217
 8 Project:初始琵琶女appium
 9 '''
10 # 3.导入模块
11 from appium import webdriver
12 import time
13 desired_caps = {}
14 desired_caps['platformName'] = 'Android'   #android的apk还是IOS的ipa
15 desired_caps['platformVersion'] = '8.0'  #android系统的版本号
16 desired_caps['deviceName'] = 'emulator-5554'    #手机设备名称,通过adb devices  查看
17 desired_caps['appPackage'] = 'com.taobao.taobao'  #apk的包名
18 desired_caps['appActivity'] = 'com.taobao.tao.welcome.Welcome'  #apk的launcherActivity
19 # desired_caps['unicodeKeyboard'] = True   #使用unicodeKeyboard的编码方式来发送字符串
20 # desired_caps['resetKeyboard'] = True   #将键盘给隐藏起来

5. Find app positioning elements

1) Start an artifact in the Android SDK to find the positioning elements of the app for script writing. There is a uiautomatorviewer.bat in the tools of the Android SDK, as shown in the following figure:

2) Click uiautomatorviewer.bat to start. There are four buttons in the upper left corner, which are used to open the local file (open), Device Screenshot (uiautomator dump) dump page, dump compressed format page, and save; after starting, click the third button in the upper left corner Icons are used to present the real machine interface.

3) Select device simulator or real machine

4) The first connection will be slow, wait patiently, and it will be fast after the connection is completed. Here I take the Baidu interface as an example.

This is the interface display after connecting to the Yeshen emulator. In this way, positioning can be performed based on id, class, text and other positioning elements.

1 driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
2 driver.find_element_by_id("index-kw").click()
3 driver.find_element_by_id("index-kw").clear()
4 driver.find_element_by_id("index-kw").send_keys('appium测试')
5 
6 7 driver.find_element_by_id("index-bn").click()
8 
9 driver.quit()

3. Take Taobao APP as an example

1. Example code

2. Code running result

3. Appium running results

4. Simulator running results

5. Reference code

 1 # coding=utf-8
 2 # 1.先设置编码,utf-8可支持中英文,如上,一般放在第一行
 3 
 4 # 2.注释:包括记录创建时间,创建人,项目名称。
 5 '''
 6 Created on 2019-6-19
 7 @author: 北京-宏哥   QQ交流群:707699217
 8 Project:学习和使用python代码appium+pycharm+连接夜神模拟器
 9 '''
10 # 3.导入模块
11 from appium import webdriver
12 import time
13 desired_caps = {}
14 desired_caps['platformName'] = 'Android'   #android的apk还是IOS的ipa
15 desired_caps['platformVersion'] = '8.0'  #android系统的版本号
16 desired_caps['deviceName'] = '127.0.0.1:62001'    #手机设备名称,通过adb devices  查看
17 desired_caps['appPackage'] = 'com.taobao.taobao'  #apk的包名
18 desired_caps['appActivity'] = 'com.taobao.tao.welcome.Welcome'  #apk的launcherActivity
19 #desired_caps['unicodeKeyboard'] = True   #使用unicodeKeyboard的编码方式来发送字符串
20 #desired_caps['resetKeyboard'] = True   #将键盘给隐藏起来
21 driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps) #启动服务器地址,后面跟的是手机信息
22 # 休眠五秒等待页面加载完成
23 time.sleep(5)
24 
25 driver.find_element_by_id("com.taobao.taobao:id/home_searchedit").click()
26 time.sleep(4)
27 driver.find_element_by_id("com.taobao.taobao:id/searchEdit").clear()
28 time.sleep(3)
29 driver.find_element_by_id("com.taobao.taobao:id/searchEdit").send_keys("琵琶")
30 time.sleep(2)
31 driver.find_element_by_id("com.taobao.taobao:id/searchbtn").click()
32 
33 # driver.quit()

At this point, run the script to perform automated testing. (After the pipa girl played, there was thunderous applause, and the pipa girl clasped her fists with both hands, and exited the stage full of pride)

summary

1. The effect of adb.exe connect 127.0.0.1:62001 and nox_adb.exe connect 127.0.0.1:62001 is the same, the premise is that you have configured system variables and resolved version conflicts.

2. Well, that's the end of the summary about appium. If you want to know what's going on, let's listen to the next chapter. hey-hey! ! !


              [The following is the most complete software test engineer learning knowledge architecture system diagram in 2023 that I compiled]


1. From entry to mastery of Python programming

2. Interface automation project actual combat

3. Actual Combat of Web Automation Project


4. Actual Combat of App Automation Project

5. Resume of first-tier manufacturers


6. Test and develop DevOps system

7. Commonly used automated testing tools


Eight, JMeter performance test

9. Summary (little surprise at the end)

life is long so add oil. Every effort will not be let down, as long as you persevere, there will be rewards in the end. Cherish your time and pursue your dreams. Don't forget the original intention, forge ahead. Your future is in your hands!

Life is short, time is precious, we cannot predict what will happen in the future, but we can grasp the present moment. Cherish every day and work hard to make yourself stronger and better. Firm belief, persistent pursuit, success will eventually belong to you!

Only by constantly challenging yourself can you constantly surpass yourself. Persist in pursuing your dreams and move forward bravely, and you will find that the process of struggle is so beautiful and worthwhile. Believe in yourself, you can do it!

Information acquisition method:
This document and video information should be the most comprehensive and complete preparation warehouse for friends who want to engage in [software testing]. This warehouse has also accompanied tens of thousands of test engineers through the most difficult journey. Hope Can help you too! All of the above can be shared, click the small card below to enter the group to receive.

Guess you like

Origin blog.csdn.net/NHB456789/article/details/131739031