Python3 + Appium + Android emulator realizes APP automated testing and generates test reports

This article mainly introduces Python3 + Appium + Android emulator to realize APP automated testing and generate test reports. This article introduces you in great detail and has certain reference value for your study or work. Friends in need can refer to the following

This article is mainly divided into the following parts

  1. Install Python3

  2. Install the Appium library for Python3

  3. Install the Android SDK

  4. Install JDK

  5. Install Appium

  6. Install the emulator

  7. Write test scripts and generate test reports

Project sample download address: https://github.com/lixk/apptest

text

1. Install Python3

Just log in to the Python official website https://www.python.org/ and download the latest version.

picture

Then pay attention to the installation path when installing, such as my installation path D:\Python37, which will be used next.

2. Install the Appium library of Python3

Open the Python installation directory, find Scriptsthe folder, and click it, for example:

picture

Type in the address bar cmdand press Enter to open the console:

picture

Enter the command in the console pip install Appium-Python-Clientand press Enter

picture

See the prompt in the figure below to indicate that the installation is successful

picture

3. Install the Android SDK

I was too lazy to toss, so I downloaded the Android Studio
download address
https://developer.android.com/studio/#downloads

picture

The download is complete and the installation starts

picture

If you need to install the built-in emulator, check this item, then Next, and then select the installation path, for example, mine is also placed on the Ddisk

picture

Next all the way to Next, pay attention to the page of selecting SDK

picture

Select Custom, so that you can only install the parts you like, and then select the theme, of course, this can be changed in Android Studio later

picture

Continue to Next to reach the custom component page, as shown below

picture

Set the installation path of the SDK, such as mine D:\Android\SDK, and then continue to Next...finish. Then it enters the slow download link, let it download slowly here, and it will be fine after the download is completed. We can move on to the next section.

4. Install JDK

Here I am using JDK8, download address
https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

Accept the agreement, and then select the corresponding platform to download

picture

The next installation process is relatively simple, and it is all the way to Next. Pay attention to choose the installation path, such as my installation pathD:\Java\jdk1.8

picture

5. Install Appium

Official website address http://appium.io/

picture

Click the download button to go to the GitHub download page, select the corresponding platform to download

picture

Choose this for Windows (quick download)

After the installation is complete, start Appium, host and port default, and then edit the configuration information

picture

picture

Then click the save and restart button below, and then click the first Start Serverbutton, you will see

picture

If you use a real device test, you can start writing script tests by plugging in your phone. However, since there is no data cable at hand, the simulator is used, so there is the next section.

Six, install the simulator

The download address https://www.yeshen.com/ is used here 夜神模拟器. The reason why I don’t use the one that comes with Android is because I remember that it took a long time to start it many years ago, and then I never used it again.
There is nothing to say about this, just pay attention to the installation path, and then go all the way to the next step.
After the installation is complete, a simple modification should be made:

  • Open the installation directory and enter binthe directory (for example: D:\Nox\bin)

  • Copy the files in the Android SDK installed in the third section adb.exe(for example, mine is in D:\Android\SDK\platform-toolsthe directory) to this directory and overwrite the existing adb.exefiles in this directory

  • Delete nox_adb.exethe files in this directory, make another copy adb.exeand rename it tonox_adb.exe

  • Start the emulator

  • Then open the console in this directory, execute nox_adb.exe connect 127.0.0.1:62001, and then execute adb devices, if you see the following information, it means success

picture

Install the test file in the simulator apk, here is a calculator as an example, just drag apkthe file into the simulator

picture

Continue to enter in the console window just now aapt dump badging D:\apk\com.youdao.calculator-2.0.0.apk, which D:\apk\com.youdao.calculator-2.0.0.apkis apkthe full path of the test.

picture

You can see that the information of the installation package has been printed out, and record the two names in the red box, which will be used later when writing the test script.
Note that if the emulator restarts, you need to perform step 5. nox_adb.exe connect 127.0.0.1:62001 Seven, write test scripts and generate test reports

1. Create a test case directory and create files testcasein this directorytest_app.py

import time
import unittest

from appium import webdriver


class MyTests(unittest.TestCase):
 # 测试开始前执行的方法
 def setUp(self):
  desired_caps = {'platformName': 'Android', # 平台名称
      'platformVersion': '4.4.2', # 系统版本号
      'deviceName': '127.0.0.1:62001', # 设备名称。如果是真机,在'设置->关于手机->设备名称'里查看
      'appPackage': 'com.youdao.calculator', # apk的包名
      'appActivity': 'com.youdao.calculator.activities.MainActivity' # activity 名称
      }
  self.driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps) # 连接Appium
  self.driver.implicitly_wait(8)

 def test_calculator(self, t=500, n=4):
  """计算器测试"""
  time.sleep(3)
  window = self.driver.get_window_size()
  x0 = window['width'] * 0.8 # 起始x坐标
  x1 = window['width'] * 0.2 # 终止x坐标
  y = window['height'] * 0.5 # y坐标
  for i in range(n):
   self.driver.swipe(x0, y, x1, y, t)
   time.sleep(1)
  self.driver.find_element_by_id('com.youdao.calculator:id/guide_button').click()
  for i in range(6):
   self.driver.find_element_by_accessibility_id('Mathbot Editor').click()
   time.sleep(1)

  btn_xpath = '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.support.v4.widget.DrawerLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.LinearLayout[2]/android.widget.LinearLayout/android.widget.LinearLayout[3]/android.view.View/android.widget.GridView/android.widget.FrameLayout[{0}]/android.widget.FrameLayout'
  self.driver.find_element_by_xpath(btn_xpath.format(7)).click()
  self.driver.find_element_by_xpath(btn_xpath.format(10)).click()
  self.driver.find_element_by_xpath(btn_xpath.format(8)).click()
  time.sleep(3)

 # 测试结束后执行的方法
 def tearDown(self):
  self.driver.quit()

2. Create run.pya file

import os
import time
import unittest

from HTMLTestRunner import HTMLTestRunner

test_dir = './testcase'
discover = unittest.defaultTestLoader.discover(start_dir='./testcase', pattern="test*.py")

if __name__ == "__main__":
 report_dir = './test_report'
 os.makedirs(report_dir, exist_ok=True)
 now = time.strftime("%Y-%m-%d %H-%M-%S")
 report_name = '{0}/{1}.html'.format(report_dir, now)

 with open(report_name, 'wb')as f:
  runner = HTMLTestRunner(stream=f, title="测试报告", description="本测试报告内容包含超级计算器的简单测试")
  runner.run(discover)

The export test report is used HTMLTestRunner, but there seems to be a problem with Python3. I found two modified ones that support Python3 on the Internet. If necessary, you can download it from the sample project at the end of this article.

3. Run run.pythe file After the program finishes running, a test report
will be generated in the directorytest_report

picture

picture

Open it in the browser to see the report content, example

picture

At this point, it's all done. If you have time, you can add the use of Appium, such as element positioning, etc. I hope this article can help students who are beginners in automated testing.
Project sample download address https://github.com/lixk/apptest
Emma, ​​so much wordy, it's eight o'clock and I haven't had dinner yet, I'm so hungry, I'll be here first today.


2019/5/21 Supplement

If the configuration is consistent with the above steps, there is no problem, but the result cannot run normally. Then the problem might be in the emulator configuration. Add the emulator configuration on my side below.

picture

picture

The code 'platformVersion': '4.4.2'is the Android version in the above picture.

If there are still many people who can't run, then you can consider taking the time to record an operation video, hahaha.

This is the end of this article about Python3 + Appium + Android emulator to realize APP automated testing and generate test reports

Finally: The complete software testing video tutorial below has been organized and uploaded, and friends who need it can get it by themselves [Guaranteed 100% free]

Software Testing Interview Documentation

We must study to find a high-paying job. The following interview questions are the latest interview materials from first-tier Internet companies such as Ali, Tencent, and Byte, and some Byte bosses have given authoritative answers. Finish this set The interview materials believe that everyone can find a satisfactory job.

Guess you like

Origin blog.csdn.net/wx17343624830/article/details/131867797