Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions botcity/core/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,16 +493,20 @@ def find_same(item, items):
return None

haystack = self.get_screenshot()
eles = cv2find.locate_all_opencv(
element_path,
haystack_image=haystack,
region=region,
confidence=matching,
grayscale=grayscale,
# locate_all_opencv returns a generator, which is always truthy.
# Collect it so an empty result makes us look again.
eles = list(
cv2find.locate_all_opencv(
element_path,
haystack_image=haystack,
region=region,
confidence=matching,
grayscale=grayscale,
)
)
if not eles:
continue
eles = deduplicate(list(eles))
eles = deduplicate(eles)
for ele in eles:
if ele is not None:
ele = self._fix_retina_element(ele)
Expand Down
32 changes: 32 additions & 0 deletions botcity/core/tests/test_find_all_wait.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import numpy
from PIL import Image

from botcity.core import DesktopBot, cv2find


def test_find_all_keeps_looking_until_the_element_appears(tmp_path, monkeypatch):
rng = numpy.random.default_rng(0)
needle = rng.integers(0, 256, (10, 10, 3), dtype=numpy.uint8)
empty_screen = rng.integers(0, 256, (100, 200, 3), dtype=numpy.uint8)
loaded_screen = empty_screen.copy()
loaded_screen[20:30, 40:50] = needle
# The element shows up on the third screenshot, well within waiting_time.
screens = [empty_screen, empty_screen, loaded_screen]

needle_path = tmp_path / "needle.png"
Image.fromarray(needle).save(needle_path)
bot = DesktopBot()
bot.add_image("needle", str(needle_path))
monkeypatch.setattr(bot, "_fix_display_size", lambda: (200, 100))
monkeypatch.setattr(
bot,
"get_screenshot",
lambda *a, **k: Image.fromarray(
screens.pop(0) if len(screens) > 1 else screens[0]
),
)
monkeypatch.setattr("botcity.core.bot.is_retina", lambda: False)

found = list(bot.find_all("needle", matching=0.9, waiting_time=5000))

assert found == [cv2find.Box(40, 20, 10, 10)]