Appium+python实现App自动化书写文本

以下是个人用Appium + python写的第一个App自动化案例,比较简单。

1.获取设备及应用信息

desired_caps={}
desired[platformName]='Android'  #设备系统
desired[platformVersion]='4.4.2' #设备系统版本
desired[deviceName]='127.0.0.1:22515' #设备名称
desired[appPackage]='com.youdao.note'
desired[appActivity]='.activity2.SplashActivity'

2.获取Appium client端口值

driver=webdriver.Remote("http://127.0.0.1:4723/wd/hub",
desired_caps)

3.根据UIautomaterviewer定位元素

driver.find_element(By.ID,'com.youdao.note:id/add_note_floater
_open').click()
time.sleep(3)
driver.find_element(By.NAME,u'新建笔记').click()
time.sleep(3)
driver.find_element(By.ID,'com.youdao.note:id/note_title')
.send_keys(u'光环演讲会')
time.sleep(3)
driver.find_element(By.XPATH,'//android.widget.LinearLayout
[@resourceid=\"com.youdao.note:id''/note_content\"]/android.widget
.EditText[1.send_keys(u'七点半签到')
time.sleep(3)
driver.find_element(By.ID,'com.youdao.note:id
/actionbar_complete_text').click()
time.sleep(5)

driver.quit()

4.完整代码

#coding=utf-8
'''
新建云笔记
'''
from selenium.webdriver.common.by import By
from appium import webdriver
import time

#1.手机信息
desired_caps={
    'platformName':'Android',
    'platformVersion':'4.4.2',
    'deviceName':'127.0.0.1:22515',
    'appPackage':'com.youdao.note',
    'appActivity':'.activity2.SplashActivity',
    'unicodeKeyboard':'True',
    'resetKeyboard':'True'
}
#2.连接appium,并将手机信息导入,开启app
driver=webdriver.Remote("http://127.0.0.1:4723/wd/hub",desired_caps)
time.sleep(5)
#+号
driver.find_element(By.ID,'com.youdao.note:id/add_note_floater_open').click()
#新建笔记  中文前加u   text-->name   content-desc-->name
driver.find_element(By.NAME,u'新建笔记').click()
#标题
driver.find_element(By.ID,'com.youdao.note:id/note_title').send_keys(u'光环演讲会')
#正文
#xpath 手写 "//类名[@属性名1=\"属性值2\" and ... ]/类名[1]"  利用上层元素寻找下层元素
driver.find_element(By.XPATH,'//android.widget.LinearLayout[@resource-id=\"com.youdao.note:id'
                             '/note_content\"]/android.widget.EditText[1]').send_keys(u'七点半签到')
#完成
driver.find_element(By.ID,'com.youdao.note:id/actionbar_complete_text').click()
#退出app
driver.quit()

猜你喜欢

转载自blog.csdn.net/yinlin330/article/details/82226863
今日推荐