File size: 2,037 Bytes
dd39c08
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import os
import gymnasium as gym
import re
import pytest

# register gym environments
import browsergym.miniwob

__SLOW_MO = 1000 if "DISPLAY_BROWSER" in os.environ else None
__HEADLESS = False if "DISPLAY_BROWSER" in os.environ else True


@pytest.mark.parametrize("seed", range(5))
def test_cheat(seed):
    env = gym.make(
        "browsergym/miniwob.click-menu-2",
        headless=__HEADLESS,
        slow_mo=__SLOW_MO,
        action_mapping=None,
    )
    obs, info = env.reset(seed=seed)

    assert obs["last_action_error"] == ""

    match1 = re.match(
        'Click the "Menu" button, and then find and click on the item labeled "(.+)".', obs["goal"]
    )
    match2 = re.match(
        'Click the "Menu" button, and then find and click on the item with the "(.+)" icon.',
        obs["goal"],
    )

    assert match1 or match2

    if match1:
        item_label = match1.groups()[0]
        item_classname = {
            "Save": "ui-icon-disk",
            "Prev": "ui-icon-seek-start",
            "Stop": "ui-icon-stop",
            "Play": "ui-icon-play",
            "Next": "ui-icon-seek-end",
            "Zoom In": "ui-icon-zoomin",
            "Zoom Out": "ui-icon-zoomout",
        }[item_label]
    else:
        item_classname = match2.groups()[0]

    action = f"""\
page.get_by_text("Menu").click()
"""

    obs, reward, term, trunc, info = env.step(action)

    assert obs["last_action_error"] == ""
    assert reward == 0
    assert term == False

    if item_classname in ("ui-icon-seek-start", "ui-icon-stop", "ui-icon-play", "ui-icon-seek-end"):

        action = f"""\
page.get_by_text("Playback").click()
"""

        obs, reward, term, trunc, info = env.step(action)

        assert obs["last_action_error"] == ""
        assert reward == 0
        assert term == False

    action = f"""\
page.locator(".{item_classname}").click()
"""

    obs, reward, term, trunc, info = env.step(action)

    assert obs["last_action_error"] == ""
    assert reward == 1
    assert term == True

    env.close()