Skip to content

Commit

Permalink
stateful_browser.py: add page, url, and form properties
Browse files Browse the repository at this point in the history
Closes #175.

The most convenient way to access the page, url, and form objects
is to use properties, which are mapped to the appropriate getter
functions using `property`.

* get_current_page -> page property

  The previous method name was simply too long for such a commonly
  used function. The "current" adjective added no extra specificity.

* get_current_form -> form property

  This is changed for clarity. What is the "current" form if there
  are multiple forms on the page? Instead, we make it clearer by
  describing it as the "selected" form.

* get_url -> url property

  This is just an elimination of the get_ prefix and use a property.

For backwards compatibility, the older method names remain as aliases.
  • Loading branch information
hemberger authored and moy committed Aug 14, 2019
1 parent 21c2849 commit 790b118
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
21 changes: 15 additions & 6 deletions mechanicalsoup/stateful_browser.py
Expand Up @@ -60,6 +60,12 @@ def __init__(self, *args, **kwargs):
self.__verbose = 0
self.__state = _BrowserState()

# Aliases for backwards compatibility
# (Included specifically in __init__ to suppress them in Sphinx docs)
self.get_current_page = lambda: self.page
self.get_current_form = lambda: self.form
self.get_url = lambda: self.url

def set_debug(self, debug):
"""Set the debug mode (off by default).
Expand All @@ -86,11 +92,18 @@ def get_verbose(self):
"""Get the verbosity level. See :func:`set_verbose()`."""
return self.__verbose

def get_url(self):
@property
def page(self):
"""Get the current page as a soup object."""
return self.__state.page

@property
def url(self):
"""Get the URL of the currently visited page."""
return self.__state.url

def get_current_form(self):
@property
def form(self):
"""Get the currently selected form as a :class:`Form` object.
See :func:`select_form`.
"""
Expand All @@ -106,10 +119,6 @@ def new_control(self, type, name, value, **kwargs):
"""Call :func:`Form.new_control` on the currently selected form."""
return self.get_current_form().new_control(type, name, value, **kwargs)

def get_current_page(self):
"""Get the current page as a soup object."""
return self.__state.page

def absolute_url(self, url):
"""Return the absolute URL made from the current URL and ``url``.
The current URL is only used to provide any missing components of
Expand Down
13 changes: 13 additions & 0 deletions tests/test_stateful_browser.py
Expand Up @@ -23,6 +23,19 @@ def test_request_forward():
assert r.text == 'Success!'


def test_properties():
"""Check that properties return the same value as the getter."""
browser = mechanicalsoup.StatefulBrowser()
browser.open_fake_page('<form></form>', url="http://example.com")
assert browser.page == browser.get_current_page()
assert browser.page is not None
assert browser.url == browser.get_url()
assert browser.url is not None
browser.select_form()
assert browser.form == browser.get_current_form()
assert browser.form is not None


def test_submit_online(httpbin):
"""Complete and submit the pizza form at http://httpbin.org/forms/post """
browser = mechanicalsoup.StatefulBrowser()
Expand Down

0 comments on commit 790b118

Please sign in to comment.