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
-
Install Python3
-
Install the Appium library for Python3
-
Install the Android SDK
-
Install JDK
-
Install Appium
-
Install the emulator
-
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.
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 Scripts
the folder, and click it, for example:
Type in the address bar cmd
and press Enter to open the console:
Enter the command in the console pip install Appium-Python-Client
and press Enter
See the prompt in the figure below to indicate that the installation is successful
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
The download is complete and the installation starts
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 D
disk
Next all the way to Next, pay attention to the page of selecting SDK
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
Continue to Next to reach the custom component page, as shown below
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
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
5. Install Appium
Official website address http://appium.io/
Click the download button to go to the GitHub download page, select the corresponding platform to download
Choose this for Windows (quick download)
After the installation is complete, start Appium, host and port default, and then edit the configuration information
Then click the save and restart button below, and then click the first Start Server
button, you will see
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
bin
the directory (for example:D:\Nox\bin
) -
Copy the files in the Android SDK installed in the third section
adb.exe
(for example, mine is inD:\Android\SDK\platform-tools
the directory) to this directory and overwrite the existingadb.exe
files in this directory -
Delete
nox_adb.exe
the files in this directory, make another copyadb.exe
and 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 executeadb devices
, if you see the following information, it means success
Install the test file in the simulator apk
, here is a calculator as an example, just drag apk
the file into the simulator
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.apk
is apk
the full path of the test.
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 testcase
in 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.py
a 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.py
the file After the program finishes running, a test report
will be generated in the directorytest_report
Open it in the browser to see the report content, example
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.
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.