Appium & Ruby - 按资源 ID (Android) 搜索花费的时间太长
Appium & Ruby - Searching by resource ID (Android) is taking too long
我对自动化测试还很陌生,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
有什么办法可以加快速度吗?如果需要,将尝试回答问题以澄清这一点。谢谢!
您似乎在使用 $driver.find_elements
,它可以有效地查看您的条件的所有元素,并给出所有找到的结果的完整列表。
调用 $driver.find_element
应该会更快,returns 首先匹配。
只是想我会更新这个,看起来缓慢是由动画引起的。在开发人员选项下关闭它可以显着加快测试速度。
我对自动化测试还很陌生,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
有什么办法可以加快速度吗?如果需要,将尝试回答问题以澄清这一点。谢谢!
您似乎在使用 $driver.find_elements
,它可以有效地查看您的条件的所有元素,并给出所有找到的结果的完整列表。
调用 $driver.find_element
应该会更快,returns 首先匹配。
只是想我会更新这个,看起来缓慢是由动画引起的。在开发人员选项下关闭它可以显着加快测试速度。