pyest+appium realizes APP automated testing, and the ideas are summarized here

Table of contents

01. Appium environment construction

2. Build python+pytest+appium environment

3. Install pycharm to build a project and write scripts

4. Execute the test

meager strength


01. Appium environment construction

  • install nodejs

http://nodejs.cn/

Why install nodejs?

Because the server of the appium tool is developed by the nodejs language

  • Install jdk and configure environment variables

Why install jdk?

Because we want to test Android, the Android debugging environment needs to rely on jdk

  • Install sdk and configure environment variables

Why install sdk?

Because we want to test Android, sdk is the environment for Android development and debugging

  • Install appium server

1. Appium server in command line form

npm install [email protected]

2. The appium desktop version can be downloaded from the official website

3. The difference between the above two

The desktop version is not just an appium server, it also includes the function of element positioning debugging and recording script

The command line version is just an appium server

  • Install appium-doctor

npm install appium-doctor

This tool is used to check the appium environment, but it does not mean that there is no error in the check here, which means that my environment must have no problems

  • Install an Android emulator (not required)

Yagami, mumu, etc.

 

2. Build python+pytest+appium environment

  • install python

  • Install pytest

pip install pytest

  • install selenium

pip install selenium

  • install appium

pip install Appium-Python-Client

  • Install allure-pytest

pip install allure-pytest

3. Install pycharm to build a project and write scripts

  •  Initialize driver code writing

# !/usr/bin python3                        

# encoding: utf-8 -*-                        

# @author: 沙陌 微信:Matongxue_2

# @Time: 2021/5/21 10:25

# @Copyright:北京码同学网络科技有限公司



#移动端初始化用到的参数特别多

#下面的这个字典里的参数是脚本告诉appium服务端我都要干什么,基础的能力参数

import time

import allure

from appium import webdriver

from selenium.webdriver.common.by import By

desired_caps = {

   #这表示你要测试的设备类型,安卓就写android,ios就写ios

   'platformName':'android',

   #这表示你要测试的app的路径

   'app':'C:\\Users\\lixio\\Desktop\\douban.apk',

   #看起来像是设备名称,但是注意在安卓上这个位置的值随便写都行,

   #这个并不能够代表你要执行的设备是哪个

   'deviceName':'xxx',

   #这个参数才是真正的指定执行哪台设备的参数,

   # 他的值写的就是使用adb devices命令看到的设备名称

   'udid':'127.0.0.1:62001',

   #该参数非常重要,表示在执行测试时不重签名apk文件,

   # 默认情况下appium会针对apk文件进行重签名操作,

   # 但是现在的app都有签名篡改的校验,如果被重签名了那么这个app就不能正常使用了

   'noSign':True,

   #该参数表示在执行appium测试时使用appium的辅助输入法,

   # 好处是支持中文输入支持键盘隐藏

   'unicodeKeyboard':True,

   #表示执行完测试之后将手机的输入法重置回手机原来的

   'resetKeyboard':True,

   #表示脚本和appium服务之间连接的session超时时间,单位是秒

   'newCommandTimeout':600

}

# 初始化driver,参数里看到的url就是appium服务的地址 driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

#增加隐式等待

driver.implicitly_wait(10) #单位是秒

```
  • element view

You can use the uiautomatorviewer.bat tool that comes with the sdk, or you can use the appium desktop version of the element to view

  • Douban login script writing, using pytest method

@allure.title('豆瓣登录')

def test_login():

   # id定位在安卓端对应的元素属性是resource-id

   driver.find_element(By.ID,'com.douban.frodo:id/left').click()

   driver.find_element(By.ID,'com.douban.frodo:id/input_user_name').send_keys('18729399607')

   driver.find_element(By.ID,'com.douban.frodo:id/input_password').send_keys('abc123456')

   driver.find_element(By.ID,'com.douban.frodo:id/sign_in_douban').click()

   #操作完成后,必须有断言来判断结果是否正常

   time.sleep(3)

   page_source = driver.page_source

   assert '书影音' in page_source

 

4. Execute the test

  • Preparatory work before execution

1. Start the appium service, and do the following operations on the command line

2. Make sure that the mobile device is connected normally

  • Execute the test

Execute the following command in the Terminal of pycharm

pytest -sv --alluredir ./report/allure-results --clean-alluredir

  • Generate allure test report

allure generate ./report/allure-results -o ./report/allure-report --clean


meager strength

Finally, I would like to thank everyone who has read my article carefully. Seeing the fans’ growth and attention all the way, there is always a need for reciprocity. Although it is not a very valuable thing, you can take it away if you need it:

These materials should be the most comprehensive and complete preparation warehouse for friends who want to advance [automated testing]. This warehouse has also accompanied me through the most difficult journey, and I hope it can help you too! Everything should be done as early as possible, especially in the technical industry, we must improve our technical skills. I hope to be helpful…

Guess you like

Origin blog.csdn.net/m0_58026506/article/details/130172493