Skip to content

Commit 7c67183

Browse files
add tests for double-click using bid and coords
1 parent 8397de7 commit 7c67183

File tree

2 files changed

+123
-2
lines changed

2 files changed

+123
-2
lines changed

tests/core/data/dblclick.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<body>
4+
<button id="dblclick-counter" ondblclick="incrementCounter()">
5+
Double-click me! Count: <span id="count">0</span>
6+
</button>
7+
8+
<script>
9+
let count = 0;
10+
function incrementCounter() {
11+
count++;
12+
document.getElementById('count').textContent = count;
13+
}
14+
</script>
15+
</body>
16+
</html>

tests/core/test_actions_highlevel.py

Lines changed: 107 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
TEXTBOX_URL = f"file://{__DATA_DIR}/textbox.html"
2828
EXAMPLE_URL = f"file://{__DATA_DIR}/example.html"
2929
HOVER_URL = f"file://{__DATA_DIR}/hover.html"
30+
DBLCLICK_URL = f"file://{__DATA_DIR}/dblclick.html"
3031
INEXISTANT_FILE_URL = f"file://{__DATA_DIR}/no_file_here.html"
3132
LONG_PAGE_URL = f"file://{__DATA_DIR}/long_page.html"
3233
TEXT_INPUT_URL = f"file://{__DATA_DIR}/input_type/text_input.html"
@@ -826,9 +827,113 @@ def get_fname_lname_elems(obs):
826827
env.close()
827828

828829

829-
@pytest.mark.skip(reason="Not implemented yet")
830830
def test_dblclick():
831-
pass
831+
action_set = HighLevelActionSet(subsets=["bid"])
832+
833+
env = gym.make(
834+
"browsergym/openended",
835+
task_kwargs={"start_url": DBLCLICK_URL},
836+
headless=__HEADLESS,
837+
slow_mo=__SLOW_MO,
838+
timeout=__TIMEOUT,
839+
action_mapping=action_set.to_python_code,
840+
)
841+
842+
def get_counter_elem(obs):
843+
soup = bs4.BeautifulSoup(flatten_dom_to_str(obs["dom_object"]), "lxml")
844+
counter = soup.find("span", attrs={"id": "count"})
845+
return counter
846+
847+
obs, info = env.reset()
848+
counter = get_counter_elem(obs)
849+
850+
# counter starts at 0
851+
assert not obs["last_action_error"]
852+
assert counter.text.strip() == "0"
853+
854+
# test dblclick using bid
855+
counter_button = counter.find_parent("button", attrs={"id": "dblclick-counter"})
856+
action = f"""
857+
dblclick({repr(counter_button.get(BID_ATTR))})
858+
"""
859+
860+
obs, reward, terminated, truncated, info = env.step(action)
861+
counter = get_counter_elem(obs)
862+
863+
# counter incremented to 1
864+
assert not obs["last_action_error"]
865+
assert counter.text.strip() == "1"
866+
867+
# test dblclick using bid again
868+
counter_button = counter.find_parent("button", attrs={"id": "dblclick-counter"})
869+
action = f"""
870+
dblclick({repr(counter_button.get(BID_ATTR))})
871+
"""
872+
873+
obs, reward, terminated, truncated, info = env.step(action)
874+
counter = get_counter_elem(obs)
875+
876+
# counter incremented to 2
877+
assert not obs["last_action_error"]
878+
assert counter.text.strip() == "2"
879+
880+
env.close()
881+
882+
883+
def test_mouse_dblclick():
884+
action_set = HighLevelActionSet(subsets=["coord"])
885+
886+
env = gym.make(
887+
"browsergym/openended",
888+
task_kwargs={"start_url": DBLCLICK_URL},
889+
headless=__HEADLESS,
890+
slow_mo=__SLOW_MO,
891+
timeout=__TIMEOUT,
892+
action_mapping=action_set.to_python_code,
893+
)
894+
895+
def counter():
896+
return env.unwrapped.page.inner_text("#count")
897+
898+
def get_counter_coords():
899+
button = env.unwrapped.page.locator("button")
900+
box = button.bounding_box()
901+
center_x = box["x"] + box["width"] / 2
902+
center_y = box["y"] + box["height"] / 2
903+
return center_x, center_y
904+
905+
906+
obs, info = env.reset()
907+
908+
# counter starts at 0 assert not obs["last_action_error"]
909+
assert counter() == "0"
910+
911+
# test mouse_dblclick using coordinates
912+
counter_x, counter_y = get_counter_coords()
913+
action = f"""
914+
mouse_dblclick({counter_x}, {counter_y})
915+
"""
916+
917+
obs, reward, terminated, truncated, info = env.step(action)
918+
919+
920+
# counter incremented to 1
921+
assert not obs["last_action_error"]
922+
assert counter() == "1"
923+
924+
counter_x, counter_y = get_counter_coords()
925+
926+
action = f"""
927+
mouse_dblclick({counter_x}, {counter_y})
928+
"""
929+
930+
obs, reward, terminated, truncated, info = env.step(action)
931+
932+
# counter incremented to 2
933+
assert not obs["last_action_error"]
934+
assert counter() == "2"
935+
936+
env.close()
832937

833938

834939
# copy/paste text using a sequence of keyboard_press actions

0 commit comments

Comments
 (0)