当前位置:网站首页>Python + appium automatic operation wechat is enough

Python + appium automatic operation wechat is enough

2020-11-06 01:17:00 itread01

## Introduction Appium Is an open source automated testing tool , Support Android、iOS Native applications on the platform , Support Java、Python、PHP And many languages . Appium It encapsulates Selenium, Can provide users with all the common JSON Format Selenium Commands and additional mobile device related control commands , such as : Multi touch gestures 、 Screen orientation, etc . ## The environment The main environment of this paper is as follows : + Win7 + JDK1.8 + Appium + Python3.7 + android-sdk + mumu Simulator ### JDK Download address :`https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html`, It can also be obtained directly at the end of the paper Configure environment variables : * Computers ( Right click )-> Properties -> Higher order system settings -> High order -> Environmental variables -> New environment variable `JAVA_HOME`, As shown in the figure : ![](https://img2020.cnblogs.com/blog/1497257/202011/1497257-20201104202048842-190799949.png) * System variables -> find `Path` Variables -> Editor -> Add... At the end of the variable `;%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin;` * newly build `CLASSPATH` Variables , The variable value is :`.;%JAVA_HOME%\lib;%JAVA_HOME%\lib\tools.jar` ### android-sdk Download address :`https://www.androiddevtools.cn/`, It can also be obtained directly at the end of the paper Configure environment variables : * And JDK The configuration is similar to , New environment variable `ANDROID_HOME`, The variable value is `android-sdk` Location , such as :`D:\android-sdk-windows` * stay `Path` Add... At the end of the variable `;%ANDROID_HOME%\tools;%ANDROID_HOME%\build-tools\30.0.0-preview;%ANDROID_HOME%\platform-tools` ### Appium Download address :`https://github.com/appium/appium-desktop/releases/tag/v1.18.3`, It can also be obtained directly at the end of the paper Install Python Ku :`pip install appium-python-client` Appium After the installation is completed and started , Click edit configuration , To configure JDK and android-sdk, As shown in the figure : ![](https://img2020.cnblogs.com/blog/1497257/202011/1497257-20201104202103416-304964754.png) ### mumu Download address :`http://www.51xiazai.cn/soft/584481.htm`, It can also be obtained directly at the end of the paper mumu After the simulator is downloaded , In addition to changing the installation path according to your own needs , Other options can be installed by default , Open click after loading ` Application Center `, Search wechat , Find it and install it , After the installation of wechat, log in with your own wechat . Because we're through Android `adb` Connecting virtual machines , So it needs to be executed on the console `adb connect 127.0.0.1:7555` command , Let `adb` Connect to the virtual machine . After executing the above command of the connection Simulator , We can do it in `cmd` Console input `adb devices` View the currently connected virtual machine . ## Use Start... First Appium and mumu, Because we've already configured Appium, Now let's just click Appium Of ` Start the server ` Button , As shown in the figure below : ![](https://img2020.cnblogs.com/blog/1497257/202011/1497257-20201104202116413-1758267038.png) After startup, it is shown in the figure : ![](https://img2020.cnblogs.com/blog/1497257/202011/1497257-20201104202128074-1894379158.png) Now we can go through Python Let's start wechat , The code is implemented as follows : ``` desired_caps = { "platformName": "Android", # The operating system "deviceName": "emulator-5554", # Device ID "platformVersion": "6.0.1", # Device version number "appPackage": "com.tencent.mm", # app Package name "appActivity": "com.tencent.mm.ui.LauncherUI", # app When it starts, it's mainly Activity 'noReset': True, # Whether to keep session Information , You can avoid logging in again 'unicodeKeyboard': True, # Use unicodeKeyboard To send a string 'resetKeyboard': True # Hide the keyboard } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps) ``` After running the above code , If you find mumu Wechat in the simulator has been started , It means that the basic environment has been adjusted ; If you run the code and find that you can't call mumu Wechat in the simulator , First in `cmd` In order to implement `adb connect 127.0.0.1:7555` command , Run the program again . ### Add friends Let's use Appium Realize the operation of adding new friends , The basic process is : Open wechat -> Click on ⊕-> Choose new friends -> Enter the micro signal in the search box -> Click search -> Click Add to address book , The code implementation of the function is as follows : ``` desired_caps = { "platformName": "Android", # The operating system "deviceName": "emulator-5554", # Device ID "platformVersion": "6.0.1", # Device version number "appPackage": "com.tencent.mm", # app Package name "appActivity": "com.tencent.mm.ui.LauncherUI", # app When it starts, it's mainly Activity 'noReset': True, # Whether to keep session Information , You can avoid logging in again 'unicodeKeyboard': True, # Use unicodeKeyboard To send a string 'resetKeyboard': True # Hide the keyboard } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps) time.sleep(10) print(' Click on + No ') driver.find_element_by_id('com.tencent.mm:id/ef9').click() time.sleep(5) print(' Choose new friends ') driver.find_elements_by_id('com.tencent.mm:id/gam')[1].click() time.sleep(5) print(' Click on the search box ') driver.find_element_by_id('com.tencent.mm:id/fcn').click() time.sleep(5) print(' Enter the micro signal in the search box ') driver.find_element_by_id('com.tencent.mm:id/bhn').send_keys('ityard') time.sleep(3) print(' Click search ') driver.find_element_by_id('com.tencent.mm:id/ga1').click() time.sleep(3) print(' Click Add to address book ') driver.find_element_by_id('com.tencent.mm:id/g6f').click() ``` In a nutshell , In code, we use `driver.find_element_by_id('com.tencent.mm:id/xx')` To get the elements on wechat , If there's a repetition , You can use `driver.find_elements_by_id('com.tencent.mm:id/xx')[n]` Come and get , Through `send_keys('xx')` Realize the input of information , Through `click()` To realize the click operation . Above we said pass `find_element(s)_by_id('com.tencent.mm:id/xx')` To get the elements , So how to determine `xx` Well ? Now let's take a look at . First of all, let's click Appium Position of magnifying glass in , As shown in the figure below : ![](https://img2020.cnblogs.com/blog/1497257/202011/1497257-20201104202143232-1684553235.png) Click to enter the following interface : ![](https://img2020.cnblogs.com/blog/1497257/202011/1497257-20201104202154273-1578964666.png) We're in the picture ` The required function ` In the code below `desired_caps` Information configuration in , After configuration, click ` Start the session ` Button , When we start, we'll find Appium China and mumu The effect of wechat in is inconsistent , As shown in the figure below : ![](https://img2020.cnblogs.com/blog/1497257/202011/1497257-20201104202205334-119776773.png) At this point, just click the rearrange button circled in the red box above , Now we can determine the value of the element ( That is to say `xx`), such as : Let's confirm the new location in wechat `⊕` Value , Click on... With the mouse `⊕` You can view , As shown in the figure below : ![](https://img2020.cnblogs.com/blog/1497257/202011/1497257-20201104202215682-1653597752.png) We then click on `⊕`, The operation steps are as follows : Come first mumu In the simulator, select the... In wechat `⊕`, As shown in the figure below : ![](https://img2020.cnblogs.com/blog/1497257/202011/1497257-20201104202226884-1118317961.png) Click and then go to Appium Click the refresh button , As shown in the figure below : ![](https://img2020.cnblogs.com/blog/1497257/202011/1497257-20201104202236873-1484534303.png) From the figure, we can see that the value of each option in the list is `com.tencent.mm:id/gam`, At this point in the code we use `driver.find_elements_by_id('com.tencent.mm:id/gam')[1]` Come and get it , Through the above introduction, I believe you have a good understanding of Appium We have a basic understanding of the use of . ### Send a message The basic process we simulate is : Open wechat -> Click on the magnifying glass of search -> Enter your friend's nickname in the search box -> Click on the friends you found -> Transmit text + expression , The code is implemented as follows : ``` desired_caps = { "platformName": "Android", # The operating system "deviceName": "emulator-5554", # Device ID "platformVersion": "6.0.1", # Device version number "appPackage": "com.tencent.mm", # app Package name "appActivity": "com.tencent.mm.ui.LauncherUI", # app When it starts, it's mainly Activity 'noReset': True, # Whether to keep session Information , You can avoid logging in again 'unicodeKeyboard': True, # Use unicodeKeyboard To send a string 'resetKeyboard': True # Hide the keyboard } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps) time.sleep(10) print(' Click on the wechat search box ') driver.find_element_by_id('com.tencent.mm:id/f8y').click() time.sleep(10) print(' Enter the search information in the search box ') driver.find_element_by_id('com.tencent.mm:id/bhn').send_keys('Python waiter ') time.sleep(3) print(' Click on the friends you found ') driver.find_element_by_id('com.tencent.mm:id/tm').click() time.sleep(3) # Input text driver.find_element_by_id('com.tencent.mm:id/al_').send_keys('hello') time.sleep(3) # Input the expression driver.find_element_by_id('com.tencent.mm:id/anz').click() time.sleep(3) driver.find_element_by_id('com.tencent.mm:id/rv').click() # Click the send button to send information driver.find_element_by_id('com.tencent.mm:id/anv').click() # sign out driver.quit() ``` And finally , Because the simulator may be slower , If the program runs with errors , You can change the waiting time between `time.sleep(x)` Set it bigger . Software and code in the public name **Python waiter ** Backstage reply **201104**

版权声明
本文为[itread01]所创,转载请带上原文链接,感谢