Appium背景介绍
Appium是由nodejs的express框架写的Http Server,Appium使用WebDriver的json wire协议,来驱动Apple系统的UIAutomation库、Android系统的UIAutomator框架。
Appium桌面客户端安装方式
1. 运行appium - desktop - Setup - 1.2 .7.exe,默认安装即可
2. 启动客户端,按图片步骤 设置
Appium-python库安装
pip install Appium-Python-Client==1.3.0
- #导入driver对象
- from appium import webdriver
- from appium.webdriver.common.touch_action import TouchAction
- #导入参数
- caps = {}
- caps["deviceName"] = "emulator-5554"
- caps["platformName"] = "Android"
- caps["appPackage"] = "com.android.settings"
- caps["appActivity"] = "com.android.settings.Settings"
- driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
- #脚本内启动其他app
- driver.start_activity(appPackage,appActivity)
- #关闭app
- driver.close_app() # 关闭当前操作的app,不会关闭驱动对象
- #关闭驱动对象
- driver.quit()
- #安装APK到手机
- driver.install_app(app_path)
- #手机中移除APP
- driver.remove_app(app_id)
- #判断APP是否已安装
- driver.is_app_installed(bundle_id)
- #获取当前屏幕内元素结构
- driver.page_source
- #通过id定位
- driver.find_element_by_id('com.android.settings:id/search')
- #通过class定位
- driver.find_elements_by_class_name('android.widget.LinearLayout')
- #通过xpath定位
- driver.find_element_by_xpath('//*[contains(@resource-id,"com.android.settings:id/search")]')
- driver.find_element_by_xpath('//*[contains(@class,"android.widget.TextView")]')
- driver.find_element_by_xpath('//*[contains(@text,"WLAN")]').click
- #定位一组元素,注意element -> elements通过id方式定位一组元素
- driver.find_elements_by_id('com.android.settings:id/search')
- #通过class方式定位一组元素
- driver.find_elements_by_class_name('android.widget.TextView')
- #通过xpath方式定位一组元素
- driver.find_elements_by_xpath('//*[contains(@package,"com.android.settings")]')
- #发送数据到输入框
- driver.find_element_by_accessibility_id('搜索设置').click()
- driver.find_element_by_id('android:id/search_src_text').send_keys(123)
- #获取元素的文本内容
- texts=driver.find_elements_by_class_name('android.widget.LinearLayout')
- for i in texts:
- print(i.text)
- #获取元素在屏幕上的坐标
- a=driver.find_element_by_id('com.android.settings:id/search')
- b=a.driver.location
- #获取app包名和启动名
- a1=driver.current_package
- a2=driver.current_activity
- #swip滑动事件
- driver.swipe(start_x=190,start_y=660,end_x=150,end_y=240)
- driver.swipe(start_x=190,start_y=660,end_x=150,end_y=240,duration=5000)
- scroll滑动事件
- a1=driver.find_element_by_xpath('//*[contians(@text,"安全")]')
- a2=driver.find_element_by_xpath('//*[contians(@text,"WLAN")]')
- driver.scroll(a1,a2)
- #drag拖拽事件
- a1=driver.find_element_by_xpath('//*[contians(@text,"安全")]')
- a2=driver.find_element_by_xpath('//*[contians(@text,"WLAN")]')
- driver.drag_and_drop(a1,a2)
- #应用置于后台事件
- driver.background_app()
- #手指轻敲操作
- a=driver.find_element_by_xpath('//*[contians(@text,"WLAN")]')
- TouchAction(driver).tap(a).perform()
- # 手指按操作
- a=driver.find_element_by_xpath("//*[contains(@text,'WLAN')]")
- TouchAction(driver).press(a).release().perform()
- TouchAction(driver).press(x=160,y=260).release().perform()
- # 手指长按操作
- driver.find_element_by_xpath("//*[contains(@text,'WLAN')]").click()
- a=driver.find_element_by_class_name('android.widget.RelativeLayout')
- TouchAction(driver).press(a).wait(3000).perform()
- TouchAction(driver).press(x=550,y=520).wait(4000).release().perform()
- # 手指移动操作
- a1=driver.find_element_by_xpath('//*[contains(@text,"WALN")]')
- a2=driver.find_element_by_xpath('//*[contains(@text,"安全")]')
- TouchAction(driver).press(a2).move_to(a1).release().perform()
- #应用设置手机密码
- a1=driver.find_element_by_xpath('//*[contains(@text,"WALN")]')
- a2=driver.find_element_by_xpath('//*[contains(@text,"安全")]')
- driver.drag_and_drop(a2,a1)
- a3=driver.find_element_by_xpath('//*[comtains(@text,"安全")]').click()
- driver.find_element_by_xpath('//*[contains(@text,"屏幕锁定")]')
- driver.find_element_by_xpath("//*[contains(@text,'图案')]").click()
- TouchAction(driver).press(x=245,y=965).wait(100).move_to(x=240,y=1200).wait(100).move_to(x=240,y=1500).wait(100).move_to(x=540,y=1500).release().perform()