阿皮姆公司;Ruby-按资源ID(Android)搜索花费的时间太长

阿皮姆公司;Ruby-按资源ID(Android)搜索花费的时间太长,android,ruby,appium,Android,Ruby,Appium,我对自动化测试和ruby相当陌生。基本上,我试图在页面上找到元素并单击它们。对于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 t

我对自动化测试和ruby相当陌生。基本上,我试图在页面上找到元素并单击它们。对于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 
[HTTP]->POST/wd/hub/session/4ee15b82-fcdb-4558-8e16-446fff65f34f/elements{“使用”:“id”,“value”:“新功能”}
[MJSONWP]使用参数调用AppiumDriver.findElements():[“id”,“What's new”,“4ee15b8…”。。。
[debug][BaseDriver]等待最长为0毫秒的条件
[debug][AndroidBootstrap]将命令发送到android{“cmd”:“action”,“action”:“find”,“params”:{“strategy”:“id”,“selector”:“What's new”,“context”:“multiple”:true}
[AndroidBootstrap][BOOTSTRAP LOG][debug]从客户端获取数据:{“cmd”:“action”,“action”:“find”,“params”:{“strategy”:“id”,“selector”:“What's new”,“context”:“,“multiple”:true}”
[AndroidBootstrap][BOOTSTRAP LOG][debug]获取了类型为ACTION的命令
[AndroidBootstrap][BOOTSTRAP LOG][debug]获得命令操作:查找
[AndroidBootstrap][BOOTSTRAP LOG][debug]使用上下文ID为“”的“ID”查找“新增内容”:“多个:true”
[AndroidBootstrap][BOOTSTRAP LOG][debug]使用:UiSelector[RESOURCE_ID=(**隐藏**):ID/What's new]
[AndroidBootstrap][BOOTSTRAP LOG][debug]getElements选择器:UiSelector[RESOURCE_ID=(**隐藏**):ID/What's new]
[AndroidBootstrap][BOOTSTRAP LOG][debug]元素[]为空:(0)
[AndroidBootstrap][BOOTSTRAP LOG][debug]getElements tmp选择器:UiSelector[INSTANCE=0,RESOURCE_ID=(**hidden**):ID/What's new]
[AndroidBootstrap][BOOTSTRAP LOG][debug]使用:UiSelector[RESOURCE_ID=android:ID/What's new]
[AndroidBootstrap][BOOTSTRAP LOG][debug]getElements选择器:UiSelector[RESOURCE_ID=android:ID/What's new]
[AndroidBootstrap][BOOTSTRAP LOG][debug]元素[]为空:(0)
[AndroidBootstrap][BOOTSTRAP LOG][debug]getElements tmp选择器:UiSelector[INSTANCE=0,RESOURCE\u ID=android:ID/What's new]
[AndroidBootstrap][BOOTSTRAP LOG][debug]使用:UiSelector[DESCRIPTION=What's new]
[AndroidBootstrap][BOOTSTRAP LOG][debug]getElements选择器:UiSelector[DESCRIPTION=What's new]
[AndroidBootstrap][BOOTSTRAP LOG][debug]元素[]为空:(0)
[AndroidBootstrap][BOOTSTRAP LOG][debug]getElements tmp选择器:UiSelector[DESCRIPTION=What's new,INSTANCE=0]
[AndroidBootstrap][BOOTSTRAP LOG][debug]未能找到元素。正在清除可访问性缓存并重试。
[AndroidBootstrap][BOOTSTRAP LOG][debug]使用上下文ID为“”的“ID”查找“新增内容”:“多个:true”
[AndroidBootstrap][BOOTSTRAP LOG][debug]使用:UiSelector[RESOURCE_ID=(**隐藏)**:ID/What's new]
[AndroidBootstrap][BOOTSTRAP LOG][debug]getElements选择器:UiSelector[RESOURCE_ID=(**隐藏**):ID/What's new]
[AndroidBootstrap][BOOTSTRAP LOG][debug]元素[]为空:(0)
[AndroidBootstrap][BOOTSTRAP LOG][debug]getElements tmp选择器:UiSelector[INSTANCE=0,RESOURCE_ID=(**hidden**):ID/What's new]
[AndroidBootstrap][BOOTSTRAP LOG][debug]使用:UiSelector[RESOURCE_ID=android:ID/What's new]
[AndroidBootstrap][BOOTSTRAP LOG][debug]getElements选择器:UiSelector[RESOURCE_ID=android:ID/What's new]
[AndroidBootstrap][BOOTSTRAP LOG][debug]元素[]为空:(0)
[AndroidBootstrap][BOOTSTRAP LOG][debug]getElements tmp选择器:UiSelector[INSTANCE=0,RESOURCE\u ID=android:ID/What's new]
[AndroidBootstrap][BOOTSTRAP LOG][debug]使用:UiSelector[DESCRIPTION=What's new]
[AndroidBootstrap][BOOTSTRAP LOG][debug]getElements选择器:UiSelector[DESCRIPTION=What's new]
[AndroidBootstrap][BOOTSTRAP LOG][debug]元素[]为空:(0)
[AndroidBootstrap][BOOTSTRAP LOG][debug]getElements tmp选择器:UiSelector[DESCRIPTION=What's new,INSTANCE=0]
[debug][AndroidBootstrap]从引导程序接收到命令结果
[AndroidBootstrap][BOOTSTRAP LOG][debug]返回结果:{“状态”:0,“值”:[]}
[MJSONWP]使用驱动程序响应客户端。findElements()结果:[]

[HTTP]您似乎正在使用
$driver.find_elements
,它有效地查找您的病情的所有元素,并给出所有找到的结果的完整列表


调用
$driver.find_element
会更快,它会返回第一个匹配项。

您似乎正在使用
$driver.find_elements
,它会有效地查找您的状况的所有元素,并给出所有找到的结果的完整列表


调用
$driver.find_element
,它将返回第一个匹配项应该会更快。

我刚想更新这个,似乎是动画导致了速度慢。在“开发人员选项”下关闭这个选项会显著加快测试速度。

我刚想更新这个,似乎是动画导致了速度慢。打开在开发者选项下关闭它可以显著加快测试速度。

谢谢你的回复@Domestus。我尝试了你提到的内容,但没有成功。它似乎没有找到以前可能找到的某些元素,所以我需要它成为
$driver。查找元素
谢谢你的回复@Domestus。我尝试了你提到的内容,但没有成功rk.它似乎找不到以前可能找到的某些元素,所以我需要它是
$driver.find_elements