天天看點

【Python自動化測試】Appium get_attribute使用方法

  • 官方文檔
  • 文檔不詳細,可以下載下傳源代碼(以安卓為例)
  • 用AndroidStudio打開,可以檢視可以過去的所有屬性值
    【Python自動化測試】Appium get_attribute使用方法
CHECKABLE(new String[]{"checkable"}),
CHECKED(new String[]{"checked"}),
CLASS(new String[]{"class", "className"}),
CLICKABLE(new String[]{"clickable"}),
CONTENT_DESC(new String[]{"content-desc", "contentDescription"}),
ENABLED(new String[]{"enabled"}),
FOCUSABLE(new String[]{"focusable"}),
FOCUSED(new String[]{"focused"}),
LONG_CLICKABLE(new String[]{"long-clickable", "longClickable"}),
PACKAGE(new String[]{"package"}),
PASSWORD(new String[]{"password"}),
RESOURCE_ID(new String[]{"resource-id", "resourceId"}),
SCROLLABLE(new String[]{"scrollable"}),
SELECTION_START(new String[]{"selection-start"}),
SELECTION_END(new String[]{"selection-end"}),
SELECTED(new String[]{"selected"}),
TEXT(new String[]{"text", "name"}),
// The main difference of this attribute from the preceding one is that
// it does not replace null values with empty strings
ORIGINAL_TEXT(new String[]{"original-text"}, false, false),
BOUNDS(new String[]{"bounds"}),
INDEX(new String[]{"index"}, false, true),
DISPLAYED(new String[]{"displayed"}),
CONTENT_SIZE(new String[]{"contentSize"}, true, false);
           
  • 與selenium的get_attributed的不同在于有大量的移動端的屬性值

基本使用執行個體:

from appium import webdriver
class TestGetAttr:
    def setup(self):
        desire_cap = {
            "platformName": "Android",
            "deviceName": "mvq8aaw4ca5tcamn",
            "appActivity": ".view.WelcomeActivityAlias",
            "appPackage": "com.xueqiu.android",
            "noReset": True
        }
        self.driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desire_cap)
        self.driver.implicitly_wait(10)

    def teardown(self):
        pass
        
    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("content-desc"))
        print(search_ele.get_attribute("bounds"))
        print(search_ele.get_attribute("clickable"))
        assert 'search' in search_ele.get_attribute("resource-id")
           

注意,在使用get_attribute方法之前需要執行個體化一個元素對象!