diff --git a/mechanicalsoup/stateful_browser.py b/mechanicalsoup/stateful_browser.py index f701dc4a..f878515e 100644 --- a/mechanicalsoup/stateful_browser.py +++ b/mechanicalsoup/stateful_browser.py @@ -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). @@ -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`. """ @@ -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 diff --git a/tests/test_stateful_browser.py b/tests/test_stateful_browser.py index f581d465..7d0687fb 100644 --- a/tests/test_stateful_browser.py +++ b/tests/test_stateful_browser.py @@ -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('
', 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()