appium(一)
1、安装
1)安装步骤略
2)安装网易mumu模拟器
3)获取appPackage和appActivity
命令:adb logcat | grep -i displayed
4)获取appPackage和appActivity
获取当前界面元素:adb shell dumpsys activity top
5)获取网易mumu的devices
注:
win adb connect 127.0.0.1:7555
mac 重启adb,然后直接获取devices
2、面板介绍
-
- 略
3、原理介绍
-
- 略
4、demo
1 from appium import webdriver 2 3 desired_caps = {} 4 desired_caps['platformName'] = 'Android' 5 desired_caps['platformVersion'] = '6.0' 6 desired_caps['deviceName'] = 'emulator-5554' 7 desired_caps['appPackage'] = 'com.xueqiu.android' 8 desired_caps['appActivity'] = '.common.MainActivity' 9 desired_caps['automationName'] = 'UiAutomator1' 10 desired_caps['noReset'] = True 11 12 driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) 13 driver.implicitly_wait(10) 14 15 el1 = driver.find_element_by_id("com.xueqiu.android:id/tv_search") 16 el1.click() 17 el2 = driver.find_element_by_id("com.xueqiu.android:id/search_input_text") 18 el2.send_keys("alibaba") 19 driver.press_keycode(66) 20 21 driver.quit()
5、定位方式介绍
which uiautomatorviewer
6、元素的常用方法
点击方法 element.click()
输入操作 element.send_keys("appium")
设置元素的值 element.set_value("appium")
清除操作 element.clear()
是否可见 element.is_displayed() 返回true
是否可用 element.is_enabled()
是否被选中 element.is_selected()
获取属性值 get_attribute(name)
7、等待的三种方法
1) 强制等待:sleep
2) 隐式等待:服务器端等待
driver.implicitly.wait(TIMEOUT)
3) 显式等待:客户端等待
WebDriverWait(self.driver,10).util(expected_conditions.visibility_of_element_located(LOCATOR))
8、属性
官方文档地址:http://appium.io/docs/en/commands/element/attributes/attribute/
def test_getattr(self): sleep(3) search_ele = self.driver.find_element_by_id("com.xueqiu.android:id/tv_search") sleep(3) print(search_ele.text) print(search_ele.get_attribute("enabled")) print(search_ele.get_attribute("clickable"))
9、断言
1) assert 断言
2)hamcrest 断言
导包:
from hamcrest import *
Git地址:https://github.com/hamcrest/PyHamcrest
assert_that(10,equal_to(10),'这是一个提示') # 接近 assert_that(12,close_to(10,2)) # 前面字符串包含后面字符串 assert_that("contains some string",contains_string("string"))
10、参数化
1 from appium import webdriver 2 import pytest 3 from hamcrest import assert_that, close_to 4 from appium.webdriver.common.mobileby import MobileBy 5 6 7 class TestDW(): 8 def setup(self): 9 desired_caps = {} 10 desired_caps['platformName'] = 'Android' 11 desired_caps['platformVersion'] = '6.0' 12 desired_caps['deviceName'] = 'emulator-5554' 13 desired_caps['appPackage'] = 'com.xueqiu.android' 14 desired_caps['appActivity'] = '.common.MainActivity' 15 desired_caps['automationName'] = 'UiAutomator1' 16 desired_caps['noReset'] = 'true' 17 # desired_caps['dontStopAppOnReset'] = 'true' 18 desired_caps['skipDeviceInitialization'] = 'true' 19 desired_caps['unicodeKeyBoard'] = 'true' 20 desired_caps['resetKeyBoard'] = 'true' 21 self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) 22 self.driver.implicitly_wait(10) 23 24 def teardown(self): 25 # self.driver.quit() 26 self.driver.find_element(MobileBy.ID,'com.xueqiu.android:id/action_close') 27 pass 28 29 # 参数化 30 @pytest.mark.parametrize('searchkey, type, expect_price',[ 31 ('alibab','BABA',180), 32 ('xiaomi','01810',10) 33 ]) 34 def test_param(self,searchkey,type,expect_price): 35 ''' 36 1. 打开雪球 37 2. 点击搜索 38 3. 输入alibab 39 4. 点击第一个搜索结果 40 5. 判断价格 41 :return: 42 ''' 43 self.driver.find_element(MobileBy.ID,"com.xueqiu.android:id/tv_search").click() 44 self.driver.find_element(MobileBy.ID,"com.xueqiu.android:id/search_input_text").send_keys(searchkey) 45 self.driver.find_element(MobileBy.ID,"com.xueqiu.android:id/name").click() 46 price_element = self.driver.find_element(MobileBy.XPATH,f"//*[@text={type}]/../../..//*[@resource-id='com.android:id/currence_price']").click() 47 current_price = float(price_element.text) 48 assert_that(current_price, close_to(expect_price,expect_price*0.1))