Skip to content

Commit

Permalink
Observation feature hide_bid_if_invisible (#43)
Browse files Browse the repository at this point in the history
* hide_bid_if_invisible feature

* version bump 0.3.3

* ci tests
  • Loading branch information
gasse authored May 24, 2024
1 parent 15a648d commit d897d45
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 15 deletions.
2 changes: 1 addition & 1 deletion core/src/browsergym/core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.3.3dev"
__version__ = "0.3.3"

import playwright.sync_api

Expand Down
18 changes: 16 additions & 2 deletions core/src/browsergym/utils/obs.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def flatten_dom_to_str(
filter_with_bid_only: bool = False,
filter_som_only: bool = False,
coord_decimals: int = 0,
hide_bid_if_invisible: int = False,
) -> str:
"""Formats a DOM snapshot into a string text"""

Expand Down Expand Up @@ -131,7 +132,13 @@ def dfs(node_idx: int, parent_node_skipped: bool) -> str:
attributes = extra_attributes_to_print + attributes

# insert bid as first attribute
if bid is not None:
if not (
bid is None
or (
hide_bid_if_invisible
and extra_properties.get(bid, {}).get("visibility", 0) < 0.5
)
):
attributes.insert(0, f'bid="{bid}"')

if not skip_node:
Expand Down Expand Up @@ -293,6 +300,7 @@ def flatten_axtree_to_str(
ignored_roles=IGNORED_AXTREE_ROLES,
ignored_properties=IGNORED_AXTREE_PROPERTIES,
remove_redundant_static_text: bool = True,
hide_bid_if_invisible: bool = False,
) -> str:
"""Formats the accessibility tree into a string text"""
node_id_to_idx = {}
Expand Down Expand Up @@ -372,7 +380,13 @@ def dfs(node_idx: int, depth: int, parent_node_filtered: bool) -> str:
if not skip_node:
node_str = f"{node_role} {repr(node_name.strip())}"

if bid is not None:
if not (
bid is None
or (
hide_bid_if_invisible
and extra_properties.get(bid, {}).get("visibility", 0) < 0.5
)
):
node_str = f"[{bid}] " + node_str

if node_value is not None:
Expand Down
33 changes: 29 additions & 4 deletions core/tests/test_observation.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,17 @@ def test_dom_to_text():
assert "Text within in non-html tag" in dom
assert "Text that should not be visible" not in dom

dom = flatten_dom_to_str(
obs["dom_object"],
extra_properties=obs["extra_element_properties"],
hide_bid_if_invisible=True,
)
assert "<title" in dom
assert "<title bid=" not in dom
assert 'type="submit" value="Submit"' in dom
assert "Text within in non-html tag" in dom
assert "Text that should not be visible" in dom

dom = flatten_dom_to_str(
obs["dom_object"],
extra_properties=obs["extra_element_properties"],
Expand Down Expand Up @@ -449,16 +460,17 @@ def test_axtree_to_text():
assert 'box="(' in axtree
assert 'center="(' in axtree
assert ", clickable, visible, som" in axtree
assert "heading 'Simple Form', box=\"(" in axtree
assert "textbox 'Email:' value='[email protected]'" in axtree
assert "] heading 'Simple Form', box=\"(" in axtree
assert "] textbox 'Email:' value='[email protected]'" in axtree
assert "Text within in non-html tag" in axtree
assert "Text that should not be visible" in axtree
assert "] paragraph" in axtree

axtree = flatten_axtree_to_str(
obs["axtree_object"], extra_properties=obs["extra_element_properties"], filter_som_only=True
)
assert "LabelText" not in axtree
assert "button 'Submit'" in axtree
assert "] button 'Submit'" in axtree
assert "Text within in non-html tag" not in axtree
assert "Text that should not be visible" not in axtree

Expand All @@ -468,9 +480,22 @@ def test_axtree_to_text():
filter_visible_only=True,
)
assert "RootWebArea" in axtree
assert "button 'Submit'" in axtree
assert "] button 'Submit'" in axtree
assert "Text within in non-html tag" in axtree
assert "Text that should not be visible" not in axtree
assert "] paragraph" not in axtree

axtree = flatten_axtree_to_str(
obs["axtree_object"],
extra_properties=obs["extra_element_properties"],
hide_bid_if_invisible=True,
)
assert "RootWebArea" in axtree
assert "] button 'Submit'" in axtree
assert "Text within in non-html tag" in axtree
assert "Text that should not be visible" in axtree
assert "] paragraph '" not in axtree
assert "paragraph '" in axtree

axtree = flatten_axtree_to_str(
obs["axtree_object"],
Expand Down
2 changes: 1 addition & 1 deletion experiments/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
browsergym-core==0.3.3dev
browsergym-core==0.3.3
tiktoken>=0.4
2 changes: 1 addition & 1 deletion miniwob/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
browsergym-core==0.3.3dev
browsergym-core==0.3.3
10 changes: 5 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ classifiers = [
"Topic :: Scientific/Engineering :: Artificial Intelligence",
"License :: OSI Approved :: Apache Software License",
]
version="0.3.3dev"
version="0.3.3"
dependencies = [
"browsergym-core==0.3.3dev",
"browsergym-miniwob==0.3.3dev",
"browsergym-webarena==0.3.3dev",
"browsergym-experiments==0.3.3dev",
"browsergym-core==0.3.3",
"browsergym-miniwob==0.3.3",
"browsergym-webarena==0.3.3",
"browsergym-experiments==0.3.3",
"browsergym-workarena",
]

Expand Down
2 changes: 1 addition & 1 deletion webarena/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
browsergym-core==0.3.3dev
browsergym-core==0.3.3
libwebarena==0.0.3

0 comments on commit d897d45

Please sign in to comment.