Brendan McCollam avatar Brendan McCollam committed 68f9f37

Form methods support taking a QWebElement as argument

Comments (0)

Files changed (1)

punky/browster.py

         else:
             raise PunkyElementNotFound("No page element found matching: {0}".format(selector))
 
+    def _get_element(self, selector_or_element):
+        if isinstance(selector_or_element, QWebElement):
+            return selector_or_element
+        else:
+            return self._first(selector_or_element)
+
     def first(self, selector):
         try:
             return self._first(selector)
         either a QWebElement instance, or string representing a CSS selector.
         If you pass in CSS, the first matching element will be the one clicked."""
 
-        if isinstance(selector_or_element, QWebElement):
-            el = selector_or_element
-        else:
-            el = self._first(selector_or_element)
+        el = self._get_element(selector_or_element)
 
         # Because we must create a new variable to initialize the mouse event
         # object, we must do the work inside an anonymous function in order to
         if self.webpage:
             del self.webpage
 
-    def back(self, timeout=None):
+    def back(self, timeout=10):
         """Navigate back, equivalent to using a browser 'back' button."""
         self.webpage.triggerAction(QWebPage.Back)
         return self._wait_load(timeout)
 
-    def forward(self, timeout=None):
+    def forward(self, timeout=10):
         """Navigate forward, equivalent to using a browser 'forward' button."""
         self.webpage.triggerAction(QWebPage.Forward)
         return self._wait_load(timeout)
         """Modify the CSS style of an element using a CSS selector."""
         self._first(selector).setAttribute('style', css)
 
-    def fill(self, selector, value):
+    def fill(self, selector_or_element, value):
         """Fill an input (of type text) or textarea with a string value using a
         CSS selector.
 
         setting this.value through JS works for both text inputs AND text areas.
 
         """
-        el = self._first(selector)
+        el = self._get_element(selector_or_element)
 
         if el.tagName() == "TEXTAREA" or (el.tagName() == "INPUT" and
                           el.attribute('type') in SUPPORTED_STRING_INPUT_TYPES):
                 )
 
 
-    def check(self, selector):
+    def check(self, selector_or_element):
         """Check a checkbox."""
-        el = self._first(selector)
+        el = self._get_element(selector_or_element)
 
         if el.tagName() == "INPUT" and el.attribute('type') in ('radio', 'checkbox'):
             el.evaluateJavaScript('this.checked=true;')
                     )
                 )
 
-    def uncheck(self, selector):
+    def uncheck(self, selector_or_element):
         """Uncheck a checkbox."""
-        el = self._first(selector)
+        el = self._get_element(selector_or_element)
 
         if el.tagName() == "INPUT" and el.attribute('type') == 'checkbox':
             el.evaluateJavaScript('this.checked=false;')
 
     choose = check          # choose a radio button
 
-    def select(self, selector):
+    def select(self, selector_or_element):
         """Mark a select widget's option selected."""
-        el = self._first(selector)
+        el = self._get_element(selector_or_element)
 
         if el.tagName() == "OPTION":
             # Make sure to mark the option as selected, but also manually
 
     #{ Extract values
 
-    def value(self, selector):
+    def value(self, selector_or_element):
         """Return the value of the first string form element that matches the
         given selector.
 
         selector, an exception will be raised.
 
         """
-        el = self._first(selector)
+        el = self._get_element(selector_or_element)
         if (el.tagName() == 'TEXTAREA' or
            (el.tagName() == 'INPUT' and
             str(el.attribute('type')) in SUPPORTED_STRING_INPUT_TYPES)):
         else:
             raise PunkyError('value() expected input[type=text] or textarea')
 
-    def checked(self, selector):
+    def checked(self, selector_or_element):
         """If the given selector references a checkbox or radio button, return
         whether it is checked.
 
         Otherwise raise an exception.
 
         """
-        el = self._first(selector)
+        el = self._get_element(selector_or_element)
         if el.tagName() == 'INPUT':
             input_type = str(el.attribute('type'))
             if input_type in ('radio', 'checkbox'):
 
         raise PunkyError('checked() expected input[type=radio] or input[type=checkbox]')
 
-    def selected(self, selector):
+    def selected(self, selector_or_element):
         """If the given selector references an option element, return whether it
         is selected.
 
         Otherwise raise an exception.
 
         """
-        el = self._first(selector)
+        el = self._get_element(selector_or_element)
         if el.tagName() == 'OPTION':
             v = self._first(selector).evaluateJavaScript('this.selected')
             return v.toBool()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.