属性获取与断言

一,获取属性

1,tag_name 获取元素tag_name值
2,text 获取元素文本值
3,size 获取元素尺寸大小
4,location 获取元素位置
image.png

示例:

from appium import webdriver

desired_caps = {
    "platformName": "Android",
    "deviceName": "127.0.0.1:5555",  # 设备
    "appPackage": "com.tencent.wework",  # 包名
    "appActivity": "launch.LaunchSplashActivity",  # 启动页
    "resetKeyboard": "True",
    "unicodeKeyboard": "True",
    "noReset": "True"  # 不重置APP
}

driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)
driver.implicitly_wait(10)  # 隐式等待

driver.wait_activity("login.controller.LoginWxAuthActivity", 10)  # 等待主页面activity出现
ele = driver.find_element_by_id("com.tencent.wework:id/a6f")
print("tag_name:", ele.tag_name)
print("text:", ele.text)
print("size:", ele.size)
print("location:", ele.location)

driver.quit()

结果:

tag_name: None
text: 微信登录
size: {'height': 129, 'width': 978}
location: {'x': 51, 'y': 1464}
5,get_attribute
  • 获取元素的各种属性值
    示例:
from appium import webdriver

desired_caps = {
    "platformName": "Android",
    "deviceName": "127.0.0.1:5555",  # 设备
    "appPackage": "com.tencent.wework",  # 包名
    "appActivity": "launch.LaunchSplashActivity",  # 启动页
    "resetKeyboard": "True",
    "unicodeKeyboard": "True",
    "noReset": "True"  # 不重置APP
}

driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)
driver.implicitly_wait(10)  # 隐式等待

driver.wait_activity("login.controller.LoginWxAuthActivity", 10)  # 等待主页面activity出现
ele = driver.find_element_by_id("com.tencent.wework:id/a6f")
# print("tag_name:", ele.tag_name)
# print("text:", ele.text)
# print("size:", ele.size)
# print("location:", ele.location)
print("package:", ele.get_attribute("package"))
print("class:", ele.get_attribute("class"))
print("resource-id:", ele.get_attribute("resource-id"))
print("text:", ele.get_attribute("text"))
print("clickable:", ele.get_attribute("clickable"))

driver.quit()

结果:

package: com.tencent.wework
class: android.widget.Button
resource-id: com.tencent.wework:id/a6f
text: 微信登录
clickable: true

二,断言

1,assert
  • 语法
assert expression [, arguments]
  • 常用断言
    -- assert xx :判断 xx 为真
    -- assert not xx :判断 xx 不为真
    -- assert a in b :判断 b 包含 a
    -- assert a == b :判断 a 等于 b
    -- assert a != b :判断 a 不等于 b

示例:

import pytest
from appium import webdriver


class Test01(object):
    def setup(self):
        desired_caps = {
            "platformName": "Android",
            "deviceName": "127.0.0.1:5555",  # 设备
            "appPackage": "com.tencent.wework",  # 包名
            "appActivity": "launch.LaunchSplashActivity",  # 启动页
            "resetKeyboard": "True",
            "unicodeKeyboard": "True",
            "noReset": "True"  # 不重置APP
        }

        self.driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)
        self.driver.implicitly_wait(10)  # 隐式等待
        self.driver.wait_activity("login.controller.LoginWxAuthActivity", 10)  # 等待主页面activity出现

    def test_01(self):
        ele = self.driver.find_element_by_id("com.tencent.wework:id/a6f")
        expect_text = u'微信登录'
        actual_text = ele.get_attribute("text")
        assert expect_text == actual_text  # 进行断言

    def teardown(self):
        self.driver.quit()


if __name__ == '__main__':
    pytest.main(["-s", "test01.py"])
2,Hamcrest断言
  • 安装
pip install pyHamcrest

  • Hamcrest在python中提供的API:
(1)对象

-- equal_to - 匹配相等对象
-- has_length - 长度匹配 len()
-- has_property - 匹配给定名称的属性值
-- has_properties - 匹配具有所给定属性的对象
-- has_string - 匹配字符串 str()
-- instance_of - 匹配对象类型
-- none, not_none - 匹配none或not none
-- same_instance - 匹配相同的对象
-- calling, raises - 封装一个方法调用并断言它引发异常

(2)数字

-- close_to - 匹配接近给定的数字值
-- greater_than, greater_than_or_equal_to, less_than, less_than_or_equal_to - 匹配数字顺序

(3)文本

-- contains_string - 匹配部分字符串
-- ends_with - 匹配字符串的结尾
-- equal_to_ignoring_case - 匹配完整的字符串但忽略大小写
-- equal_to_ignoring_whitespace - 匹配完整的字符串,但忽略多余的空格
-- matches_regexp - 使用正则表达式匹配字符串
-- starts_with - 匹配字符串的开头
-- string_contains_in_order - 按相对顺序匹配字符串的各个部分

(4)逻辑

-- all_of - 如果所有匹配器都匹配才匹配,像Java里的&&
-- any_of - - 如果任何匹配器匹配就匹配,像Java里的||
-- anything - 匹配任何东西,如果不关心特定值则在复合匹配器中很有用
-- is_not, not_ -如果包装的匹配器不匹配器时匹配,反之亦然

(5)序列

-- contains - 完全匹配整个序列
-- contains_inanyorder - 以任何顺序匹配整个序列
-- has_item - 只要给定项目在序列中出现则匹配
-- has_items - 如果所有给定项以任意顺序出现在序列中则匹配
-- is_in - 在给定顺序中如果给定项出现则匹配
-- only_contains - 在给定顺序中如果序列的项目出现则匹配
-- empty - 如果序列为空则匹配

(6)字典

-- has_entries - 匹配具有键值对列表的字典
-- has_entry - 匹配包含键值对的字典
-- has_key - 使用键匹配字典
-- has_value - 使用值匹配字典

(7)装饰器

-- calling - 在延迟中封装一个可调用对象,在后续的调用行为中匹配
-- raises - 确保延迟调用可以按预期方式引发
-- described_as - 添加一个定制的失败表述装饰器
-- is_ - 改进可读性装饰器

  • 示例
import pytest
from appium import webdriver
from hamcrest import *   # 导入包


class Test01(object):
    def setup(self):
        desired_caps = {
            "platformName": "Android",
            "deviceName": "127.0.0.1:5555",  # 设备
            "appPackage": "com.tencent.wework",  # 包名
            "appActivity": "launch.LaunchSplashActivity",  # 启动页
            "resetKeyboard": "True",
            "unicodeKeyboard": "True",
            "noReset": "True"  # 不重置APP
        }

        self.driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)
        self.driver.implicitly_wait(10)  # 隐式等待
        self.driver.wait_activity("login.controller.LoginWxAuthActivity", 10)  # 等待主页面activity出现

    def test_01(self):
        ele = self.driver.find_element_by_id("com.tencent.wework:id/a6f")
        expect_text = u'微信登录'
        actual_text = ele.get_attribute("text")
        assert_that(actual_text, equal_to(expect_text))  # 相等
        assert_that(actual_text, contains_string(u'微信'))  # 包含
        

    def teardown(self):
        self.driver.quit()


if __name__ == '__main__':
    pytest.main(["-s", "test01.py"])

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