Skip to content

Commit 6c662c2

Browse files
Fix bug in double click (#370)
* fix bug in double click Currently double click calls click, instead of double click. * add tests for double-click using bid and coords --------- Co-authored-by: Aman Jaiswal <[email protected]>
1 parent a59ccb6 commit 6c662c2

File tree

3 files changed

+122
-3
lines changed

3 files changed

+122
-3
lines changed

browsergym/core/src/browsergym/core/action/functions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ def dblclick(
183183
add_demo_mode_effects(page, elem, bid, demo_mode=demo_mode, move_cursor=True)
184184

185185
def do(force: bool):
186-
elem.click(button=button, modifiers=modifiers, force=force, timeout=500)
186+
elem.dblclick(button=button, modifiers=modifiers, force=force, timeout=500)
187187

188188
call_fun(do, retry_with_force)
189189

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: 105 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,111 @@ 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+
obs, info = env.reset()
906+
907+
# counter starts at 0 assert not obs["last_action_error"]
908+
assert counter() == "0"
909+
910+
# test mouse_dblclick using coordinates
911+
counter_x, counter_y = get_counter_coords()
912+
action = f"""
913+
mouse_dblclick({counter_x}, {counter_y})
914+
"""
915+
916+
obs, reward, terminated, truncated, info = env.step(action)
917+
918+
# counter incremented to 1
919+
assert not obs["last_action_error"]
920+
assert counter() == "1"
921+
922+
counter_x, counter_y = get_counter_coords()
923+
924+
action = f"""
925+
mouse_dblclick({counter_x}, {counter_y})
926+
"""
927+
928+
obs, reward, terminated, truncated, info = env.step(action)
929+
930+
# counter incremented to 2
931+
assert not obs["last_action_error"]
932+
assert counter() == "2"
933+
934+
env.close()
832935

833936

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

0 commit comments

Comments
 (0)