Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def get(self, tag, **kwargs):
"""Return an element matching ``tag``, an ``attribute`` and an
``index``. For example:
resp.doc.get('input', name='go') => xpath=//input[@name="go"]
resp.doc.get('li', description='Item') => xpath=//li[.="Item"]
"""
locator = _eval_xpath(tag, **kwargs)
return Element(self.resp, locator)
def css(self, selector):
"""Get an :class:`~webtest.browser.Element` using a css selector"""
return Element(self.resp, 'css=%s' % selector)
class Field(forms.Field, Element):
classes = {}
def __init__(self, *args, **kwargs):
super(Field, self).__init__(*args, **kwargs)
self.browser = self.form.browser
self.options = []
self.selectedIndices = []
self._forced_values = []
self.locator = _eval_xpath(self.tag,
locator=self.form.locator,
name=self.name)
value = property(Element.value__get, Element.value__set)
class Select(Field):
"""Field representing ``<select>``"""
def force_value(self, value):
self.select('value=%s' % value)
def value__set(self, value):
self.select('value=%s' % value)
def value__get(self):
return self.getSelectedValue()
value = property(value__get, value__set)
</select>
def __contains__(self, s):
if isinstance(s, Element):
return s.exist()
return self.browser.isTextPresent(json.dumps(s))
def __call__(self, locator):
return Element(locator)
###########
# Forms
###########
class Field(forms.Field, Element):
classes = {}
def __init__(self, *args, **kwargs):
super(Field, self).__init__(*args, **kwargs)
self.browser = self.form.browser
self.options = []
self.selectedIndices = []
self._forced_values = []
self.locator = _eval_xpath(self.tag,
locator=self.form.locator,
name=self.name)
value = property(Element.value__get, Element.value__set)
def __call__(self, locator):
return Element(locator)
def xpath(self, path):
"""Get an :class:`~webtest.browser.Element` using xpath"""
return Element(self.resp, 'xpath=%s' % path)
def __contains__(self, s):
if isinstance(s, Element):
return s.isElementPresent()
return s in self.text
Field.classes['button'] = Submit
Field.classes['image'] = Submit
class Forms(object):
def __init__(self, resp):
self.resp = resp
def __getitem__(self, key):
return Form(self.resp, key)
class Form(forms.Form, Element):
"""See :class:`~webtest.Form`"""
FieldClass = Field
def __init__(self, resp, id):
self.resp = resp
self.test_app = resp.test_app
self.browser = resp.browser
if isinstance(id, int):
self.locator = _eval_xpath('form', index=id)
else:
self.locator = _eval_xpath('form', id=id)
if not self:
raise LookupError('No form found at %s' % self.locator)
form = self.eval('e.innerHTML')
super(Form, self).__init__(resp, '<form>%s</form>' % form)