在学习了Python之后,这两天写了一个小的自动化程序来一起分享。
首先说明的是,图片你需要上传到你的模拟器上面。
# This sample code uses the Appium python client
# pip install Appium-Python-Client
# Then you can paste this into a file and simply run with Python
# http://appium.io/docs/en/writing-running-appium/caps/index.html
import os
from appium import webdriver
from selenium.common.exceptions import NoSuchElementException
import time
caps = {}
caps["automationName"] = "UiAutomator2"
caps["platformName"] = "Android"
caps["deviceName"] = "127.0.0.1:21503" # 使用自己的手机devices name
caps['noReset'] = True
caps['fullReset'] = False
caps['appPackage'] = "com.sina.weibolite"
caps['appActivity'] = "com.sina.weibo.SplashActivity"
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", caps)
driver.implicitly_wait(30)
import os
jpgfile = os.path.join(os.path.dirname(__file__),'helloworld.jpg')
# 推送图片
driver.push_file('/sdcard/DCIM/helloworld.jpg',source_path=jpgfile)
#
while True:
try:
create_btn_android = 'new UiSelector().className("android.widget.TextView").index(0).resourceId("com.sina.weibolite:id/titleSave")'
driver.find_element_by_android_uiautomator(create_btn_android).click()
break
except NoSuchElementException:
driver.find_element_by_android_uiautomator('new UiSelector().text("不了,谢谢").index(0)').click()
weibo_content = """
今天搭建一起做app 自动化测试,学习了很多知识。
学习了如下内容:
1.appium 环境配置
2.元素定位
3.自动发微博
"""
edit_id='com.sina.weibolite:id/edit_view'
driver.find_element_by_id(edit_id).send_keys(weibo_content)
# 发图片
image_android = 'new UiSelector().resourceId("com.sina.weibolite:id/tvTitle").text("相册").index(1)'
driver.find_element_by_android_uiautomator(image_android).click()
# 选择第一张
first_image_android = 'new UiSelector().resourceId("com.sina.weibolite:id/photo_album_grideview_item_select").index(2)'
driver.find_element_by_android_uiautomator(first_image_android).click()
driver.find_element_by_accessibility_id('下一步').click()
driver.find_element_by_id('com.sina.weibolite:id/btn_confirm_edit').click()
send_btn_android='new UiSelector().resourceId("com.sina.weibolite:id/titleSave").text("发送").index(0)'
driver.find_element_by_android_uiautomator(send_btn_android).click()
driver.quit()