十、Appium属性获取与断言

目录

  • get attribute原理分析
  • 属性命名规则

get attribute原理分析

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

下一节:参数化用例,使用参数化构建多条相似用例。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容