2016-09-30 13 views
0

我對自動化測試和ruby相當陌生。基本上,我試圖找到頁面上的元素並點擊它們。對於android來說,當我運行代碼來查找元素時,它有時可能會花費很長時間。下面是運行在尋找元素過程中的代碼示例:Appium&Ruby - 按資源ID(Android)搜索時間過長

def find_element(element_name) 
elements = nil 
result = nil 

if(is_iphone) 
    element_name.gsub("'", '\\' * 4 + "'") 
end 

# Check if element_name is present in the lookup dictionary, if present, use value instead. 
if(name_lookup(element_name, is_android == true ? "Android" : "iOS")) then 
    element_name = name_lookup(element_name, is_android == true ? "Android" : "iOS") 
end 

# Search by name or exact text. 
value = '//*[@name="' + element_name + '"]' 
elements = $driver.find_elements(:xpath, value) 
if (elements.size() > 0) 
result = elements[0] 
    return result 
end 

# Search by label. 

label = '//*[@label="' + element_name + '"]' 
elements = $driver.find_elements(:xpath, label) 
if (elements.size() > 0) 
    result = elements[0] 
    return result 
end 


if(is_android) 
    # Search by resource id (Android only). 
    elements = $driver.find_elements(:id, element_name) 
    if (elements.size() > 0) 
    result = elements[0] 
     return result 
    end 
end 

# Search for element containing the text "element_name". Uses xpath. 
# iOS searches by name, Android by text. 
is_iphone ? (xpath = '//*[contains(@name, "' + element_name + '")]') : (xpath = '//*[contains(@text, "' + element_name + '")]') 
elements = $driver.find_elements(:xpath, xpath) 
if (elements.size() > 0) 
    result = elements[0] 
    return result 
end 

return result 
end 

從本質上講,發生的事情是,通過資源ID搜索需要讓過去的登錄屏幕,並且不長任何需要 - 事實上,它需要毫秒。但是,一旦我通過登錄屏幕,搜索似乎將永遠存在。這裏是一個日誌的例子:

[HTTP] --> POST /wd/hub/session/4ee15b82-fcdb-4558-8e16-446fff65f34f/elements {"using":"id","value":"What's new"} 
[MJSONWP] Calling AppiumDriver.findElements() with args: ["id","What's new","4ee15b8... 
[debug] [BaseDriver] Waiting up to 0 ms for condition 
[debug] [AndroidBootstrap] Sending command to android {"cmd":"action","action":"find","params":{"strategy":"id","selector":"What's new","context":"","multiple":true}} 
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {"cmd":"action","action":"find","params":{"strategy":"id","selector":"What's new","context":"","multiple":true}} 
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type ACTION 
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command action: find 
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Finding 'What's new' using 'ID' with the contextId: '' multiple: true 
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Using: UiSelector[RESOURCE_ID=(**hidden**):id/What's new] 
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] getElements selector:UiSelector[RESOURCE_ID=(**hidden**):id/What's new] 
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Element[] is null: (0) 
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] getElements tmp selector:UiSelector[INSTANCE=0, RESOURCE_ID=(**hidden**):id/What's new] 
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Using: UiSelector[RESOURCE_ID=android:id/What's new] 
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] getElements selector:UiSelector[RESOURCE_ID=android:id/What's new] 
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Element[] is null: (0) 
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] getElements tmp selector:UiSelector[INSTANCE=0, RESOURCE_ID=android:id/What's new] 
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Using: UiSelector[DESCRIPTION=What's new] 
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] getElements selector:UiSelector[DESCRIPTION=What's new] 
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Element[] is null: (0) 
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] getElements tmp selector:UiSelector[DESCRIPTION=What's new, INSTANCE=0] 
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Failed to locate element. Clearing Accessibility cache and retrying. 
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Finding 'What's new' using 'ID' with the contextId: '' multiple: true 
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Using: UiSelector[RESOURCE_ID=(**hidden)**:id/What's new] 
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] getElements selector:UiSelector[RESOURCE_ID=(**hidden**):id/What's new] 
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Element[] is null: (0) 
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] getElements tmp selector:UiSelector[INSTANCE=0, RESOURCE_ID=(**hidden**):id/What's new] 
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Using: UiSelector[RESOURCE_ID=android:id/What's new] 
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] getElements selector:UiSelector[RESOURCE_ID=android:id/What's new] 
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Element[] is null: (0) 
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] getElements tmp selector:UiSelector[INSTANCE=0, RESOURCE_ID=android:id/What's new] 
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Using: UiSelector[DESCRIPTION=What's new] 
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] getElements selector:UiSelector[DESCRIPTION=What's new] 
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Element[] is null: (0) 
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] getElements tmp selector:UiSelector[DESCRIPTION=What's new, INSTANCE=0] 
[debug] [AndroidBootstrap] Received command result from bootstrap 
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result: {"status":0,"value":[]} 
[MJSONWP] Responding to client with driver.findElements() result: [] 
[HTTP] <-- POST /wd/hub/session/4ee15b82-fcdb-4558-8e16-446fff65f34f/elements 200 92230 ms - 74 

有沒有什麼辦法來加速?如果需要,將嘗試回答問題以澄清此問題。謝謝!

回答

1

只是想我會更新這個,看起來緩慢是由動畫造成的。在開發人員選項下關閉測試顯着加快了測試。

0

您似乎在使用$driver.find_elements,它可以有效地查看您的病情的所有元素,並提供所有找到的結果的完整列表。

調用$driver.find_element應該會更快,它返回第一個匹配。

+0

感謝您的回覆@Domestus。我嘗試了你提到的,但沒有奏效。它似乎沒有找到它以前的某些元素,所以我需要它是'$ driver.find_elements' – sohaib