Common code patterns#
See also Frequently Asked Questions.
Performance measurements#
wait_until#
stbt.wait_until will wait until any condition specified by you:
keypress = stbt.press("KEY_HOME")
assert stbt.wait_until(lambda: XXX) # Replace "XXX" with your custom expression
end_time = time.time()
duration = end_time - keypress.end_time
If you are waiting for a Page Object to become visible, then you can use the Page Object’s frame timestamp:
keypress = stbt.press("KEY_HOME")
page = stbt.wait_until(Home) # "Home" is a stbt.FrameObject class
assert page
duration = page._frame.time - keypress.end_time
Logging#
Log the measurement to stbt.log:
print(f"Measurement of XXX: {duration:.2f} seconds")
Or at the end of the test write all your measurements to a json file:
measurements = {
"XXX": duration,
}
with open("measurements.json", "w") as f:
json.dump(measurements, f)
press_and_wait#
stbt.press_and_wait uses motion detection: It presses a key, then waits for the screen to change, then waits for it to stop changing.
transition = stbt.press_and_wait("KEY_HOME")
assert transition
print(f"Measurement of Home page load: {transition.duration:.2f} seconds")
You may need to provide a mask parameter to press_and_wait to ignore some
areas of the screen. See Regions and Masks.
You should also check that the Home screen loaded successfully, to ensure the
measurement is valid. For example if Home is a FrameObject class:
transition = stbt.press_and_wait("KEY_HOME")
assert transition
page = Home(transition.frame)
assert page
print(f"Measurement of Home page load: {transition.duration:.2f} seconds")