Skip to content
Merged
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
2 changes: 1 addition & 1 deletion browsergym/core/src/browsergym/core/action/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def dblclick(
add_demo_mode_effects(page, elem, bid, demo_mode=demo_mode, move_cursor=True)

def do(force: bool):
elem.click(button=button, modifiers=modifiers, force=force, timeout=500)
elem.dblclick(button=button, modifiers=modifiers, force=force, timeout=500)

call_fun(do, retry_with_force)

Expand Down
16 changes: 16 additions & 0 deletions tests/core/data/dblclick.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<body>
<button id="dblclick-counter" ondblclick="incrementCounter()">
Double-click me! Count: <span id="count">0</span>
</button>

<script>
let count = 0;
function incrementCounter() {
count++;
document.getElementById('count').textContent = count;
}
</script>
</body>
</html>
107 changes: 105 additions & 2 deletions tests/core/test_actions_highlevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
TEXTBOX_URL = f"file://{__DATA_DIR}/textbox.html"
EXAMPLE_URL = f"file://{__DATA_DIR}/example.html"
HOVER_URL = f"file://{__DATA_DIR}/hover.html"
DBLCLICK_URL = f"file://{__DATA_DIR}/dblclick.html"
INEXISTANT_FILE_URL = f"file://{__DATA_DIR}/no_file_here.html"
LONG_PAGE_URL = f"file://{__DATA_DIR}/long_page.html"
TEXT_INPUT_URL = f"file://{__DATA_DIR}/input_type/text_input.html"
Expand Down Expand Up @@ -826,9 +827,111 @@ def get_fname_lname_elems(obs):
env.close()


@pytest.mark.skip(reason="Not implemented yet")
def test_dblclick():
pass
action_set = HighLevelActionSet(subsets=["bid"])

env = gym.make(
"browsergym/openended",
task_kwargs={"start_url": DBLCLICK_URL},
headless=__HEADLESS,
slow_mo=__SLOW_MO,
timeout=__TIMEOUT,
action_mapping=action_set.to_python_code,
)

def get_counter_elem(obs):
soup = bs4.BeautifulSoup(flatten_dom_to_str(obs["dom_object"]), "lxml")
counter = soup.find("span", attrs={"id": "count"})
return counter

obs, info = env.reset()
counter = get_counter_elem(obs)

# counter starts at 0
assert not obs["last_action_error"]
assert counter.text.strip() == "0"

# test dblclick using bid
counter_button = counter.find_parent("button", attrs={"id": "dblclick-counter"})
action = f"""
dblclick({repr(counter_button.get(BID_ATTR))})
"""

obs, reward, terminated, truncated, info = env.step(action)
counter = get_counter_elem(obs)

# counter incremented to 1
assert not obs["last_action_error"]
assert counter.text.strip() == "1"

# test dblclick using bid again
counter_button = counter.find_parent("button", attrs={"id": "dblclick-counter"})
action = f"""
dblclick({repr(counter_button.get(BID_ATTR))})
"""

obs, reward, terminated, truncated, info = env.step(action)
counter = get_counter_elem(obs)

# counter incremented to 2
assert not obs["last_action_error"]
assert counter.text.strip() == "2"

env.close()


def test_mouse_dblclick():
action_set = HighLevelActionSet(subsets=["coord"])

env = gym.make(
"browsergym/openended",
task_kwargs={"start_url": DBLCLICK_URL},
headless=__HEADLESS,
slow_mo=__SLOW_MO,
timeout=__TIMEOUT,
action_mapping=action_set.to_python_code,
)

def counter():
return env.unwrapped.page.inner_text("#count")

def get_counter_coords():
button = env.unwrapped.page.locator("button")
box = button.bounding_box()
center_x = box["x"] + box["width"] / 2
center_y = box["y"] + box["height"] / 2
return center_x, center_y

obs, info = env.reset()

# counter starts at 0 assert not obs["last_action_error"]
assert counter() == "0"

# test mouse_dblclick using coordinates
counter_x, counter_y = get_counter_coords()
action = f"""
mouse_dblclick({counter_x}, {counter_y})
"""

obs, reward, terminated, truncated, info = env.step(action)

# counter incremented to 1
assert not obs["last_action_error"]
assert counter() == "1"

counter_x, counter_y = get_counter_coords()

action = f"""
mouse_dblclick({counter_x}, {counter_y})
"""

obs, reward, terminated, truncated, info = env.step(action)

# counter incremented to 2
assert not obs["last_action_error"]
assert counter() == "2"

env.close()


# copy/paste text using a sequence of keyboard_press actions
Expand Down
Loading