目录
- get attribute原理分析
- 属性命名规则
get attribute原理分析
- 官方文档:http://appium.io/docs/en/commands/element/attributes/attribute/
- 与selenium不同,有大量移动端元素的属性值
- 用法:
tagName = self.driver.find_element_by_accessibility_id('SomeAccessibilityID').get_attribute('content-desc')
-
源码:image.png
- 实例
from appium import webdriver
class TestLocator:
def setup(self):
desired_caps = {}
desired_caps['platformName'] = 'Android'
# desired_caps['platformVersion'] = '6.0'
desired_caps['deviceName'] = '127.0.0.1:7555'
desired_caps['appPackage'] = 'com.xueqiu.android'
desired_caps['appActivity'] = '.main.view.MainActivity'
desired_caps['noReset'] = 'true'
# desired_caps['dontStopAppOnReset'] = "true"
desired_caps['skipDeviceInitialization'] = "true"
"""当要输入中文时需要以下两个参数"""
desired_caps['unicodeKeyBoard']='true'
desired_caps['resetKeyBoard']='true'
# 超时时间
desired_caps['adbExecTimeout'] = 500000
self.driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)
# 隐式等待
self.driver.implicitly_wait(5)
def teardown(self):
self.driver.quit()
def test_get_attr(self):
search_ele = self.driver.find_element_by_id("com.xueqiu.android:id/home_search")
print(search_ele.get_attribute("resource-id"))
print(search_ele.get_attribute("enabled"))
print(search_ele.get_attribute("clickable"))
print(search_ele.get_attribute("bounds"))
断言
- 普通断言 Assert
- Hamcrest 断言
def test_assert(self):
a = 10
b = 20
assert a > b # 第一条断言失败则不会运行后面的断言
assert "h" in "this"
Hamcrest 断言
- github地址: https://github.com/hamcrest/PyHamcrest
- hamcrest框架介绍
- Hamcrest是一个为了测试为目的,能组合成灵活表达式的匹配器类库。用于编写断言的框架,使用这个框架编写断言,提高可读性及开发测试的效率
- Hamcrest提供了大量被称为"匹配器"的方法。每个匹配器都设计用于执行
特定的比较操作。 - Hamcrest的可扩展性强,让你能够创建自定义的匹配器。
- 支持多种语言,java、python、ruby、object-c、php、erlang、swift
- 安装:
pip install pyhamcrest
from hamcrest import *
import unittest
class BiscuitTest(unittest.TestCase):
def testEquals(self):
theBiscuit = Biscuit("Ginger")
myBiscuit = Biscuit("Ginger")
assert_that(theBiscuit, equal_to(myBiscuit))
if __name__ == "__main__":
unittest.main()
- 可以在第三个参数中添加断言失败的提示语:
assert_that(10, equal_to(10), "这是一个提示")
- 例子
from hamcrest import *
def test_hamcrest():
assert_that(10, equal_to(10), "这是一个提示") # 等于
assert_that("contains some string", contains_string("string")) # 包含
assert_that(13, close_to(10, 2), "应在范围10-12内") # 在10的上下2浮动,范围为8-12
- 运行结果为:
================================== FAILURES ===================================
________________________________ test_hamcrest ________________________________
def test_hamcrest():
assert_that(10, equal_to(10), "这是一个提示")
> assert_that(13, close_to(10, 2), "应在范围10-12内")
E AssertionError: 应在范围10-12内
E Expected: a numeric value within <2> of <10>
E but: <13> differed by <3.0>
test_assert.py:18: AssertionError
=========================== short test summary info ===========================
FAILED test_assert.py::test_hamcrest - AssertionError: 应在范围10-12内
============================== 1 failed in 0.39s ==============================
Process finished with exit code 1
Assertion failed
下一节:参数化用例,使用参数化构建多条相似用例。