From 790b1184c268101243ba17a9c31c1e3a54f99ae1 Mon Sep 17 00:00:00 2001 From: Dan Hemberger Date: Thu, 4 Jan 2018 09:41:17 -0800 Subject: [PATCH] stateful_browser.py: add page, url, and form properties 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. --- mechanicalsoup/stateful_browser.py | 21 +++++++++++++++------ tests/test_stateful_browser.py | 13 +++++++++++++ 2 files changed, 28 insertions(+), 6 deletions(-) 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()