|
27 | 27 | TEXTBOX_URL = f"file://{__DATA_DIR}/textbox.html" |
28 | 28 | EXAMPLE_URL = f"file://{__DATA_DIR}/example.html" |
29 | 29 | HOVER_URL = f"file://{__DATA_DIR}/hover.html" |
| 30 | +DBLCLICK_URL = f"file://{__DATA_DIR}/dblclick.html" |
30 | 31 | INEXISTANT_FILE_URL = f"file://{__DATA_DIR}/no_file_here.html" |
31 | 32 | LONG_PAGE_URL = f"file://{__DATA_DIR}/long_page.html" |
32 | 33 | TEXT_INPUT_URL = f"file://{__DATA_DIR}/input_type/text_input.html" |
@@ -826,9 +827,113 @@ def get_fname_lname_elems(obs): |
826 | 827 | env.close() |
827 | 828 |
|
828 | 829 |
|
829 | | -@pytest.mark.skip(reason="Not implemented yet") |
830 | 830 | 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() |
832 | 937 |
|
833 | 938 |
|
834 | 939 | # copy/paste text using a sequence of keyboard_press actions |
|
0 commit comments